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

JAVA- Edit this code to use if/else statements rather than switch/case statement

ID: 3813909 • Letter: J

Question

JAVA- Edit this code to use if/else statements rather than switch/case statements. Also, instead of outputting "Insufficient Funds...", change it to output "Error: Can not withdraw (deposit amount)". An example of the input/output is below.

import java.util.Scanner;

public class Prog8 {
public static void main(String[] args) {
   Scanner reader = new Scanner(System.in);
       char type;
       double amount;
  
//Creating an Account object
Account myAccount = new Account();
  
//Reading transactions file
while(reader.hasNext()) {
//Reading type of transaction
type = reader.next().charAt(0);
  
//Calling appropriate functions
switch(type)
{
//Deposit
case 'd':
//Fetching amount
amount = reader.nextDouble();
//Calling deposit function
myAccount.deposit(amount);
//Printing result
System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber());
break;
  
//Withdraw
case 'w':
//Fetching amount
amount = reader.nextDouble();
//Calling withdraw function
myAccount.withdraw(amount);
//Printing result
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
break;
  
//Interest
case 'i':
//Calling addInterest function
double intAdded = myAccount.addInterest();
//Printing result
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
break;
  
//Get Balance
case 'p':
//Printing result
System.out.printf(" Current balance for account %d: $%.2f ", myAccount.getAccountNumber(), myAccount.getBalance());
break;
  
//Default case
default: break;
}
}
  
//Closing Scanner
reader.close();
System.out.println(" ");
}
}


import java.util.Random;

//Account class definition
class Account
{
//Private instance variable
private int accountNumber;
private double balance;
  
//Constructor that initializes variables
public Account()
{
//Random class object
Random rand = new Random();
//Generating a random 4 digit Account Number
accountNumber = rand.nextInt((9999 - 1000) + 1) + 1000;
  
//Initializing balance
balance = 0.0;
}
  
//Getter method for Account Number
public int getAccountNumber()
{
//Return Account Number
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(amt > balance)
System.out.println(" Insufficient Funds.... ");
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;
}
}

ile in dat d 100 w 00 cution Error: cannot withdraw $1.00 Current balance for account 358 5: $0.00 $100.00 deposited into account 3585 current balance for account 3585: 100.00 Error cannot withdraw $200.00 current balance for account 3585: $100.00

Explanation / Answer

Hi, I have changed the program accordingly.

Please let me know in case of any issue.

import java.util.Random;

//Account class definition

public class Account

{

   //Private instance variable

   private int accountNumber;

   private double balance;

   //Constructor that initializes variables

   public Account()

   {

       //Random class object

       Random rand = new Random();

       //Generating a random 4 digit Account Number

       accountNumber = rand.nextInt((9999 - 1000) + 1) + 1000;

       //Initializing balance

       balance = 0.0;

   }

   //Getter method for Account Number

   public int getAccountNumber()

   {

       //Return Account Number

       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(amt > balance)

           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;

   }

}

#########################################

import java.util.Scanner;

public class Prog8 {

   public static void main(String[] args) {

       Scanner reader = new Scanner(System.in);

       char type;

       double amount;

       //Creating an Account object

       Account myAccount = new Account();

       //Reading transactions file

       while(reader.hasNext()) {

           //Reading type of transaction

           type = reader.next().charAt(0);

           //Calling appropriate functions

           if(type == 'd'){

               //Fetching amount

               amount = reader.nextDouble();

               //Calling deposit function

               myAccount.deposit(amount);

               //Printing result

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

           }

           else if(type == 'w'){

               //Fetching amount

               amount = reader.nextDouble();

               //Calling withdraw function

               myAccount.withdraw(amount);

               //Printing result

               System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());

           }

           else if(type == 'i'){

               //Calling addInterest function

               double intAdded = myAccount.addInterest();

               //Printing result

               System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());

           }

           else if(type == 'p'){

               //Printing result

               System.out.printf(" Current balance for account %d: $%.2f ",

                       myAccount.getAccountNumber(), myAccount.getBalance());

           }

           //Closing Scanner

           reader.close();

           System.out.println(" ");

       }

   }

   }