Q. 제품 등록, 수정, 삭제 등 제품관리 프로그램을 만드시오. (단, 제품번호는 자동할당 받아야 됌)

제품정보에는 제품번호, 제품명, 가격, 수량

메뉴에는 1.등록 2.검색 3.수정 4.삭제 5.전체출력 6.종료

 

#VO

class Product:
    cnt = 0
    def __init__(self, name='', price=0, amount=0):
        Product.cnt += 1
        self.num = Product.cnt
        self.name = name
        self.price = price
        self.amount = amount

    def __str__(self):
        return '제품번호:' + str(self.num) + '/ 제품명:' + self.name + '/ 가격:' + str(self.price) + '/ 수량:' + str(self.amount)
        print()

#DAO
class ProdDao:
    def __init__(self):
        self.datas = []

    def insert(self, p):
        self.datas.append(p)

    def delete(self, p):
        self.datas.remove(p)

    def select(self, num):
        for i in self.datas:
            if i.num == num:
                return i

    def selectAll(self):
        return self.datas


#Service
class ProdService:
    def __init__(self):
        self.dao = ProdDao()

    def addProd(self):
        name = input('제품명:')
        price = input('가격:')
        amount = input('수량:')

        p = Product(name, price, amount)
        self.dao.insert(p)

    def printProd(self):
        num = int(input('검색할 제품번호를 입력하시오.'))
        p = self.dao.select(num)
        if p == None:
            print('제품을 찾을 수 없습니다.')
        else:
            print(p)

    def editProd(self):
        num = int(input('검색할 제품번호를 입력하시오.:'))
        pd = self.dao.select(num)
        if pd == None:
            print('제품을 찾을 수 없습니다.')
        else:
            pd.price=int(input('수정할 가격을 입력하시오.'))
            pd.amount=int(input('수정할 수량을 입력하시오.'))
            print('수정되었습니다.')

    def delProd(self):
        num = int(input('검색할 제품번호를 입력하시오.:'))
        pd = self.dao.select(num)
        if pd == None:
            print('제품을 찾을 수 없습니다.')
        else:
            self.dao.delete(pd)
            print('삭제되었습니다.')

    def printAll(self):
        datas = self.dao.selectAll()
        for i in datas:
            print(i)

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

    def run(self):
        while True:
            menu = int(input('1.등록 2.검색 3.수정 4.삭제 5.전체출력 6.종료'))
            if menu == 1:
                self.service.addProd()
            if menu == 2:
                self.service.printProd()
            if menu == 3:
                self.service.editProd()
            if menu == 4:
                self.service.delProd()
            if menu == 5:
                self.service.printAll()
            if menu == 6:
                break

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

main()

야호! 간단한 프로그램이지만 뭔가를 만들어 내는 것에 익숙해지고 있다.

맨 처음 오류는 변수 num이 int타입인데 서비스 클래스에서 printProd 메소드에서의 검색할 num을 그냥 str로 입력받아 오류가 났었다.

그리고 두번째는 Attributeerror: 'Product' object has no attribute 'printprod'가 떴었다.

처음에는 Product 클래스에서 출력메소드명을 print라고 했었는데 이게 printProd와 서로 같지 않아서 생긴 오류였다.

서로 다른 클래스 내에 속해있지만 같은 함수명을 가져야 했구나... 거의 2시간동안 오류가 난 것이 죄다 printProd 메소드때문이어서 당분간 쟤랑 좀 절교해야겠다.

 

+ Recent posts