내용

글번호 790
작성자 heojk
작성일 2017-12-21 16:48:45
제목 CustomerManager.py
내용 # -*- coding: utf-8 -*- """ Created on Thu Dec 21 14:33:46 2017 @author: JinKyoung Heo """ # %% """ 파이썬으로 고객관리 애플리케이션을 작성하세요. 고객 정보는 이름, 전화번호, 나이, 고객 등급(1~5), 이메일, 특징(고객의 외형상 특징)입니다. class Customer : def __init__(self, name, phone, email, age, grade, etc): self.name = name # 생략 def print_info(self): print(…) 고객 클래스를 이용하여 정보를 저장하고 리스트를 이용하여 관리해야 합니다. cust_list = [] 고객 정보들은 파일에 저장하고 불러올 수 있어야 합니다.(피클 사용) 리스트 데이터는 CSV 파일로 내보내기 기능이 있어야 합니다. 고객의 정보는 입력, 고객정보 전체 조회, 검색(이름으로 검색), 삭제(이름으로) 할 수 있어야 합니다. """ # %% class Customer: def __init__(self, name, phone, email, age, grade, etc): self.name = name self.phone = phone self.email = email self.age = age self.grade = grade self.etc = etc def print_info(self): print('{:>5} {:<3} {:<15} {:<15} {:>3}'.format('*'*self.grade, self.name, self.phone, self.email, self.age), self.etc, sep=' ', end='\n') def print_customer(cust_list): print("===============================================================") print(" 고 객 정 보 리 스 트") print("---------------------------------------------------------------") print('{:<5} {:<6} {:<15} {:<15} {:>3} {:}' .format("GRADE", "NAME ", "PHONE", "EMAIL", "AGE", "ETC")) print("===============================================================") for cust in cust_list: cust.print_info() print("===============================================================") def search_customer(cust_list, name): search_list = [] for i, cust in enumerate(cust_list): if cust.name == name: search_list.append(cust_list[i]) return search_list; def delete_customer(cust_list, name): for i, cust in enumerate(cust_list): if cust.name == name: del cust_list[i] def insert_customer_info(): name = input("이름: ") phone = input("전화번호: ") email = input("이메일: ") age = input("나이: ") grade = input("고객등급: ") etc = input("기타정보: ") cust = Customer(name, phone, email, age, int(grade), etc) return cust def save_customer(cust_list): try: f = open("customer_db.data", "wb") import pickle pickle.dump(cust_list, f) except Exception as e: print("데이터 저장 예외 :", e) finally: f.close() def load_customer(): try: f = open("customer_db.data", "rb") import pickle cust_list = pickle.load(f) except Exception as e: print("데이터 로드 예외 :", e) finally: f.close() return cust_list def print_menu(): print("1.입력", "2.전체조회", "3.삭제", "4.찾기", "5.내보내기(CSV)", "9. 종료", sep=" | ", end="") menu = input("메뉴선택: ") return int(menu) def save_csv(cust_list): try: file_name = input("저장할 파일 이름을 입력하세요.") f = open(file_name, "wt") for cust in cust_list: print(cust.name, cust.phone, cust.email, cust.age, cust.grade, cust.etc, sep=',', end="\n", file=f) except Exception as e: print("예외발생 :", e) else: print("CSV 파일일 저장됐습니다:", file_name) finally: f.close() def main(): cust_list = [] # cust = Customer("홍길동", "010-2222-2222", "hong@hong.com", 20, 2, "대머리") # cust2 = Customer("홍길서", "010-2223-2224", "kil@hong.com", 21, 4, "중머리") # cust3 = Customer("홍길남", "010-2223-2224", "nam@hong.com", 22, 1, "소머리") # cust_list.append(cust) # cust_list.append(cust2) # cust_list.append(cust3) # save_customer(cust_list) try: cust_list = load_customer() except OSError as e: print(e) else: print("데이터파일이 로드되었습니다.") while 1: menu = print_menu() if menu == 1: cust = insert_customer_info() cust_list.append(cust) elif menu == 2: print_customer(cust_list) elif menu == 3: name = input("삭제할 이름을 입력하세요.") delete_customer(cust_list, name) elif menu == 4: name = input("찾을 고객 이름을 입력하세요.") print_customer(search_customer(cust_list, name)) elif menu == 5: save_csv(cust_list) elif menu == 9: save_customer(cust_list) break if __name__ == '__main__': main()