Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 14 of 65.
Challenge
MediumWrite the class BankAccount in bank_account.gd. It brings together everything from this chapter.
- A static
intvariableinterest_percentthat starts at2, shared by all accounts - Two private properties:
_owner_name(aString) and_balance(anintthat starts at0) - A read-only property
owner_namewhose getter returns_owner_name - A property
balancewhose getter returns_balanceand whose setter printsBalance cannot be negativefor a negative value and otherwise stores the value in_balance - The constructor
_init(account_owner, start_balance)stores the owner and assignsstart_balancetobalance, so the setter checks it
And these methods:
deposit(amount): printsDeposit amount must be positiveand returnsfalsewhenamountis0or less; otherwise adds it and returnstruewithdraw(amount): printsWithdrawal amount must be positiveand returnsfalsewhenamountis0or less, printsInsufficient fundsand returnsfalsewhen it is more than the balance, otherwise subtracts it and returnstrueapply_interest(): adds_balance * interest_percent / 100(whole dollars) to the balance and returns that interestdisplay_info(): prints three lines,Account Owner: NAME,Balance: $BALANCEandInterest Rate: PERCENT%
The supplied code reads owner start_balance, how many commands follow, and the commands deposit N, withdraw N, set N (assigns balance), interest, rate N (sets BankAccount.interest_percent) and info. It prints a line for each successful deposit, withdrawal and interest payment, and the owner's balance at the end. For Coddy 1000 and the commands deposit 500, withdraw 200, interest, info, set 5000 and withdraw 10000 it prints:
Deposited $500
Withdrew $200
Interest earned: $26
Account Owner: Coddy
Balance: $1326
Interest Rate: 2%
Insufficient funds
Coddy has $5000Your code goes in bank_account.gd. main.gd holds the supplied input/output code and cannot be edited.
Try it yourself
extends Node
func _ready():
# Supplied input/output code: keep it as it is
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])
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self KeywordMethodsPropertiesConstructor (_init)Recap - Simple Calculator4Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator2Class Properties
Instance vs Static VariablesStatic FunctionsProperty SettersProperty GettersPrivate by ConventionRecap - Bank Account Manager5Encapsulation
Public and Private MembersValidation with SettersInformation HidingRead-Only PropertiesRecap - Student Records System8Advanced OOP Concepts
Composition vs InheritanceInner ClassesNodes as ComponentsRefCounted vs NodeEnums in Classes11Project: Library Management
Project OverviewBook and User Classes3Inheritance
Basic InheritanceThe super KeywordMethod OverridingInheritance ChainsType Checks with isRecap - Employee Hierarchy6Special Methods
Virtual Methods IntroductionThe _to_string MethodCustom IteratorsRecap - Custom List9Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy PatternPractice on your own: Online GDScript compiler