J o e
JoE's StOrY
J o e
  • 분류 전체보기 (206)
    • workSpace (184)
      • 도메인 지식 (2)
      • ALGORITHM (39)
      • ANDROID (3)
      • JS (0)
      • JAVA (21)
      • MYSQL (6)
      • NETWORK (3)
      • PYTHON (91)
      • LINUX (9)
      • PROJECT (4)
    • Others (20)
      • Opic (1)
      • myLife (17)
      • popSong (1)
      • 정보처리기사 (1)
    • 훈빠의 특강 (0)
      • opencv (0)
      • python (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • The code with long statements is⋯
  • 매일 매일이 행복하고 밝은 날이 될거에요

인기 글

태그

  • Fully Connected Network
  • java
  • 넘파이함수
  • full loss
  • sort_index
  • 넘파이 문제
  • ㅖ43
  • linearclassification
  • How to create a GUI in Java with JFrame?
  • 이미지 연산
  • MySQL
  • sort_value
  • 파이썬
  • 단어의 개수
  • DTO
  • numpy
  • Python
  • read_html
  • 태블릿 연동
  • dao

최근 댓글

최근 글

티스토리

J o e

WHY?

[Python] 제품입출력 프로그램 (MVC pattern)
workSpace/PYTHON

[Python] 제품입출력 프로그램 (MVC pattern)

2020. 12. 26. 20:22
class Product:
    cnt = 0  # 클래스 변수. 제품의 개수 카운팅

    def __init__(self, name, price, amount):  # 객체가 생성 될때마다 호출된다.
        Product.cnt += 1  # 중복되지 않게 카운트해준다.
        self.num = Product.cnt
        self.name = name
        self.price = price
        self.amount = amount

    def printProduct(self):
        print('num:', self.num)
        print('name:', self.name)
        print('price:', self.price)
        print('amount:', self.amount)


class Dao:  # 저장소 : 저장, 검색, 수정, 삭제
    def __init__(self):  # 생성자로 리스트를 사용했다.
        self.prod = []

    def insert(self, p):  # 프로덕트
        print('제품추가. p는 Product 객체')
        self.prod.append(p)  # 파라메터로 받아온 프로덕트 객체를 추가해준다.

    def select(self, num):  # 제품번호로 검색하여 그 위치를 반환해준다.
        for idx, p in enumerate(self.prod):  # 반복문으로 제품의 인덱스와 내용을 가져올수 있게 선언해준다.
            if num == p.num:  # 파라메터값으로 받아온 숫자와 제품의 번호와 같으면
                return idx, p  # 해당 인덱스를 리턴으로 반환해준다.

    def update(self, p):  # 제품 가격만 수정
        print('제품 p를 번호로 찾아서 새 정보로 수정')  # 제품이 있는가 확인하고, 있다면 수정이 가능하게 구현,
        res = self.select(p.num)
        if res != None:
            res[1].price = p.price
        else:
            print("없는 제품")

    def delete(self, num):
        print('제품 번호로 찾아서 삭제')
        res = self.select(num)
        if res != None:
            del self.prod[res[0]]
        else:
            print("없는 제품")

    def selectAll(self):
        return self.prod


class Service:
    def __init__(self):
        self.dao = Dao()

    def addProduct(self):
        print('제품명, 가격, 수량 입력받아 Product객체 생성 후 dao.insert()로 추가')
        name = input("제품명 :")
        price = int(input("가격 :"))
        a = int(input("수랑 :"))

        self.dao.insert(Product(name, price, a))

    def getProduct(self):
        print('검색할 번호 입력받아 dao.select()로 검색')
        num = int(input("num :"))  # 제품 입력받아서
        res = self.dao.select(num)  # dao에서 찾을 수 있도록 .
        # 제품이 있는지 확인.
        if res == None:
            print('없는 제품')
        else:
            res[1].printProduct()

    def editProduct(self):
        print('수정할 제품 번호와 새 가격 입력받아 dao.update()로 수정')
        num = int(input("num :"))  # 제품 입력받아서
        price = int(input("new price :"))
        p = Product('', price, 0)
        p.num = num
        self.dao.update(p)

    def delProduct(self):
        print('삭제할 제품 번호 입력받아 dao.delete()로 삭제')
        num = int(input("num :"))  # 제품 입력받아서
        self.dao.delete(num)

    def printAll(self):
        print('dao.selectAll()로 전체 검색한 결과 출력')
        datas = self.dao.selectAll()
        for p in datas:
            p.printProduct()





class Menu:
    def __init__(self):
        self.service = Service()

    def run(self):
        prompt = """1. 추가\n2. 검색\n3. 수정\n4. 삭제\n5. 전체목록\n6. 종료"""

        while True:
            print(prompt)
            nummber = int(input("숫자 입력 :"))
            if nummber == 1:
                self.service.addProduct()
            elif nummber == 2:
                self.service.getProduct()
            elif nummber == 3:
                self.service.editProduct()
            elif nummber == 4:
                self.service.delProduct()
            elif nummber == 5:
                self.service.printAll()
            elif nummber == 6:
                break


def main():
    m = Menu()
    m.run()


main()

'workSpace > PYTHON' 카테고리의 다른 글

[Python] 파일 생성 수정 삭제  (0) 2020.12.26
[Python] 주소록 프로그램  (0) 2020.12.26
[Python] 성적입출력 프로그램 (MVC pattern)  (0) 2020.12.26
[Python] bubble, insert, select sorting + 이진탐색  (0) 2020.12.26
[Python] 리스트 요소의 합과 평균, 최대값, 최소값 출력하기.  (0) 2020.12.26
    'workSpace/PYTHON' 카테고리의 다른 글
    • [Python] 파일 생성 수정 삭제
    • [Python] 주소록 프로그램
    • [Python] 성적입출력 프로그램 (MVC pattern)
    • [Python] bubble, insert, select sorting + 이진탐색
    J o e
    J o e
    나의 과거를 기록합니다.

    티스토리툴바