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

JAVA- How do I u[date this code to add the new guidelines? Make the following mo

ID: 3820519 • Letter: J

Question

JAVA- How do I u[date this code to add the new guidelines?

Make the following modifications:

Create multiple instances of account. The number of instances will be indicated by the first integer in each input file. Use an array of Account objects.

Each command in the input file now has an index to an account. Perform theappropriate transaction on that account. (e.g. d 0 100 means deposit $100 into theaccount at index 0 in your array; w 1 50 means withdraw $50 from the account at index1 in your array)

Record each transaction (up to 5 per instance of account), and print out the account’shistory whenever the “p” command is read from the input file.

Print a message with the account ID every time a new instance of Account is created(this should happen at the very beginning of your program)

This is the code I need to edit. There are 2 JAVA files:*************

import java.util.Scanner;

public class Prog8 {
public static void main(String[] args) {
       Scanner reader = new Scanner(System.in);
       Account myAccount = new Account();
     
char type;
double amount;

while(reader.hasNext()) {
type = reader.next().charAt(0);

if(type == 'd'){
               amount = reader.nextDouble();
               myAccount.deposit(amount);

               System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber());
}

else if(type == 'w'){
               amount = reader.nextDouble();
               myAccount.withdraw(amount);
              
               System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
}

else if(type == 'i'){
               double intAdded = myAccount.addInterest();
               System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
}

else if(type == 'p'){
               System.out.printf(" Current balance for account %d: $%.2f",
myAccount.getAccountNumber(), myAccount.getBalance());
}
}
     
           System.out.println();
   }
}

******************


class Account{

private int accountNumber;
private double balance;
  
public Account(){

//Generating a random 4 digit Account Number
accountNumber = (int)(Math.random() * 9000) + 1000;
  
balance = 0.0;
}
  
//Getter method for Account Number
public int getAccountNumber(){
return accountNumber;
}
  
//Method that adds amount to balance
public void deposit(double amt){
//Updating new balance
balance = balance + amt;
}
  
//Method that withdraws amount from balance
public void withdraw(double amt){
//Validating amount
if((balance - amt) < 0)
System.out.println(" Error: Can not withdraw $" + amt);
else
//Updating new balance
balance = balance - amt;
}
  
//Method that adds interest to balance amount
public double addInterest(){
double actualBalance = balance;
  
//Adding interest to balance
balance = balance * 1.005;
  
//Returning amount of interest added
return (balance - actualBalance);
}
  
//Method that returns balance amount
public double getBalance(){
return balance;
}
}

Explanation / Answer

CODE:

import java.util.Scanner;

class Account{
   private int accountNumber;
   private double balance;
  
   public Account(){
   //Generating a random 4 digit Account Number
   accountNumber = (int)(Math.random() * 9000) + 1000;
  
   balance = 0.0;
   }
  
   //Getter method for Account Number
   public int getAccountNumber(){
   return accountNumber;
   }
  
   //Method that adds amount to balance
   public void deposit(double amt){
   //Updating new balance
   balance = balance + amt;
   }
  
   //Method that withdraws amount from balance
   public void withdraw(double amt){
   //Validating amount
   if((balance - amt) < 0)
   System.out.println(" Error: Can not withdraw $" + amt);
   else
   //Updating new balance
   balance = balance - amt;
   }
  
   //Method that adds interest to balance amount
   public double addInterest(){
   double actualBalance = balance;
  
   //Adding interest to balance
   balance = balance * 1.005;
  
   //Returning amount of interest added
   return (balance - actualBalance);
   }
  
   //Method that returns balance amount
   public double getBalance(){
   return balance;
   }
     
   }


public class Prog8 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//Account myAccount = new Account();
Account[] myAccount;
char type;
double amount;
int numAccnts = reader.nextInt();
myAccount = new Account[numAccnts];

while(reader.hasNext()) {
type = reader.next().charAt(0);
if(type == 'd'){
   int accNum = reader.nextInt();
   if (accNum >= numAccnts)
       System.out.println("Sorry you cannot create this account!!!");
   else{
       amount = reader.nextDouble();
   myAccount[accNum] = new Account();
   myAccount[accNum].deposit(amount);
   System.out.printf(" $%.2f deposited into account %d ", amount, myAccount[accNum].getAccountNumber());

   }
}
else if(type == 'w'){
   int accNum = reader.nextInt();
   if (accNum >= myAccount.length )
       System.out.println("Sorry this account does not exist!!");
   else{
amount = reader.nextDouble();
myAccount[accNum].withdraw(amount);
  
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount[accNum].getAccountNumber());
   }
}
else if(type == 'p'){
   for(int i=0;i<myAccount.length;i++){
System.out.printf(" Current balance for account %d: $%.2f",
myAccount[i].getAccountNumber(), myAccount[i].getBalance());
   }
}
}

System.out.println();
}
}

OUTPUT:

2
d 0 100

$100.00 deposited into account 8848
d 1 1000

$1000.00 deposited into account 9972
w 1 500

$500.00 withdrawn from account 9972
p

Current balance for account 8848: $100.00
Current balance for account 9972: $500.00