본문 바로가기
SW 코딩/파이썬

[파이썬 python] 전화번호부 만들기 - 미니 프로젝트

by 『 2021. 8. 15.
728x170

목표 기능

1. 로그인 기능

2. 연락처 추가

3. 연락처 삭제

4. 연락처 검색

5. module, class 사용하기

소스 코드

main.py
0.00MB
phonebook_extra.py
0.00MB

import getpass
import os
import phonebook_extra
 
'''
로그인 기능
ID/비밀번호 확인하기
'''
get_id = input("ID 입력: ")
 
'''
pycharm 에서 실행하기 getpass 실행하기 위해서는 'edit configuration'에서 
emulate terminal in output console 선택 필수
'''
get_password = getpass.getpass("비밀번호 입력: ")
print(get_password)
 
if get_id == "master" and get_password == "1234":
    print("로그인 성공")
else:
    print("로그인 실패")
    print("강제 종료합니다")
    exit()
 
def save_information():
    get_name = input("이름 입력하세요: ")
    get_phone_number = input("전화번호 입력하세요(ex,010-1234-1234): ")
    get_birth_day = input("생년월일을 입력하세요(ex,19900221): ")
 
    save_file_name = f'{get_name}_{get_phone_number}_{get_birth_day}.txt'
 
    windows_user_name = os.path.expanduser('~')
    try:
        os.makedirs(f'{windows_user_name }//Desktop//PhoneBook')
    except FileExistsError:
        pass
 
    phonebook_file = f'{windows_user_name}//Desktop//PhoneBook//{save_file_name}'
 
    with open(phonebook_file , 'w'as f:
        f.write(f'이름:{get_name}\n')
        f.write(f'번호:{get_phone_number}\n')
        f.write(f'생일:{get_birth_day}\n')
 
class DeleteInformation:
    # 생성자란?
    # 메서드 이름으로 __init__을 사용하면 메서드는 생성자가됨
    # 메서드(method): 클래스 안에 구현됨 함수
    # 객체에 초기값을 설정할 때 사용
    def __init__(self, name):
        self.name = name
 
    # 메서드(method): 클래스 안에 구현됨 함수
    def check_name_duplication(self):
        print(f'삭제 이름은{self.name} 입니다')
 
        windows_user_name = os.path.expanduser('~')
        get_all_file_name = os.listdir(f'{windows_user_name}//Desktop//PhoneBook')
        print(get_all_file_name)
 
        for each_file_name in get_all_file_name:
            if each_file_name.find(self.name) == -1:
                pass
            else:
                print(each_file_name)
                print("있습니다")
 
                os.remove(f'{windows_user_name}//Desktop//PhoneBook//{each_file_name}')
                print("삭제완료")
 
'''
class SaveInformation:
    def __init__(self, name, phone_number, birth_date):
        self.name = name
        self.phone_number = phone_number
        self.birth_date = birth_date
'''
 
def print_menu():
    print("----------------")
    print("1. 연락처 추가")
    print("2. 연락처 삭제")
    print("3. 연락처(이름) 검색")
    print("4. 연락처(생일) 검색")
    print("5. 나가기")
 
    choice_number = int(input("번호를 입력하세요:"))
 
    if choice_number == 1:
        print("추가할 연락처를 입력하세요")
        save_information()
    elif choice_number == 2:
        choice_delete_name = input("삭제할 연락처의 이름을 인력하세요: ")
        a = DeleteInformation(choice_delete_name)
        a.check_name_duplication()
    elif choice_number == 3:
        choice_name = input("검색할 이름을 입력하세요:")
        phonebook_extra.find_by_name(choice_name)
    elif choice_number == 4:
        choice_delete_birthday = input("검색할 생일을 입력하세요: ")
        b = phonebook_extra.FindByBirthday(choice_delete_birthday)
        b.find_by_birthday()
 
    elif choice_number == 5:
        exit()
    else:
        print("wrong input")
 
while True:
    print_menu()
 
 
 
cs

 

 

import os
 
windows_user_name = os.path.expanduser('~')
 
def find_by_name(find_name):
    get_all_file_name = os.listdir(f'{windows_user_name}//Desktop//PhoneBook')
 
    for each_file_name in get_all_file_name:
        if each_file_name.find(find_name) == 0:
            pass
        else:
            print(f'검색완료: {each_file_name}')
 
class FindByBirthday():
    def __init__(self, find_birthday):
        self.find_birthday = find_birthday
 
    def find_by_birthday(self):
        windows_user_name = os.path.expanduser('~')
        get_all_file_name = os.listdir(f'{windows_user_name}//Desktop//PhoneBook')
 
        for each_file_name in get_all_file_name:
            print(each_file_name)
            if each_file_name.find(self.find_birthday) == -1:
                pass
            else:
                print(f'검색완료: {each_file_name}')
 
 
 
cs
그리드형

댓글