728x90
반응형

주제 : 고객 정보 관리 시스템
진행인원 : 1명
 

1. 주요 내용
지난 실습에 작성한 기존의 메인 로직에 데이터 저장 기능 추가하기
 
2. 요구사항
- 'S' 메뉴 > 고객 정보를 파일로 저장하는 기능 추가
- 프로그램 실행시 자동으로 저장된 고객정보 읽어와 복원
- 프로그램 종료시 자동으로 고객 정보 파일로 저장
 

이전 실습코드에 따로 추가한 내용

- 함수를 따로 작성하여 import 해 오는 방법을 사용해  더욱 간단한 코드로 변경한 상태이다. 이렇게 함수를 따로 작성하면 오류가 어디서 나는지 바로 알 수 있어서 수정하는데 유용하다.

from cust.common import print_menu
from cust.insert.do_insert import do_I
from cust.delete.do_delete import do_D
from cust.update.do_update import do_U
from cust.read.do_n import do_N
from cust.read.do_p import do_P
from cust.read.do_c import do_C

# Main
def main():
    customers = list()
    index = -1

    while True:
        menu = print_menu()
        if menu == 'I':
            index = do_I(customers, index)
            # print('index : ', index)
            # print('customers : ', customers)
        elif menu == 'P':
            do_P(customers, index)
        elif menu == 'C':
            do_C(customers, index)
        elif menu == 'N':
            do_N(customers, index)
        elif menu == 'U':
            do_U(customers, index)
        elif menu == 'D':
            index = do_D(customers, index)
        elif menu == 'Q':
            print('안녕히가세요~')
            break
        else:
            print('잘못 입력하셨습니다. 다시 입력 해주세요')
            
if __name__ == '__main__':
    main()

따로 작성해둔 함수들을 import 해오는 방식을 쓴다.

 
- do_insert.py 코드

     > lambda 함수 사용

from cust.common import chk_input_data

def do_I(customers, index):
    print('고객정보 입력')
    customer = { 'name':'', 'gender': '', 'email': '', 'year': 0 }
    customer['name'] = chk_input_data('이름을 입력하세요 : ', lambda x: True if len(x) > 2 else False) # 조건식이 참일 때 True리턴 거짓일때 False리턴
    customer['gender'] = chk_input_data('성별 (M/F)를 입력하세요 : ', lambda x: True if x in ('M','F') else False)
    customer['email'] = chk_input_data('이메일 주소를 입력하세요 : ', lambda x: True if '@' in x else False)
    customer['year'] = chk_input_data('출생년도 4자리 입력하세요 : ', lambda x: True if len(x) == 4 and x.isdigit() else False)
    customers.append(customer)
    index = len(customers) - 1
    
    return index

 
요구사항을 반영해 적어 본 코드
 

- do_save.py 코드
     > pickle 라이브러리 사용

import pickle

def do_S(customers, index):
    print('고객 정보 저장')
    with open('cust/custInfo.pickle', 'wb') as info:
        pickle.dump(customers, info)

 
※ pickle 설명
https://mzero.tistory.com/26

 

[Digital Boot] 파이썬 수업 5일차

range() - range()로 반복 횟수를 전달하면 range()가 자동으로 순차적인 정수들을 생성해준다. - range( start = 0, stop, strp = 1 ) > start : 시작값 > stop : 종료값이지만 stop은 포함되지 않는다. > step : 한번에

mzero.tistory.com

 
- do_s() 적용한 코드

import pickle
from cust.common import print_menu
from cust.insert.do_insert import do_I
from cust.delete.do_delete import do_D
from cust.update.do_update import do_U
from cust.save.do_save import do_S
from cust.read.do_n import do_N
from cust.read.do_p import do_P
from cust.read.do_c import do_C

# Main
def main():
    customers = list()
    index = -1

    with open('cust\\custInfo.pickle', 'rb') as info:
        a = pickle.load(info)
        customers.extend(a)
        print(customers)

    while True:
        menu = print_menu()
        if menu == 'I':
            index = do_I(customers, index)
            # print('index : ', index)
            # print('customers : ', customers)
            a.extend(customers)
            print(customers)
        elif menu == 'P':
            do_P(customers, index)
        elif menu == 'C':
            do_C(customers, index)
        elif menu == 'N':
            do_N(customers, index)
        elif menu == 'U':
            do_U(customers, index)
        elif menu == 'D':
            index = do_D(customers, index)
        elif menu == 'S':
            do_S(customers, index)
        elif menu == 'Q':
            do_S(customers, index)
            print('안녕히가세요~')
            break
        else:
            print('잘못 입력하셨습니다. 다시 입력 해주세요')

if __name__ == '__main__':
    main()
728x90
반응형

+ Recent posts