THINK FIRST·CODE LATER

← All labs

Savings and checking accounts

Problem

Write a superclass Account (protected id and balance; deposit, boolean withdraw that refuses non-positive amounts or more than the balance, an empty monthEnd(), toString() → S-1 1250.00) and two subclasses:

  • SavingsAccount(id, balance, annualRate) — withdraw refuses anything that would leave less than MINIMUM = 100, otherwise it calls super.withdraw; monthEnd() adds one month of interest (balance × rate / 100 / 12).
  • CheckingAccount(id, balance, overdraftLimit) — withdraw may go below zero down to −overdraftLimit and counts withdrawals; monthEnd() charges FEE = 2 per withdrawal beyond the first three, then resets the counter.

BankBranch.main reads the savings balance and rate, the checking balance and overdraft, then operations D S-1 200, W C-1 50, M (month end) until the end of input. Refused withdrawals print refused: withdraw 500.00 from S-1. Finally print both accounts (Savings S-1 …, Checking C-1 …).

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-4-data-1.txt

1000 6 300 200
W S-1 950
W S-1 900
W C-1 400
W C-1 200
D C-1 100
M
What your program must print for l14-4-data-1.txt
refused: withdraw 950.00 from S-1
refused: withdraw 200.00 from C-1
month end
Savings  S-1 100.50
Checking C-1 0.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-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-4-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.