티스토리 뷰

python lecture/project

[python] 은행프로그램

burningrizen 2020. 7. 2. 22:13

 

1. 계좌생성

> 예금주 계좌번호 입금액

2. 입금

> 계좌번호 입금액

3. 출금

> 계좌번호 출금액

4. 이체

> 출금계좌번호 입금계좌번호 액수

5. 조회

> 계좌번호

6. 저장

> 파일로 일괄저장

> 계좌번호.txt 
> 계좌번호 파일에 통장이력은 아래와 같음

(기록시간, 입금 혹은 출금, 액수, 잔액)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import datetime


class Log:
    def __init__(self, add=0, sub=0, balance=0):
        self.now = datetime.datetime.now()
        if add:
            self.add = add
        if sub:
            self.sub = sub
        self.balance = balance

    def __str__(self):
        return f"{','.join(map(str, self.__dict__.items()))}"


class Account:
    def __init__(self, name, number):
        self.name = name
        self.number = number
        self.logs = list()
        self.balance = 0

    def add(self, add=0, sub=0):
        if add:
            self.balance += add
        if sub:
            self.balance -= sub
        log = Log(add=add, sub=sub, balance=self.balance)
        self.logs.append(log)

    def send(self, other, amount):
        self.add(sub=amount)
        other.add(add=amount)

    def __get_logs(self):
        return "\n".join(map(str, self.logs))

    def write(self):
        with open(f"./out/{self.number}.txt", "w") as f:
            f.writelines(self.__get_logs())

    def __str__(self):
        head = f"name={self.name} number={self.number}\n"
        body = self.__get_logs()
        return head + body


class Bank:
    def __init__(self):
        self.accounts = list()

    def find_account(self, number):
        accounts = list(filter(lambda account: account.number == number, self.accounts))
        return accounts[0] if accounts else None

    def create(self, name, number, amount):
        if not self.find_account(number):
            account = Account(name, number)
            account.add(add=amount)
            self.accounts.append(account)
            print(account)

    def deposit(self, number, amount):
        account = self.find_account(number)
        if account:
            account.add(add=amount)
            print(account)

    def withdrawal(self, number, amount):
        account = self.find_account(number)
        if account:
            account.add(sub=amount)
            print(account)

    def remittance(self, start_number, target_number, amount):
        start_account = self.find_account(start_number)
        target_account = self.find_account(target_number)
        if start_number and target_number:
            start_account.send(target_account, amount)
            print(start_account)
            print(target_account)

    def show_account(self, number):
        account = self.find_account(number)
        if account:
            print(account)

    def write(self):
        for account in self.accounts:
            account.write()


class CmdManager:
    def __init__(self):
        pass


def main():
    bank = Bank()
    while True:
        menu = input("메뉴를 입력 하시오\n")
        if menu == "1":
            cmd = input("개설할 계좌 예금주, 계좌번호, 입금금액을 입력하시오\n").split()
            name, number, amount = cmd[0], cmd[1], int(cmd[2])
            bank.create(name=name, number=number, amount=amount)

        elif menu == "2":
            cmd = input("입글할 계좌번호와 금액을 입력하시오\n").split()
            if len(cmd) == 2:
                number, amount = cmd[0], int(cmd[1])
                bank.deposit(number=number, amount=amount)

        elif menu == "3":
            cmd = input("출금할 계좌번호와 금액을 입력하시오\n").split()
            if len(cmd) == 2:
                number, amount = cmd[0], int(cmd[1])
                bank.withdrawal(number=number, amount=amount)

        elif menu == "4":
            cmd = input("이체할 계좌번호, 입금할 계좌번호, 금액을 입력하시오\n").split()
            if len(cmd) == 3:
                start_number, target_number, amount = cmd[0], cmd[1], int(cmd[2])
                bank.remittance(start_number=start_number, target_number=target_number, amount=amount)

        elif menu == "5":
            cmd = input("조회할 계좌번호를 입력하시오\n").split()
            if len(cmd) == 1:
                number = cmd[0]
                bank.show_account(number)

        elif menu == "6":
            bank.write()
            exit("수업종료")
        else:
            print("잘못된 명령어 입니다.")


main()

'python lecture > project' 카테고리의 다른 글

[python] 호텔 평점 프로젝트  (0) 2020.11.08
[Python] Pillow 영상처리 이미지 회전  (0) 2020.10.14
[python] 지하철 승하차 기록 조회  (0) 2020.06.07
[python] n 진수 바꾸기  (0) 2020.06.03
[python] 시간변환  (0) 2020.05.27
댓글