THINK FIRST·CODE LATER

← All labs

An account that keeps its history

Problem

Write the classes Transaction and Account, and a public class AccountHistory.

  • A Transaction records its number, its type ('D' deposit or 'W' withdrawal), the amount, the balance after the operation and a description; its toString() formats one line of the statement.
  • An Account has an owner, an id, a balance and a private ArrayList<Transaction>. deposit(amount, description) and withdraw(amount, description) update the balance and add a transaction; withdraw returns false, and records nothing, when the balance is too low. Also write getTransactionCount(), getTransaction(int) and total(char type).

main reads owner id openingBalance, then operations D amount description or W amount description until the end of the input, and prints the statement. For the input

George 1122 1000
D 30 salary
D 40 gift
W 5 coffee
W 2000 car
W 4 bus
D 50 refund

the program prints

Account 1122 of George opens with 1000.00
Refused: 2000.00 for car (balance 1065.00)
 #  T    Amount    Balance  Description
 1  D     30.00    1030.00  salary
 2  D     40.00    1070.00  gift
 3  W      5.00    1065.00  coffee
 4  W      4.00    1061.00  bus
 5  D     50.00    1111.00  refund
Deposits 120.00, withdrawals 9.00, final balance 1111.00

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l14-9-data-1.txt

George 1122 1000
D 30 salary
D 40 gift
W 5 coffee
W 2000 car
W 4 bus
D 50 refund
What your program must print for l14-9-data-1.txt
Account 1122 of George opens with 1000.00
Refused: 2000.00 for car (balance 1065.00)
 #  T    Amount    Balance  Description
 1  D     30.00    1030.00  salary
 2  D     40.00    1070.00  gift
 3  W      5.00    1065.00  coffee
 4  W      4.00    1061.00  bus
 5  D     50.00    1111.00  refund
Deposits 120.00, withdrawals 9.00, final balance 1111.00

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l14-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.