The Java program below does not continue to ask for user inputs when i tell the
ID: 3859138 • Letter: T
Question
The Java program below does not continue to ask for user inputs when i tell the program that I have more customers. Can you please edit this program so that the program repeatedly accepts user inputs for several customers and call the user defined method to get the results until the user chooses not to. You can change it however you see fit as long as the program runs correctly and keeps asking for inputs. I use jGrasp for this program.
import java.io.*;
import java.util.Scanner;
class Firm
{
//create scanner object to represent keyboard
Scanner console = new Scanner(System.in);
public static double serviceTaxCalc(double hourlyRate, int consultingTime, double income)
{
if(income <= 25000)
{
consultingTime = (consultingTime > 30 ? consultingTime-30 : 0);
return hourlyRate/60 * .4 * consultingTime;
}
else
{
consultingTime = (consultingTime > 20 ? consultingTime-20 : 0);
return hourlyRate/60 * .7 * consultingTime;
}
}
public static void main(String[] args)
{
//declare variables
double hourlyRate, income;
int consultingTime;
boolean haveMoreCustomers = true;
Scanner console = new Scanner(System.in);
//User information
while(haveMoreCustomers)
{
System.out.print("Enter hourly rate: ");
hourlyRate = console.nextDouble();
System.out.print("Enter consulting time in minutes: ");
consultingTime = console.nextInt();
System.out.print("Enter income of the customer: ");
income = console.nextDouble();
System.out.println("The service to be paid is: "+serviceTaxCalc(hourlyRate, consultingTime, income));
System.out.print("Do you have more customers (yes/no): ");
haveMoreCustomers = console.nextBoolean();
}
}//end main
}//end class
Explanation / Answer
Firm.java
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
class Firm {
// create scanner object to represent keyboard
Scanner console = new Scanner(System.in);
public static double serviceTaxCalc(double hourlyRate, int consultingTime,
double income) {
if (income <= 25000) {
consultingTime = (consultingTime > 30 ? consultingTime - 30 : 0);
return hourlyRate / 60 * .4 * consultingTime;
} else {
consultingTime = (consultingTime > 20 ? consultingTime - 20 : 0);
return hourlyRate / 60 * .7 * consultingTime;
}
}
public static void main(String[] args) {
// declare variables
double hourlyRate, income;
int consultingTime;
char ch;
boolean haveMoreCustomers = true;
Scanner console = new Scanner(System.in);
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat("#.##");
//The Program continues to execute until the user enters 'Y' or 'y'
while (true) {
System.out.print("Enter hourly rate: ");
hourlyRate = console.nextDouble();
System.out.print("Enter consulting time in minutes: ");
consultingTime = console.nextInt();
System.out.print("Enter income of the customer: ");
income = console.nextDouble();
System.out.println("The service to be paid is: "+ df.format(serviceTaxCalc(hourlyRate, consultingTime, income)));
System.out.print("Do you have more customers (Y/N): ");
ch = console.next(".").charAt(0);
if(ch=='Y'||ch=='y')
continue;
else
{
System.out.println(":: Program Exit ::");
break;
}
}
}// end main
}// end class
_________________
Output:
Enter hourly rate: 12
Enter consulting time in minutes: 45
Enter income of the customer: 50000
The service to be paid is: 3.5
Do you have more customers (Y/N): Y
Enter hourly rate: 10
Enter consulting time in minutes: 35
Enter income of the customer: 40000
The service to be paid is: 1.75
Do you have more customers (Y/N): N
:: Program Exit ::
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.