can someone help me do this step in this program ? Allow up to 3 total invalid a
ID: 3871466 • Letter: C
Question
can someone help me do this step in this program ?
Allow up to 3 total invalid attempts for the user to enter the correct number of hours worked. After the 3rd attempt, print out "You're Fired" and exit the program (or start program over)
import java.util.Scanner;
class CalculatePay {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String choice;
do {
System.out.println("What is your name?");
String name = scanner.next();
System.out.println("What type of employee are you?");
String employeeType = scanner.next();
System.out.println("How many hours did you work?");
int hoursWorked = scanner.nextInt();
//Keep reading the input until user enters a valid hours
while (hoursWorked < 0 | hoursWorked > 280) {
System.out.println("That's not possible, try again!");
hoursWorked = scanner.nextInt();
}
System.out.println("What is your pay rate?");
float payRate = scanner.nextFloat();
float totalPay = hoursWorked * payRate;
if (payRate >= 100) {
System.out.println("You must be a Java Programmer.");
}
switch (employeeType) {
case "p":
employeeType = "part- time employee";
break;
case "f":
employeeType = "full-time employee";
break;
case "t":
employeeType = "temporary employee";
break;
default:
employeeType = "unknown employee type";
break;
}
System.out.printf("Hi %s , you made $%.2f, this week as a %s !", name, totalPay, employeeType);
System.out.println("Do you want to start the program over again?");
choice = scanner.next().toLowerCase();
} while (choice.equals("yes"));
}
}
Explanation / Answer
Please find my implementation.
import java.util.Scanner;
class CalculatePay {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String choice;
do {
System.out.println("What is your name?");
String name = scanner.next();
System.out.println("What type of employee are you?");
String employeeType = scanner.next();
System.out.println("How many hours did you work?");
int hoursWorked = scanner.nextInt();
//Keep reading the input until user enters a valid hours
int c = 1;
while ((hoursWorked < 0 || hoursWorked > 280) && (c < 3)) {
System.out.println("That's not possible, try again!");
hoursWorked = scanner.nextInt();
c++;
}
if(hoursWorked < 0 || hoursWorked > 280)
System.out.println("You're Fired");
else{
System.out.println("What is your pay rate?");
float payRate = scanner.nextFloat();
float totalPay = hoursWorked * payRate;
if (payRate >= 100) {
System.out.println("You must be a Java Programmer.");
}
switch (employeeType) {
case "p":
employeeType = "part- time employee";
break;
case "f":
employeeType = "full-time employee";
break;
case "t":
employeeType = "temporary employee";
break;
default:
employeeType = "unknown employee type";
break;
}
System.out.printf("Hi %s , you made $%.2f, this week as a %s !", name, totalPay, employeeType);
}
System.out.println("Do you want to start the program over again?");
choice = scanner.next().toLowerCase();
} while (choice.equals("yes"));
scanner.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.