복습 - 은행 계좌 관리자
Coddy GDScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 65개 중 14번째.
챌린지
중급bank_account.gd에 BankAccount 클래스를 작성하세요. 이 클래스에는 이번 장에서 배운 모든 내용이 담겨 있습니다.
- 모든 계정이 공유하는 정적
int변수interest_percent의 초기값은2입니다. - 비공개 속성 두 개:
_owner_name(String)과 초기값이0인_balance(int) - 읽기 전용 속성
owner_name의 getter는_owner_name을 반환합니다. - 속성
balance의 getter는_balance를 반환하고, setter는 음수 값이면Balance cannot be negative를 출력하며, 그렇지 않으면 값을_balance에 저장합니다. - 생성자
_init(account_owner, start_balance)는 소유자를 저장하고start_balance를balance에 할당하여 setter가 값을 확인하도록 합니다.
그리고 다음 메서드를 작성하세요.
deposit(amount):amount가0이하이면Deposit amount must be positive을 출력하고false를 반환합니다. 그렇지 않으면 금액을 더하고true를 반환합니다.withdraw(amount):amount가0이하이면Withdrawal amount must be positive를 출력하고false를 반환합니다. 잔액보다 많으면Insufficient funds을 출력하고false를 반환합니다. 그렇지 않으면 금액을 빼고true를 반환합니다.apply_interest():_balance * interest_percent / 100(정수 달러 금액)을 잔액에 더하고 해당 이자를 반환합니다.display_info(): 세 줄을 출력합니다.Account Owner: NAME,Balance: $BALANCE,Interest Rate: PERCENT%
제공된 코드는 owner start_balance, 이어지는 명령의 개수, 그리고 deposit N, withdraw N, set N(balance를 할당), interest, rate N(BankAccount.interest_percent를 설정), info 명령을 읽습니다. 입금, 출금, 이자 지급이 성공할 때마다 한 줄을 출력하고, 마지막에는 소유자의 잔액을 출력합니다. Coddy 1000과 명령 deposit 500, withdraw 200, interest, info, set 5000, withdraw 10000을 입력하면 다음을 출력합니다.
Deposited $500
Withdrew $200
Interest earned: $26
Account Owner: Coddy
Balance: $1326
Interest Rate: 2%
Insufficient funds
Coddy has $5000코드는 bank_account.gd에 작성하세요. main.gd에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다.
직접 해보기
extends Node
func _ready():
# 제공된 입력/출력 코드: 그대로 유지하세요
var first := OS.read_string_from_stdin().split(" ")
var account := BankAccount.new(first[0], int(first[1]))
var count := int(OS.read_string_from_stdin())
for i in count:
var parts := OS.read_string_from_stdin().split(" ")
match parts[0]:
"deposit":
if account.deposit(int(parts[1])):
print("Deposited $%s" % parts[1])
"withdraw":
if account.withdraw(int(parts[1])):
print("Withdrew $%s" % parts[1])
"set":
account.balance = int(parts[1])
"interest":
print("Interest earned: $%d" % account.apply_interest())
"rate":
BankAccount.interest_percent = int(parts[1])
"info":
account.display_info()
print("%s has $%d" % [account.owner_name, account.balance])
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 GDScript 컴파일러