Program Requirements You are to construct a program named AccountBank.java that
ID: 3675010 • Letter: P
Question
Program Requirements
You are to construct a program named AccountBank.java that keeps a list of files containing names and balance of patrons. The program will ask the patron for a command, execute the command, then ask the patron for another command. The commands must be chosen from the following options.
a Show all records
r Remove the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
d Add a deposit to the current record
w Make a withdrawal from the current record
q Quit
s Select a record from the record list to become the current record
The following example illustrates the behavior of each command (user input is in bold)
c:java Bank [enter]
A Program to keep a Bank Record:
a Show all records
r Remove the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
d Add a deposit to the current record
w Make a withdrawal from the current record
q Quit
s Select a record from the record list to become the current record
Enter a command from the list above (q to quit): a
No records exist!!
a Show all records
r Remove the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
d Add a deposit to the current record
w Make a withdrawal from the current record
q Quit
s Select a record from the record list to become the current record
Enter a command from the list above (q to quit): n
Enter first name: Nathan
Enter last name: Drake
Enter the balance amount: 1000
Current record is: Nathan Drake $1000.00
a Show all records
r Remove the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
d Add a deposit to the current record
w Make a withdrawal from the current record
q Quit
s Select a record from the record list to become the new current record
Enter a command from the list above (q to quit): n
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
Enter first name: Ada
Enter last name: Wong
Current record is: Ada Wong
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Current balance
------------- ------------- ------------------
Ada Wong $0.00
Nathan Drake $1000.00
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): n
Enter first name: Elwood
Enter last name: Havens
Current record is: Elwood Havens
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Current Balance
------------- ------------- ------------------
Ada Wong $0.00
Nathan Drake $1000.00
Elwood Havens $0.00
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): f
Enter new first name: Jake
Current record is: Jake Havens
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): s
Enter first name: Carl
Enter last name: Patton
No matching record found.
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): s
Enter first name: Nathan
Enter last name: Drake
Current record is: Nathan Drake
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): r
Deleted: Nathan Drake
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Current Balance
------------- ------------- ------------------
Ada Wong $0.00
Elwood Havens $0.00
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): s
Enter first name: Ada
Enter last name: Wong
Current record is: Ada Wong
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): d
Enter the deposited amount: 1000.00
Current record is: Ada Wong with 1000.00 added to deposit
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Current Balance
------------- ------------- ------------------
Ada Wong $1000.00
Elwood Havens $0.00
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): q
The output of your program must match the format illustrated in this example.
Extra things to put in
After a deletion, there is no record currently selected
Each record (first name, last name and balance) must be stored as an object. These objects must be stored in a Linked List.
The Linked List must be kept sorted at all times based on last name. Sorting is to be achieved when an insertion or modification is done. NO SORTING ROUTINE IS ALLOWED. ALWAYSINSERT A NEW RECORD OR EDITED RECORD INTO ITS' CORRECT PLACE IN THE LINKED LIST. Note: Changing the last name will require resorting.
Explanation / Answer
// AccountBank.java
public class AccountBank
{
private String firstName;
private String lastName;
private double balance;
public AccountBank()
{
firstName = "";
lastName = "";
balance = 0.0;
}
public AccountBank(String first, String last)
{
firstName = first;
lastName = last;
balance = 0;
}
public AccountBank(String first, String last, double bal)
{
firstName = first;
lastName = last;
balance = bal;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getBalance()
{
return balance;
}
public void setFirstName(String first)
{
firstName = first;
}
public void setLastName(String last)
{
lastName = last;
}
public void setBalance(double bal)
{
balance = bal;
}
public void deposit(double amount)
{
if(amount < 0)
System.out.println("Nagative amount is not allowed!");
else
balance = balance + amount;
}
public void withdraw(double amount)
{
if(amount < 0)
System.out.println("Nagative amount is not allowed!");
else if(balance - amount < 0)
System.out.println("Insufficient balance!");
else
balance = balance - amount;
}
public String toString()
{
return String.format("%-15s%-15s%15s", firstName, lastName, String.format("$%.2f", balance));
}
}
// AccountBankDemo.java
import java.util.Scanner;
import java.util.LinkedList;
public class AccountBankDemo
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
LinkedList<AccountBank> records = new LinkedList<AccountBank>();
AccountBank currRec = null;
System.out.println("A Program to keep a Bank Record:");
char choice;
do
{
choice = getMenuChoice();
switch(choice)
{
case 'a':
printAllRecords(records);
break;
case 'r':
removeCurrentRecord(records, currRec);
sortRecords(records);
break;
case 'f':
changeCurrentRecordFirstName(records, currRec);
break;
case 'l':
changeCurrentRecordLastName(records, currRec);
sortRecords(records);
break;
case 'n':
insertNewRecord(records);
currRec = records.get(records.size() - 1);
printCurrentRecord(currRec);
sortRecords(records);
break;
case 'd':
depositToCurrentRecord(records, currRec);
break;
case 'w':
withdrawToCurrentRecord(records, currRec);
break;
case 'q':
System.out.println("Thank yout");
break;
case 's':
selectCurrentRecord(records, currRec);
break;
default:
System.out.println("Invalid choice");
}
}while(choice != 'q');
}
public static void sortRecords(LinkedList<AccountBank> records)
{
for(int i = 0 ; i < records.size() - 1; i++)
{
int minPos = i;
for (int j = i + 1 ; j < records.size() ; j++ )
{
if (records.get(j).getLastName().compareToIgnoreCase(records.get(minPos).getLastName()) < 0)
minPos = j;
}
if(minPos != i)
{
AccountBank temp =records.get(i);
records.set(i, records.get(minPos));
records.set(minPos, temp);
}
}
}
public static void selectCurrentRecord(LinkedList<AccountBank> records, AccountBank currRec)
{
System.out.print("Enter first name: ");
String first = keyboard.next();
System.out.print("Enter last name: ");
String last = keyboard.next();
boolean found = false;
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(first) && records.get(i).getLastName().equalsIgnoreCase(last))
{
found = true;
currRec = records.get(i);
System.out.println("Current record is: " + currRec.getFirstName() + " " + currRec.getLastName());
break;
}
}
if(!found)
{
System.out.println("No matching record found.");
}
}
public static void withdrawToCurrentRecord(LinkedList<AccountBank> records, AccountBank currRec)
{
if(currRec == null)
System.out.println("No current record exits!");
else if(records.size() == 0)
System.out.println("No records exist!!");
else
{
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(currRec.getFirstName()) && records.get(i).getLastName().equalsIgnoreCase(currRec.getLastName()))
{
System.out.print("Enter the withdrawn amount: ");
double amount = keyboard.nextDouble();
records.get(i).withdraw(amount);
System.out.println("Current record is: " + records.get(i).getFirstName() + " " + records.get(i).getLastName() + " with " + String.format("%.2f", amount) + " removed from deposit");
break;
}
}
}
}
public static void depositToCurrentRecord(LinkedList<AccountBank> records, AccountBank currRec)
{
if(currRec == null)
System.out.println("No current record exits!");
else if(records.size() == 0)
System.out.println("No records exist!!");
else
{
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(currRec.getFirstName()) && records.get(i).getLastName().equalsIgnoreCase(currRec.getLastName()))
{
System.out.print("Enter the deposited amount: ");
double amount = keyboard.nextDouble();
records.get(i).deposit(amount);
System.out.println("Current record is: " + records.get(i).getFirstName() + " " + records.get(i).getLastName() + " with " + String.format("%.2f", amount) + " added to deposit");
break;
}
}
}
}
public static void changeCurrentRecordLastName(LinkedList<AccountBank> records, AccountBank currRec)
{
if(currRec == null)
System.out.println("No current record exits!");
else if(records.size() == 0)
System.out.println("No records exist!!");
else
{
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(currRec.getFirstName()) && records.get(i).getLastName().equalsIgnoreCase(currRec.getLastName()))
{
System.out.print("Enter new last name: ");
String last = keyboard.next();
records.get(i).setLastName(last);
System.out.println("Current record is: " + records.get(i).getFirstName() + " " + records.get(i).getLastName());
break;
}
}
}
}
public static void changeCurrentRecordFirstName(LinkedList<AccountBank> records, AccountBank currRec)
{
if(currRec == null)
System.out.println("No current record exits!");
else if(records.size() == 0)
System.out.println("No records exist!!");
else
{
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(currRec.getFirstName()) && records.get(i).getLastName().equalsIgnoreCase(currRec.getLastName()))
{
System.out.print("Enter new first name: ");
String first = keyboard.next();
records.get(i).setFirstName(first);
System.out.println("Current record is: " + records.get(i).getFirstName() + " " + records.get(i).getLastName());
break;
}
}
}
}
public static void removeCurrentRecord(LinkedList<AccountBank> records, AccountBank currRec)
{
if(currRec == null)
System.out.println("No current record exits!");
else if(records.size() == 0)
System.out.println("No records exist!!");
else
{
for(int i = 0; i < records.size(); i++)
{
if(records.get(i).getFirstName().equalsIgnoreCase(currRec.getFirstName()) && records.get(i).getLastName().equalsIgnoreCase(currRec.getLastName()))
{
AccountBank deleted = records.remove(i);
System.out.println("Deleted: " + deleted.getFirstName() + " " + deleted.getLastName());
currRec = null;
break;
}
}
}
}
public static void printCurrentRecord(AccountBank currRec)
{
System.out.println("Current record is: " + currRec.getFirstName() + " " + currRec.getLastName() + " " + String.format("$%.2f", currRec.getBalance()));
}
public static void insertNewRecord(LinkedList<AccountBank> records)
{
System.out.print(" Enter first name: ");
String first = keyboard.next();
System.out.print("Enter last name: ");
String last = keyboard.next();
System.out.print("Enter the balance amount: ");
double bal = keyboard.nextDouble();
AccountBank account = new AccountBank(first, last, bal);
records.add(account);
}
public static void printAllRecords(LinkedList<AccountBank> records)
{
if(records.size() == 0)
System.out.println("No records exist!!");
else
{
System.out.println();
System.out.printf("%-15s%-15s%15s ", "First Name", "Last Name", "Current Balance");
System.out.println("-----------------------------------------------");
for(int i = 0; i < records.size(); i++)
{
System.out.println(records.get(i));
}
}
}
private static char getMenuChoice()
{
System.out.println();
System.out.println("a. Show all records");
System.out.println("r. Remove the current record");
System.out.println("f. Change the first name in the current record");
System.out.println("l. Change the last name in the current record");
System.out.println("n. Add a new record");
System.out.println("d. Add a deposit to the current record");
System.out.println("w. Make a withdrawal from the current record");
System.out.println("q. Quit");
System.out.println("s. Select a record from the record list to become the current record");
System.out.print("Enter a command from the list above (q to quit): ");
char choice = keyboard.next().charAt(0);
return choice;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.