Please be patient with me. I am a new user. I am trying to make a salary calculator that if the applicant is under 18, they cannot receive overtime, because in the state I am in the maximum allowed for minors is 18 hours. I have it set where you cannot earn overtime until you reach 40 hours. Is there anything I can do for this?
import java.util.Scanner;
public class SalaryV2
{
public static void main(String[] args)
{
//Declare and initialize variables
double totalSalary;
boolean isOvertime;
Scanner in = new Scanner(System.in);
//Input
System.out.print("Please enter your name (first last): ");
String firstName = in.next();
String lastName = in.nextLine();
System.out.print("Please enter your age: ");
String customerAge = in.nextLine();
double age = Double.parseDouble(customerAge);
System.out.print("\nWhat is your hourly rate of pay: ");
String rateOfPay = in.nextLine();
double payRate = Double.parseDouble(rateOfPay);
System.out.print("\nHow many hours did you work: ");
String totalHoursWorked = in.nextLine();
double totalHours = Double.parseDouble(totalHoursWorked);
System.out.println();
//Processing
if(17 > age)
{
isOvertime = false;
totalSalary = totalHours * payRate;
}
else if(totalHours > 40)
{
if(totalHours > 40)
{
isOvertime = true;
totalSalary = 40 * payRate (totalHours - 40) * payRate * 1.5;
}
else
{
isOvertime = false;
totalSalary = totalHours * payRate;
}
}
//Output
System.out.print("Employee Name: " lastName ", " firstName\n);
System.out.print("Hours worked: " totalHours);
System.out.println("\t\tOvertime: " isOvertime );
System.out.println("Salary: " totalSalary);
} }
CodePudding user response:
I didn't understand very well your answer, but here is something you can improve.
In the first if, you are not considering even 17 years old, you should put if(17 >= age)
or if(18 > age)
.
Then, in the else if you put the same condition as the if below, so what you should do is:
else if(totalHours > 40)
{
isOvertime = true;
totalSalary = 40 * payRate (totalHours - 40) * payRate * 1.5;
}
else
{
isOvertime = false;
totalSalary = totalHours * payRate;
}
And to make your code more efficient, you can put two conditions in one if.
if(18 > age && totalHours < 40) //here i changed the ">" to "<"
{
isOvertime = false;
totalSalary = totalHours * payRate;
}
else
{
isOvertime = true;
totalSalary = 40 * payRate (totalHours - 40) * payRate * 1.5;
}
I didn't check if the code actually works, but it should be fine
CodePudding user response:
Keep it simple, no need for else-if-if,
import java.util.Scanner;
public class SalaryV2{
public static void main(String[] args){
//Declare and initialize variables
double totalSalary= 0;
boolean isOvertime = false;
Scanner in = new Scanner(System.in);
//Input
System.out.print("Please enter your name (first last): ");
String firstName = in.next();
String lastName = in.nextLine();
System.out.print("Please enter your age: ");
String customerAge = in.nextLine();
double age = Double.parseDouble(customerAge);
System.out.print("\nWhat is your hourly rate of pay: ");
String rateOfPay = in.nextLine();
double payRate = Double.parseDouble(rateOfPay);
System.out.print("\nHow many hours did you work: ");
String totalHoursWorked = in.nextLine();
double totalHours = Double.parseDouble(totalHoursWorked);
System.out.println();
//Processing
if(age >= 18 && totalHours > 40) {
isOvertime = true;
totalSalary = 40 * payRate (totalHours - 40) * payRate * 1.5;
}else{
isOvertime = false;
totalSalary = totalHours * payRate;
}
//Output
System.out.print("Employee Name: " lastName ", " firstName '\n' );
System.out.print("Hours worked: " totalHours '\n');
System.out.println("Overtime: " isOvertime );
System.out.println("Salary: " totalSalary);
}}
With the following input STDIN firstname lastname 19 10 42
Output :
$javac SalaryV2.java
$java -Xmx128M -Xms16M SalaryV2
Please enter your name (first last): Please enter your age:
What is your hourly rate of pay:
How many hours did you work:
Employee Name: lastname, firstname
Hours worked: 42.0
Overtime: true
Salary: 430.0
If under 18 STDIN
firstname lastname 17 10 42
Output :
$javac SalaryV2.java
$java -Xmx128M -Xms16M SalaryV2
Please enter your name (first last): Please enter your age:
What is your hourly rate of pay:
How many hours did you work:
Employee Name: lastname, firstname
Hours worked: 42.0
Overtime: false
Salary: 420.0
Checked by executing the above code.
CodePudding user response:
If you are a minor you cannot get additional pay for any overtime you work (strange as that may seem).
If you have less than 40 hours you cannot get pay for overtime (everybody knows the weird consequences of that...).
So you can only get paid for overtime if you are over 18 and have more than 40 hours.
if((18 <= age) && (totalHours > 40))
{
isOvertime = false;
totalSalary = totalHours * payRate;
} else
{
isOvertime = true;
totalSalary = 40 * payRate (totalHours - 40) * payRate * 1.5;
}
I.e. I do not see the need for nested if
s.