# diary.py
import os
import time
class DuplicateIDException(Exception):
def __init__(self, member_id):
super().__init__(f"[ERROR] '{member_id}' already exists.")
class NotRegisteredException(Exception):
def __init__(self, member_id):
super().__init__(f"[ERROR] '{member_id}' is a not registered member.")
class IncorrectPasswordException(Exception):
def __init__(self, member_id):
super().__init__(f"[ERROR] The password of '{member_id}' is incorrect.")
class DiaryManage():
def __init__(self):
self.member_db = dict()
def register_member(self, member_id, member_pw):
if member_id in self.member_db:
raise DuplicateIDException(member_id)
else:
self.member_db[member_id] = member_pw
def write_diary(self, member_id, member_pw, write_content):
if member_id not in self.member_db.keys():
raise NotRegisteredException(member_id)
elif self.member_db[member_id] != member_pw:
raise IncorrectPasswordException(member_id)
else:
write_path = os.path.join("./data", member_id + ".txt")
time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
with open(write_path, "a") as f:
f.write(f"[{time_stamp}] {write_content}\n")
def select_diary(self, member_id, member_pw):
if member_id not in self.member_db.keys():
raise NotRegisteredException(member_id)
elif self.member_db[member_id] != member_pw:
raise IncorrectPasswordException(member_id)
else:
write_path = os.path.join("./data", member_id + ".txt")
with open(write_path, "r") as f:
print(f.read())
# run.py
from diary import *
import time
if __name__ == "__main__":
# 클래스 초기화
dm = DiaryManage()
while True:
choice = int(input("\n원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): "))
if choice == 4:
break
# 가입
elif choice == 1:
member_id = input("ID: ")
member_pw = input("PW: ")
try:
dm.register_member(member_id, member_pw)
print(f"[SUCCESS] '{member_id}' 회원가입 완료!")
except DuplicateIDException as e:
print(e)
# 쓰기
elif choice == 2:
member_id = input("ID: ")
member_pw = input("PW: ")
write_content = input("작성 내용: ")
try:
dm.write_diary(member_id, member_pw, write_content)
print(f"[SUCCESS] '{member_id}'의 일기 저장 완료!")
except NotRegisteredException as e:
print(e)
except IncorrectPasswordException as e:
print(e)
# 보기
elif choice == 3:
member_id = input("ID: ")
member_pw = input("PW: ")
try:
dm.select_diary(member_id, member_pw)
except NotRegisteredException as e:
print(e)
except IncorrectPasswordException as e:
print(e)
# run.py 실행결과
# 원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): 1
# ID: hong
# PW: 1234
# [SUCCESS] 'hong' 회원가입 완료!
# 원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): 2
# ID: hong
# PW: 0000
# 작성 내용: I am sad today.
# [ERROR] The password of 'hong' is incorrect.
# 원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): 2
# ID: hong
# PW: 1234
# 작성 내용: I am sad today.
# [SUCCESS] 'hong'의 일기 저장 완료!
# 원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): 2
# ID: hong
# PW: 1234
# 작성 내용: I am happy today.
# [SUCCESS] 'hong'의 일기 저장 완료!
# 원하는 기능을 선택하세요 (1. 가입, 2. 쓰기, 3. 보기, 4. 종료): 3
# ID: hong
# PW: 1234
# [2024-11-09 15:49:21] I am sad today.
# [2024-11-11 08:12:58] I am happy today.
# account_book.py
import time
class AccountBook():
def __init__(self):
self.balance = 0
self.write_path = "./data/account_book.txt"
def income(self, amount, desc):
curr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.balance += amount
with open(self.write_path, "a") as f:
f.write(f"[입금/{curr_time}] {amount:,d}원, 잔액: {self.balance:,d}원, 내역: {desc}\n")
def expense(self, amount, desc):
curr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.balance -= amount
with open(self.write_path, "a") as f:
f.write(f"[출금/{curr_time}] {amount:,d}원, 잔액: {self.balance:,d}원, 내역: {desc}\n")
# run.py
from account_book import *
if __name__ == "__main__":
# 클래스 초기화
book = AccountBook()
while True:
choice = int(input("\n원하는 기능을 선택하세요 (1. 입금, 2. 출금, 3. 종료): "))
if choice == 3:
break
# 입금
elif choice == 1:
amt = int(input("입금액: "))
desc = input("내역: ")
book.income(amt, desc)
print(f"[SUCCESS] 입금 기록 완료!")
# 출금
elif choice == 2:
amt = int(input("입금액: "))
desc = input("내역: ")
book.expense(amt, desc)
print(f"[SUCCESS] 출금 기록 완료!")
# run.py 출력예시
# 원하는 기능을 선택하세요 (1. 입금, 2. 출금, 3. 종료): 1
# 입금액: 3000000
# 내역: 11월 월급
# [SUCCESS] 입금 기록 완료!
# 원하는 기능을 선택하세요 (1. 입금, 2. 출금, 3. 종료): 1
# 입금액: 50000
# 내역: 적금 이자
# [SUCCESS] 입금 기록 완료!
# 원하는 기능을 선택하세요 (1. 입금, 2. 출금, 3. 종료): 2
# 입금액: 1700000
# 내역: 카드대금
# [SUCCESS] 출금 기록 완료!
# 원하는 기능을 선택하세요 (1. 입금, 2. 출금, 3. 종료): 3
# account_book.txt 저장예시
# [입금/2024-11-10 16:10:24] 3,000,000원, 잔액: 3,000,000원, 내역: 11월 월급
# [입금/2024-11-10 16:10:33] 50,000원, 잔액: 3,050,000원, 내역: 적금 이자
# [출금/2024-11-10 16:10:41] 1,700,000원, 잔액: 1,350,000원, 내역: 카드대금
*이 글은 제로베이스 데이터 취업 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.