Menu
CoddyTech

Recap - Bank Account Manager

Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 14 of 65.

challenge icon

Challenge

Medium

Write the class BankAccount in bank_account.gd. It brings together everything from this chapter.

  • A static int variable interest_percent that starts at 2, shared by all accounts
  • Two private properties: _owner_name (a String) and _balance (an int that starts at 0)
  • A read-only property owner_name whose getter returns _owner_name
  • A property balance whose getter returns _balance and whose setter prints Balance cannot be negative for a negative value and otherwise stores the value in _balance
  • The constructor _init(account_owner, start_balance) stores the owner and assigns start_balance to balance, so the setter checks it

And these methods:

  • deposit(amount): prints Deposit amount must be positive and returns false when amount is 0 or less; otherwise adds it and returns true
  • withdraw(amount): prints Withdrawal amount must be positive and returns false when amount is 0 or less, prints Insufficient funds and returns false when it is more than the balance, otherwise subtracts it and returns true
  • apply_interest(): adds _balance * interest_percent / 100 (whole dollars) to the balance and returns that interest
  • display_info(): prints three lines, Account Owner: NAME, Balance: $BALANCE and Interest 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 $5000

Your 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

Practice on your own: Online GDScript compiler