728x90
반응형

입출금 클래스 만들기

 

1. 요구사항

- 은행 계좌
- 잔액(balance) 상태
- 저금(deposit) 행동, 인출(witndraw) 행동

 

2. 내가 작성해 본 코드

class Bank():
    def __init__(self):
        self.__balance = 0
        self.__money = 0
    def deposit(self, money):
        if money > 0:
            self.__money = money
            self.__balance += self.__money
        print('입금되었습니다.')
        print(f'잔액이 {self.__balance} 남았습니다.')
        return self.__balance
    def withdraw(self, money):
        if money > 0:
            self.__money = money
            self.__balance -= self.__money
        print('출금되었습니다.')
        print(f'잔액이 {self.__balance} 남았습니다.')
        return self.__balance

 

 2 -1. 실행 결과


3. 강사님 코드

   - 전달 받은 금액을 잔액에 누적한다.
    - 입금 금액의 범위가 0보다는 커야 한다.
    - 최대 입금액 한도를 초과하지 않아야 한다.
    - (옵션) 총 잔액을 반환한다.

class BankAccount:
    def __init__(self):
        self.__balance = 0
           
    def deposit(self, amount):
        if 0 < amount and 10000000 > amount:
            self.__balance += amount
        print(f'{amount}를 입금 하셨습니다. \n 잔액은 총 {self.__balance}입니다.')
        return self.__balance
    
    def withdraw(self, amount):
        if 0 < amount and 10000000 > amount:
            self.__balance -= amount
            print(f'{amount}를 출금 하셨습니다. \n 잔액은 총 {self.__balance}입니다.')
            return self.__balance

 

3 -1. 실행 결과

 

728x90
반응형

+ Recent posts