은행 계좌 클래스
잔액을 저장하고 입금과 출금을 처리하는 BankAccount 클래스를 작성하시오.
작성할 클래스
다음 형태의 클래스를 작성한다.
class BankAccount:
def __init__(self, balance):
# 코드를 작성하세요.
def deposit(self, amount):
# 코드를 작성하세요.
def withdraw(self, amount):
# 코드를 작성하세요.
def get_balance(self):
# 코드를 작성하세요.
- 생성자
__init__(balance)는 초기 잔액balance를 저장한다. deposit(amount)는 잔액에amount를 더하고 값을 반환하지 않는다.withdraw(amount)는 잔액이amount이상이면 잔액에서amount를 빼고True를 반환한다.- 잔액이 부족하면
withdraw(amount)는 잔액을 변경하지 않고False를 반환한다. get_balance()는 현재 잔액을int로 반환한다.
클래스를 사용하는 코드
작성한 클래스는 아래 코드와 함께 실행된다.
balance = int(input())
q = int(input())
account = BankAccount(balance)
for _ in range(q):
command = input().split()
if command[0] == "deposit":
result = account.deposit(int(command[1]))
print(type(result))
elif command[0] == "withdraw":
result = account.withdraw(int(command[1]))
print(result)
print(type(result))
else:
result = account.get_balance()
print(result)
print(type(result))
위 코드가 정상적으로 동작하도록 클래스를 작성한다. 제출할 때는 작성한 클래스의 정의 전체만 제출한다. 입력을 받거나 객체를 생성하는 코드는 제출하지 않는다.
입력
첫째 줄에 초기 잔액 balance가 주어진다.
둘째 줄에 명령의 개수 \(Q\)가 주어진다.
다음 \(Q\)개의 줄에 명령이 하나씩 주어진다. 명령의 종류는 다음과 같다.
deposit amount:deposit(amount)를 호출한다.withdraw amount:withdraw(amount)를 호출한다.balance:get_balance()를 호출한다.\(0 \le balance \le 1,000,000,000\)
- \(1 \le Q \le 1,000\)
- \(1 \le amount \le 1,000,000,000\)
출력
각 deposit 명령에서는 deposit()이 반환한 값의 자료형을 출력한다.
각 withdraw 명령에서는 출금 성공 여부와 withdraw()가 반환한 값의 자료형을 출력한다.
각 balance 명령에서는 현재 잔액과 get_balance()가 반환한 값의 자료형을 출력한다.
각 메서드를 호출한 뒤에는 반환값의 자료형도 출력한다.
예제 입력 1
1000
8
balance
deposit 500
withdraw 700
balance
withdraw 900
deposit 200
withdraw 900
balance
예제 출력 1
1000
<class 'int'>
<class 'NoneType'>
True
<class 'bool'>
800
<class 'int'>
False
<class 'bool'>
<class 'NoneType'>
True
<class 'bool'>
100
<class 'int'>
예제 설명 1
처음 계좌의 잔액은 \(1,000\)이다.
- 첫 번째
balance는 현재 잔액1000을 반환한다. deposit 500으로 잔액이 \(1,500\)이 된다.deposit()은 값을 반환하지 않는다.withdraw 700은 성공하여True를 반환하고 잔액은 \(800\)이 된다.- 두 번째
balance는 현재 잔액800을 반환한다. withdraw 900은 잔액이 부족하므로 실패하여False를 반환하며 잔액은 변하지 않는다.deposit 200으로 잔액이 \(1,000\)이 된다.withdraw 900은 성공하여True를 반환하고 잔액은 \(100\)이 된다.- 마지막
balance는 현재 잔액100을 반환한다.
예제 입력 2
0
5
balance
deposit 100
withdraw 50
withdraw 50
balance
예제 출력 2
0
<class 'int'>
<class 'NoneType'>
True
<class 'bool'>
True
<class 'bool'>
0
<class 'int'>
코멘트