Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to ask again, since I got an incomplete answer last time. Thanks in advance

ID: 671147 • Letter: N

Question

Need to ask again, since I got an incomplete answer last time. Thanks in advance:

Use the Account class below to simulate an ATM machine (ATM.java). Try not to put your code in main; each option should call a static method to perform the action.

Also, create a NewATM class that extends ATM.java.

Inside this code, your program will display an option for "0", named "see Account History". If selected, it displays the withdraw and deposit history for the associated account.

----------------------------------------

class Account
{
   private int number;
   private double balance;
   private double annualInterestRate;

   private java.util.Date dateCreated = new java.util.Date();

   public Account()
   {
      java.util.Date dateCreated = new java.util.Date();
   }

   public Account (int id, double bal, double Rate)
   {
      number=id;
      balance=bal;
      annualInterestRate=Rate;
   }

   public java.util.Date getdate()
   {
      return dateCreated;
   }

   public void setDate(java.util.Date dateCreated)
   {
      this.dateCreated = dateCreated;
   }

   public int getID()
   {
      return number;
   }

   public double getBalance()
   {
      return balance;
   }

   public double deposit (double deposit)
   {
      balance += deposit;
      return(deposit);
   }

   public double withdraw (double withdraw)
   {
      this.balance -= withdraw;
      return(withdraw);
   }

   public double getAnnualInterestRate()
   {
      return annualInterestRate;
   }

   public void setAnnualInterestRate(double annualInterestrate)
   {
      this.annualInterestRate = annualInterestRate;
   }

   public double getMonthlyInterestRate()
   {
      double monthlyInterest = (annualInterestRate / 1200) * balance;
      return monthlyInterest;
   }
}

----------------------------------------

Upon execution, the program will create 10 accounts with a random balance of more than 0, but smaller than 100. Each account will also have unique IDs, which range from 0 to 9. The program will then ask the user to enter their account ID, or the character 'z' to terminate program. If the ID entered does not have a match with an account, the program will say, "ID invalid, please enter another ID."

If an ID is entered that matches an account, the following main menu is displayed:

Main Menu for Account {ID number}

1. Check balance

2. Withdraw

3. Deposit

4. Exit

Enter a choice:

If they enter "1", this will be displayed:

The balance is {Account}.

(Display main menu)

If the user enters "2", this will be displayed:

Enter an amount to withdraw:

The balance is {new balance}

(Display main menu)

If the user enters "3", this will be displayed:

Enter an amount to deposit:

The balance is {new balance}

(Display main menu)

If the user enters "4", this will be displayed:

Enter an ID (or 'z' to quit):

Explanation / Answer

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;


public class NewATM extends ATM {
  
   public static void main(String []args){
       Scanner sc = new Scanner(System.in);
       NewATM atm = new NewATM();
       System.out.println("Enter your Account Id");
       int accId = sc.nextInt();
      
       //create initial accounts
       Account acc = new Account(1, 99, 5);
       atm.addAccount(acc);
       acc.withdraw(50);
       acc.deposit(5);
       acc.deposit(100);
       acc.withdraw(30);
      
       System.out.println("Main Menu for Account {"+accId+"}" +
               "0.Account History" +
               "1. Check balance" +
               "2. Withdraw" +
               "3. Deposit" +
               "4. Exit");
      
       int option = sc.nextInt();
       if(option == 0) {
           atm.showAccHistory(accId);
       }
       //write other cases as per your old programme
   }
  
  
   public NewATM() {
       super();
   }
   public void showAccHistory(int accountId) {
       Account account = this.accounts.get(accountId);
       if(account != null) {
           ArrayList<String> history = account.getHistory();
           System.out.println("Account Id : "+account.getID()
                      + " ACcount Created on: "+account.getdate());
           for(String str: history)
               System.out.println(str);
       }
   }
  
}

class ATM {
  
     protected HashMap<Integer,Account> accounts;
   
     public ATM() {
       accounts = new HashMap<Integer,Account>();
     }
   
   public double checkBalance(int accountId) {
       Account account = accounts.get(accountId);
       if(account != null)
           return account.getBalance();
       return -1;
   }
  
   public void withdraw(int accountId, double amount) {
       Account account = accounts.get(accountId);
       if(account != null)
           account.withdraw(amount);
      
   }
  
   public void deposit(int accountId, double amount) {
       Account account = accounts.get(accountId);
       if(account != null)
           account.deposit(amount);
      
   }
  
   public void addAccount(Account acc) {
       this.accounts.put(acc.getID(), acc);
   }
}


class Account
{
   private int number;
   private double balance;
   private double annualInterestRate;
   private ArrayList<String> history = new ArrayList<String> ();

   private java.util.Date dateCreated = new java.util.Date();

   public Account()
   {
      java.util.Date dateCreated = new java.util.Date();

   }

   public Account (int id, double bal, double Rate)
   {
      number=id;
      balance=bal;
      annualInterestRate=Rate;
   }

   public java.util.Date getdate()
   {
      return dateCreated;
   }

   public void setDate(java.util.Date dateCreated)
   {
      this.dateCreated = dateCreated;
   }

   public int getID()
   {
      return number;
   }

   public double getBalance()
   {
      return balance;
   }

   public double deposit (double deposit)
   {
      addToHistory(balance, deposit, true);
      balance += deposit;
      return(deposit);
   }

   public double withdraw (double withdraw)
   {
      addToHistory(balance, withdraw, true);
      this.balance -= withdraw;
   
      return(withdraw);
   }


   private void addToHistory(double origBalance, double amount, boolean isWithdraw) {
     
       String str = "Date: "+new Date()+" Transaction Type: "+
                     (isWithdraw ? "Withdraw" : "Deposit")
                   +" Transaction Amount: "+amount+" Original Balance: "+origBalance
                   +" New balance: ";
       if(isWithdraw)
           str += origBalance-amount;
       else
           str += origBalance+amount;
     
          this.history.add(str);
   }

   public double getAnnualInterestRate()
   {
      return annualInterestRate;
   }

   public void setAnnualInterestRate(double annualInterestrate)
   {
      this.annualInterestRate = annualInterestRate;
   }

   public double getMonthlyInterestRate()
   {
      double monthlyInterest = (annualInterestRate / 1200) * balance;
      return monthlyInterest;
   }

   public ArrayList<String> getHistory() {
       return history;
   }
  
   public void setHistory(ArrayList<String> history) {
       this.history = history;
   }

  

}

/

---------------------------ouput------------------------------

Main Menu for Account {1} 0.Account History1. Check balance2. Withdraw3. Deposit4. Exit
0
Account Id : 1 ACcount Created on: Tue Oct 06 13:24:58 IST 2015
Date: Tue Oct 06 13:24:58 IST 2015 Transaction Type: Withdraw Transaction Amount: 50.0 Original Balance: 99.0 New balance: 49.0
Date: Tue Oct 06 13:24:58 IST 2015 Transaction Type: Withdraw Transaction Amount: 5.0 Original Balance: 49.0 New balance: 44.0
Date: Tue Oct 06 13:24:58 IST 2015 Transaction Type: Withdraw Transaction Amount: 100.0 Original Balance: 54.0 New balance: -46.0
Date: Tue Oct 06 13:24:58 IST 2015 Transaction Type: Withdraw Transaction Amount: 30.0 Original Balance: 154.0 New balance: 124.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote