내용

글번호 69
작성자 heojk
작성일 2016-06-25 00:00:00
제목 [파이썬]고객관리 프로그램
내용 #개인용 주소록 프로젝트 #주소록을 관리하는 애플리케이션을 만들어야 합니다. ''' 요구사항 정의 주소록 애플리케이션은 고객의 주소 및 전화번호 등을 관리하는 애플리케이션 입니다. 관리해야 하는 고객의 정보는 이름, 전화번호, 이메일, 주소입니다. 고객의 정보를 입력/출력/삭제 하는 기능이 있어야 합니다. 현재 프로젝트는 고객 정보를 파일에 저장하지는 않습니다. 파일에 저장하는 기능을 다음 차수 프로젝트에 구현하기로 했습니다. 지금은 리스트를 통해 고객의 정보를 관리할 수 있도록 구현하면 됩니다. ''' class Customer: def __init__(self, name, phone, email, address): self.name = name self.phone = phone self.email = email self.address = address def to_string(self): str_ = "이름: {0}, 전화: {1}, 이메일: {2}, 주소: {3}" return str_.format(self.name, self.phone, self.email, self.address) class VIPCustomer(Customer) : def __init__(self, name, phone, email, address, note): Customer.__init__(self, name, phone, email, address) self.note = note def to_string(self): return Customer.to_string(self) + ", 특징: {0}".format(self.note) cust_list = [] def print_menu(): print(cust_list.__len__()) print("1.입력, 2.수정, 3.삭제, 4.리스트, 5.조회, 6.종료, 7.저장하기, 8.불러오기") menu = 0 try: menu = int(input("메뉴를 선택하세요 :")) except ValueError: raise Exception("메뉴 입력 에러입니다.") return menu def get_index(): index = int(input("인덱스를 입력하세요.")) return index def insert_cust_info(): print("입력 메뉴를 선택했습니다.=====================") name = input("이름 : ") phone = input("전화번호 : ") email = input("이메일 : ") address = input("주소 : ") is_vip = input("VIP 입니까?(Y/N): ") if is_vip.upper() == "Y" : note = input("특징: ") cust = VIPCustomer(name, phone, email, address, note) else : cust = Customer(name, phone, email, address) cust_list.append(cust) def update_cust_info(index): print("수정 메뉴를 선택했습니다.=====================") name = input("이름 [" + cust_list[index].name + "]: ") phone = input("전화번호 [" + cust_list[index].phone + "]: ") email = input("이메일 [" + cust_list[index].email + "]: ") address = input("주소[" + cust_list[index].address + "] : ") is_vip = input("VIP 입니까?(Y/N): ") if is_vip.upper() == "Y" : note = input("특징[" + cust_list[index].note + "] : ") cust = VIPCustomer(name, phone, email, address, note) else : cust = Customer(name, phone, email, address) cust_list[index] = cust def delete_cust_info(index): print("삭제 메뉴를 선택했습니다.=====================") cust_list.remove(cust_list[index]) def list_cust_info(): print("리스트 메뉴를 선택했습니다.=====================") for cust in cust_list: print(cust.to_string()) def view_cust_info(index): print("뷰 메뉴를 선택했습니다.=====================") print(cust_list[index].to_string()) def save_cust_info(): f = open("cust_db.txt", "wt") for cust in cust_list: f.write(cust.name + '\n') f.write(cust.email + '\n') f.write(cust.phone + '\n') f.write(cust.address + '\n') if isinstance(cust, VIPCustomer) : f.write(cust.note + '\n') else : f.write('\n') f.close() def load_cust_info(): f = open("cust_db.txt", "rt") lines = f.readlines() num = len(lines) / 5 num = int(num) for i in range(num): name = lines[5*i].rstrip('\n') email = lines[5*i+1].rstrip('\n') phone = lines[5*i+2].rstrip('\n') address = lines[5*i+3].rstrip('\n') note = lines[5*i+4].rstrip('\n') if note.__len__() > 0 : cust = VIPCustomer(name, phone, email, address, note) else : cust = Customer(name, phone, email, address) cust_list.append(cust) f.close() def main(): while 1: menu = print_menu() if menu == 1: insert_cust_info() elif menu == 2: try: index = get_index() update_cust_info(index) except Exception as e: print("[ERROR] " + str(e)) elif menu == 3: index = get_index() delete_cust_info(index) elif menu == 4: list_cust_info() elif menu == 5: index = get_index() view_cust_info(index) elif menu == 6: print("프로그램을 종료합니다.") break elif menu == 7: save_cust_info() elif menu == 8: load_cust_info() else : print("없는 메뉴 입니다.") if __name__ == "__main__": main()