Question: The ATM class is the driver class and is the class that needs to be modified, the other classes are posted as references in case

The ATM class is the driver class and is the class that needs to be modified, the other classes are posted as references in case they are needed. The method in the ATM class getUserInput needs to be updated so that when a user enters their account they are able to perform three transaction at the main menu: check their balance, withdraw, or deposit, and are allowed to exit after a transaction or go back to the main menu and exit from there if they want.

ATM Class

import java.util.Scanner; import java.io.File; import java.io.IOException;

// Driver class for Bank project

public class ATM { public static void main (String[] args) { try {

// Read data from a file into a Bank. // Each line of the file has info for one account. Bank myBank = readFromFile("BankAccounts.txt");

// Print all the data stored in the bank. System.out.println (myBank); getUserInput(myBank);

} // end try catch (IOException ioe) { System.out.println("IOException in main: " + ioe.getMessage()); ioe.printStackTrace(); } // end catch catch (Exception e) { System.out.println("Exception in main: " + e.getMessage()); e.printStackTrace(); } // end catch } // end main

/** * readFromFile: reads in a file and adds account objects created from the file into a bank * Precondition: A file must be made and the path to get the file must be stated. * Postcondition: The information in the bank object is returned * * @params String fileName is the name of the file * @return returns the information in the bank object */ public static Bank readFromFile (String fileName) throws IOException { // Creata a bank. Bank myBank = new Bank("Bank");

// Open a file for reading. Scanner inputSource = new Scanner (new File(fileName)); // while there are more tokens to read from the input source... while (inputSource.hasNextLine()) { // Read one line of input from the file into an Account object Account acct = InputManager.readOneAccountFrom (inputSource); // Store the account info in the bank. myBank.addAccount(acct); } // end while return myBank; } // end readFromFile /** * getUserInput: The user is allowed to input their id number to enter their account * Precondition: An account must be made and stored in the bank * Postcondition: The account of the user is either returned or it does not exist prompting * the user if they want to try again. * * @params myBank is the bank object that holds all the accounts * @return none */ public static void getUserInput(Bank myBank){ boolean continueFlag = true; Scanner scan = new Scanner(System.in); while(continueFlag) { System.out.println(" Please Enter your ID:"); String userID = scan.next(); Account acct = myBank.search(userID); if (null == acct) { System.out.println("Sorry the ID entered: "+ userID +" does not exist"); System.out.println("Do you want to try again? (Y/N) : "); if(scan.next().trim().toLowerCase().equals("y")){ continueFlag = true; }else{ System.out.println("You have choosen to exit."); System.out.println("Thank You for using AU Bank"); continueFlag = false; } }else{ System.out.println("Welcome: "+ acct.getName()); continueFlag = false; } } } // end getUSerInput } // end ATM

InputManager Class

import java.util.Scanner;

public class InputManager { // Method: readOneAccountFrom // Precondition: inputSource is a Scanner object, already set up // to read from a text file or standard input source (keyboard). // Postcondition: returns an Account with the data read for its attributes. // Assumption: Account data will be in the format of: name,id,balance public static Account readOneAccountFrom (Scanner inputSource) { // Read one line of account data into oneLine System.out.println ("Reading: name,id,balance"); String oneLine = inputSource.nextLine(); // Parse line of account data, separated by commas. Scanner lineTokenizer = new Scanner (oneLine); lineTokenizer.useDelimiter (","); // Get account data (i.e. name, accountNum and balance) from oneLine String name = lineTokenizer.next(); String accountNum = lineTokenizer.next(); Money balance = new Money(lineTokenizer.nextLong()); // Create and return an Account object with the data read for one account. Account oneAccount = new Account (name, accountNum, balance); System.out.println ("Account read: " + oneAccount); return oneAccount; } // end readOneAccountFrom } // end InputManager

Bank Class

public class Bank implements BankInterface { private String nameOfBank; private int numOfAccounts; private Account[] accounts ; /** * Constructor: initializes all attributes * based on the given name of the bank * * @param Name the name of the bank */ public Bank(String nameOfBank) { this.nameOfBank = nameOfBank; this.accounts = new Account[5]; this.numOfAccounts = 0; }

/** * getBank * * @return the name of the bank */ public String getBank() { return this.nameOfBank; } /** * getNumOfAccounts * * @return the number of accounts in the array */ public int getNumOfAccounts() { return numOfAccounts; } /** * addAccount: adds an account object to the array of accounts * * Precondition: an array of account objects is created and an account object is created and * added to the array. * Postcondition: the account object is added to the array * * @param an account object to be added to the array * @return the info on the account that was added to the array */ public void addAccount(Account theAcct) { this.accounts[numOfAccounts] = theAcct; numOfAccounts++; } /** * search: searches for the id of an account * * Preconditon: there has to be an account in the array * Postcondition: the correct account is returned or null if account was not found * * @param the id number of the account that needs to be searched * @return the account that corresponds wth the id */ public Account search(String theID) { if (numOfAccounts == 0) { return null; } else { for (int i = 0; i < numOfAccounts; i++) { if (accounts[i].getID().equals(theID)) { return accounts[i]; } } } return null; } /** * deposit: the account is searched by using its id and then is deposited a money value * * Precondition: there has to be an account in the array and a money value must be created * Postcondition: the correct account is given the deposit * * @param the id number of the account that needs to be searched and the amount to be deposited * @return the account with that corresponds with the id is given its deposit amount */ public void depositTo(String theID, Money amount) { Account acct = search(theID); if (acct == null) { //Does nothing }else { acct.deposit(amount); } } /** * withdraw: the account is searched by using its id and then money is withdrawed from the account * * Precondition: there has to be an account in the array and a money value must be created. * Postcondition: the correct account is returned and the money is withdrawn from the account * * @param the id number of the account that needs to be searched and the amount to be withdrawn * @return the account with that corresponds with the id reurns with its new balance */ public boolean withdrawFrom(String theID, Money amount) { Account acct = search(theID); if (acct == null) { return false; } else { return acct.withdraw(amount); } } /** * toString: returns the string representation of this bank object * * Precondition: this bank object is valid * * @return a String representation of this object. */ public String toString() { String info = ""; info = this.nameOfBank + " contains " + this.numOfAccounts + " account(s)"; return info; }

}

BankInterface

public interface BankInterface { /** * getBank * * @return the name of the bank */ public String getBank(); /** * getNumOfAccounts * * @return the number of accounts in the array */ public int getNumOfAccounts(); /** * addAccount: adds an account object to the array of accounts * * Precondition: an array of account objects is created and an account object is created and * added to the array. * Postcondition: the account object is added to the array * * @param an account object to be added to the array * @return the info on the account that was added to the array */ public void addAccount(Account theAcct); /** * search: searches for the id of an account * * Preconditon: there has to be an account in the array * Postcondition: the correct account is returned * * @param the id number of the account that needs to be searched * @return the account that corresponds wth the id */ public Account search(String theID); /** * deposit: the account is searched by using its id and then is deposited a money value * * Precondition: there has to be an account in the array and a money value must be created * Postcondition: the correct account is given the deposit * * @param the id number of the account that needs to be searched and the amount to be deposited * @return the account with that corresponds with the id is given its deposit amount */ public void depositTo(String theID, Money amount); /** * withdraw: the account is searched by using its id and then money is withdrawed from the account * * Precondition: there has to be an account in the array and a money value must be created. * Postcondition: the correct account is returned and the money is withdrawn from the account * * @param the id number of the account that needs to be searched and the amount to be withdrawn * @return the account with that corresponds with the id reurns with its new balance */ public boolean withdrawFrom(String theID, Money amount); /** * toString: returns the string representation of this bank object * * Precondition: this bank object is valid * * @return a String representation of this object. */ public String toString(); }

Account Class

public class Account implements Comparable { // attributes = state variables private String name; private String id; private Money balance; /** * Constructor: initializes all attributes * based on the given name, id, and balance * * @param Name the name of the person under the account * @param ID the id number of the account * @param Balance the amount of money in the account */ public Account(String Name, String ID, Money Balance){ this.name = Name; this.id = ID; this.balance = Balance; } /** * setName * * @param theName Sets the name of the person under the account */ public void setName(String theName) { this.name = theName; } /** * getName * * @return the name of the person under the account */ public String getName() { return this.name; } /** * setID * * @param theID Sets the ID of the account */ public void setID(String theID) { this.id = theID; } /** * getID * * @return the ID of the account */ public String getID() { return this.id; } /** * setBalance * * @param theBalance Sets the balance of the account */ public void setBalance(Money theBalance) { this.balance = theBalance; } /** * getBalance * * @return the balance in the account */ public Money getBalance() { return this.balance; } /** * deposit: adds two money values to the balance of the account * * Precondition: two Money amounts are created and valid * Postcondition: the amount is added to the balance of the money amount given as parameters * * @param a Money in the original balance, the deposited amount of money to be added to the balance * @retrun Money, the new balance after the deposit */ public void deposit (Money deposited) { this.balance = this.balance.add(deposited); } /** * withdraw: subtracts two money values to the balance of the account * * Precondition: two Money amounts are created and valid * Postcondition: the amount is subtracted from the balance of the money amount given as parameters * * @param a Money in the original balance, the withdrawn amount of money to be subtracted from the balance * @retrun Money, the new balance after the withdraw */ public boolean withdraw (Money withdraw) { //this.balance = this.balance.subtract(withdraw); if (this.balance.subtract(withdraw).compareTo(withdraw) < 0) { return false; } else { return false; } } /** * transfer: subtracts two money values from a balance and then adds it to another balance * * Precondition: three Money amounts are created and valid * Postcondition: the amount is subtracted from a balance and added to another balance * * @param a Money in the original balance, the transfer amount of money to be subtracted from one balance, then the transfer amount is added to another balance * @return the new balance in both the first account balance and the second account balance */ public boolean transfer (Account otherAcct, Money Amount) { this.balance.subtract(Amount); otherAcct.deposit(Amount); return true; } /** * toString: return the String representation of this Account object * Precondttion: this account object is valid * * @return a String representation of this object */ public String toString() { return ("Name: "+ this.name+", ID: "+ this.id + ", Balance: " + this.balance); }

/** * equals: compare the status of two account objects * * @param account a Account object * @return true if calling object is in the same state as the Account object received as a parameter, and false otherwise */ public boolean equals (Account account) { if ( this.name.equals(account.name) && this.id.equals(account.id) && this.balance.equals(account.balance)) return true; else return false; } /** * compareTo * * @param * @return the id that is being compared if the id is equal */ public int compareTo(Account acct) { if (acct instanceof Account) { Account acc = (Account)acct; return this.id.compareTo(acct.id); } else return -1000; } }

AccountInterface Comparable

public interface Comparable { /** * setName * * @param theName Sets the name of the person under the account */ public void setName(String theName); /** * getName * * @return the name of the person under the account */ public String getName(); /** * setID * * @param theID Sets the ID of the account */ public void setID(String theID); /** * getID * * @return the ID of the account */ public String getID(); /** * setBalance * * @param theBalance Sets the balance of the account */ public void setBalance(Money theBalance); /** * getBalance * * @return the balance in the account */ public Money getBalance(); /** * deposit: adds two money values to the balance of the account * * Precondition: two Money amounts are created and valid * Postcondition: the amount is added to the balance of the money amount given as parameters * * @param a Money in the original balance, the deposited amount of money to be added to the balance * @retrun Money, the new balance after the deposit */ public void deposit (Money deposited); /** * withdraw: subtracts two money values to the balance of the account * * Precondition: two Money amounts are created and valid * Postcondition: the amount is subtracted from the balance of the money amount given as parameters * * @param a Money in the original balance, the withdrawn amount of money to be subtracted from the balance * @retrun Money, the new balance after the withdraw */ public boolean withdraw (Money withdraw); /** * transfer: subtracts two money values from a balance and then adds it to another balance * * Precondition: three Money amounts are created and valid * Postcondition: the amount is subtracted from a balance and added to another balance * * @param a Money in the original balance, the transfer amount of money to be subtracted from one balance, then the transfer amount is added to another balance * @return the new balance in both the first account balance and the second account balance */ public boolean transfer (Account otherAcct, Money Amount); /** * toString: return the String representation of this Account object * Precondttion: this account object is valid * * @return a String representation of this object */ public String toString();

/** * equals: compare the status of two account objects * * @param account a Account object * @return true if calling object is in the same state as the Account object received as a parameter, and false otherwise */ public boolean equals (Account account); /** * An example of a method header - replace this comment with your own * * @param y a sample parameter for a method * @return the result produced by sampleMethod */ public int compareTo(Account acct); }

Money Class

public class Money { // attributes = state variables private long totalCents;

/** * Constructor: initializes all attributes (i.e. totalCents) * based on the given dollars and cents. * * @param theDollars the number of dollars * @param theCents the number of cents */ public Money(int theDollars, int theCents) { this.totalCents = theDollars * 100L + theCents; }

/** * Constructor: initializes all attributes (i.e. totalCents) * based on the given total cents. * * @param theCents the total number of cents */ public Money(long theCents) { this.totalCents = theCents; } /** * getDollars: * * @return the number of dollars */ public int getDollars() { return (int) this.totalCents / 100; } /** * getCents: * * @return the number of cents (between 0 and 99, inclusive) */ public int getCents() { return (int) this.totalCents % 100; } /** * add: adds two money values * * Precondition: two Money amounts are created and valid * Postcondition: the amount in this Money object is added to the Money amount given as parameter; the result is returned. * Neither the calling object nor the parameter are changed. * * @param a Money, the Money amount to add to the calling object (this) * @return Money, the sum */ public Money add (Money theMoney) { return new Money (this.totalCents + theMoney.totalCents); } /** * subtract: subtracts two money values * * Precondition: two Money amounts are created and valid * Postcondition: the amount in this Money object is subtracted to the Money amount given as parameter; the result is returned. * Neither the calling object nor the parameter are changed. * * @param a Money, the Money amount to add to the calling object (this) * @return Money, the difference */ public Money subtract (Money theMoney) { return new Money (this.totalCents - theMoney.totalCents); } /** * toString: return String representation of this Money object * Precondition: this Money object is valid * * @return a String representation of this object */ public String toString() { String result = "$" + this.getDollars() + "."; if (this.getCents() < 10) { result += "0"; } result += this.getCents(); return result; } /** * equals: compare the status of two money objects. * * @param other a Money object * @return true if calling object (this) is in the same state as the Money object received as a parameter, and false otherwise. */ public boolean equals (Money other) { return (this.totalCents == other.totalCents); } /** * compareTo: compares two money objects * * one that invokes the method and the one that received as parameter and returns */ public int compareTo (Money other) { if (this.totalCents == other.totalCents) { return 0; } else { if (this.totalCents < other.totalCents) { return -1; } else return 1; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!