Question: Hello, I would like to change the following java code to fit this description of the account states and so that it answers the question

Hello,

I would like to change the following java code to fit this description of the account states and so that it answers the question below. I am also having trouble implementing attribute balance, accessor methods, and method add() properly, I am new to java and can't understand how to do them using the set and get. Please help me so that the code answers the following question. Thank you for your help in advance

Hello, I would like to change the following java code to fit this description of the account states and so that it answers the

package account;

import static java.lang.Character.toUpperCase;

import java.util.Scanner;

public class Account {

//Account attribute balance; initialized to 0.0

double balance = 0.0;

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

//Create object of Account

Account obj1 = new Account();

//amount used for altering balance

double amount = 0.0;

//Scanner Object

Scanner sc = new Scanner(System.in);

//Char choice; will allow user to repeat program

char choice = 'Y';

//Loop used for multiple iterations

while(choice != 'N')

{

//User Prompt for amount

System.out.println(" What is the amount that you would like to add to your balance? (It may be negative): ");

//Input Validation Loop

while(!sc.hasNextDouble())

{

System.out.println(" Error, please enter a valid double value: ");

sc.next();

}

//Capture user input

amount = sc.nextDouble();

//Call add() member function of Account with argument 'amount'

obj1.add(amount);

//User Prompt

System.out.println(" Would you like to continue?(Y/N): ");

//Accept user input

choice = sc.next().charAt(0);

//Capitalize user input

choice = toUpperCase(choice);

}

//Exit message

System.out.println(" Thank-you, this program will now end.");

}

//Member Function add() with parameter double amount

public void add(double amount){

//Update balance

balance = balance + amount;

//If statement block; Used for State transition and print balance

if (balance > 0)

{

System.out.println(" State: Sound Balance = " + balance);

}

else if (balance == 0)

{

System.out.println(" State: Empty Balance = " + balance);

}

else

{

System.out.println("State: Arrears Balance = " + balance);

}

}

}

Write the code for a class Account with attribute balance, accessor methods, and method add) Assume that Account has states Sound, Empty, and Arrears, and that these are implemented using the State design pattern. Write a complete set of unit tests for Account, including state- oriented tests

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!