Accounts
Time for the last project! This checkpoint puts the last two lessons to work, generics and classes, together with everything you learned before them. As in the other checkpoints, the instructions say what the program should print, how you write it is up to you, and each exercise also runs your program with other data.
The project. Adventurers don't carry all their gold around: they keep it in the adventurers' bank. Over three exercises, you'll write the bank's program: its accounts, the transactions at its counter, and accounts that keep their gold in the vault.
The data. The array accounts at the top of the program holds the bank's accounts. Each of them is an object of the class Account, created with new Account(owner, gold): the name of its owner and the gold in it this morning. The class doesn't exist yet, so TypeScript reports Cannot find name 'Account'. Writing it is your first task.
The rules. The owner of an account never changes. Anyone may find out how much gold is in an account, but only the account's own methods may change it, so that the bank's rules always apply. You'll write those methods in the next exercise.
The task. Write the class above the array: a class has to be declared before the program creates objects from it. Then print every account and the gold that the bank holds in all:
Kira: 120 gold
Bruno: 40 gold
Lena: 75 gold
The bank holds 235 gold.A tip: readonly and private from the Classes lesson let TypeScript guard the rules for you. And a method that describes the account will come in handy later.
Instructions
Write the class Account, and print every account with its owner and gold, as in the example.
Below the accounts, print how much gold the bank holds.
Start programming for free
1/3