Simple Employee Payment Program
Hi all,
I am struggling with a small program which calculates an employee's payment due.
Some basic points:
* Min Wage as 6.21
* Hours over first 40 is payed at 1.5
* Program should output the payment due
As you can see below i am using if statements to try and do this.
I have looked through several times but cant work out where the problem is.
Code :
package latesttasks;
import java.util.*;
// Write a program that reads two numbers from the command line, the number of
// hours worked by an employee and their base pay rate. Then output the
// total pay due. Ensure the base pay rate entered is over minimum wage, if it
// doesnt meet minimum wage print a warning and ask again for correct wage.
// After 40 hour mark, pay rate is 150% or i.e. * 1.5.
public class Task2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double hoursWorked;
double minimumWage = 6.21;
double baseRate;
double overtimeRate;
double amountDue;
int hoursLimit = 40;
System.out.println("Please enter your basic hourly rate: ");
baseRate = scan.nextInt();
if (baseRate < minimumWage) {
System.out.println("Warning! rate doesn't meet minimum wage.");
System.out.println("");
}
System.out.println("How many hours have you worked this week: ");
hoursWorked = scan.nextInt();
System.out.println("");
if (hoursWorked <= 0) {
System.out.println("You are due no payment.");
} else if (hoursWorked > hoursLimit) {
overtimeRate = baseRate * 1.5;
amountDue = (baseRate * hoursLimit) +
((hoursWorked - hoursLimit) * overtimeRate);
System.out.println(amountDue);
}
else {
amountDue = (hoursWorked * baseRate);
System.out.println(amountDue);
}
}
}
Any advice and suggestions appreciated.
Re: Simple Employee Payment Program
How exactly is your program misbehaving?
Re: Simple Employee Payment Program
hello djl1990
can you please specify the problem you're having ?
is it a calculation problem or a code problem?
which part of the code do you have problem with ?
I just did a modification on a little part of the code, hope it's correct !
Code :
import java.util.*;
// Write a program that reads two numbers from the command line, the number of
// hours worked by an employee and their base pay rate. Then output the
// total pay due. Ensure the base pay rate entered is over minimum wage, if it
// doesnt meet minimum wage print a warning and ask again for correct wage.
// After 40 hour mark, pay rate is 150% or i.e. * 1.5.
public class Task2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double hoursWorked;
double minimumWage = 6.21;
double baseRate;
double overtimeRate;
double amountDue;
int hoursLimit = 40;
while(true){// to go over this, until the input is > minimumWage.
System.out.println("Please enter your basic hourly rate: ");//
baseRate = scan.nextInt();
if (baseRate < minimumWage) {
System.out.println("Warning! rate doesn't meet minimum wage.");
System.out.println("");
}
else // if baseRate is > minimumWage, break the loop.
break;
}
System.out.println("How many hours have you worked this week: ");
hoursWorked = scan.nextInt();
System.out.println("");
if (hoursWorked <= 0) {
System.out.println("You are due no payment.");
} else if (hoursWorked > hoursLimit) {
overtimeRate = baseRate * 1.5;
amountDue = (baseRate * hoursLimit) +
((hoursWorked - hoursLimit) * overtimeRate);
System.out.println(amountDue);
}
else {
amountDue = (hoursWorked * baseRate);
System.out.println(amountDue);
}
}
}
Re: Simple Employee Payment Program
Here was the error i was getting:
Quote:
run:
Please enter your basic hourly rate:
12.79
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at latesttasks.Task2.main(Task2.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
--- Update ---
Thanks for the advice CosmicCode. I added the while true parts and break statement so now have this:
Code :
package latesttasks;
import java.util.*;
// Write a program that reads two numbers from the command line, the number of
// hours worked by an employee and their base pay rate. Then output the
// total pay due. Ensure the base pay rate entered is over minimum wage, if it
// doesnt meet minimum wage print a warning and ask again for correct wage.
// After 40 hour mark, pay rate is 150% or i.e. * 1.5.
public class Task2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double hoursWorked;
double minimumWage = 6.21;
double baseRate;
double overtimeRate;
double amountDue;
int hoursLimit = 40;
while (true) {
System.out.println("Please enter your basic hourly rate: ");
baseRate = scan.nextInt();
if (baseRate < minimumWage) {
System.out.println("Warning! rate doesn't meet minimum wage.");
System.out.println("");
} else {
break;
}
}
System.out.println("How many hours have you worked this week: ");
hoursWorked = scan.nextInt();
System.out.println("");
if (hoursWorked <= 0) {
System.out.println("You are due no payment.");
} else if (hoursWorked > hoursLimit) {
overtimeRate = baseRate * 1.5;
amountDue = (baseRate * hoursLimit)
+ ((hoursWorked - hoursLimit) * overtimeRate);
System.out.println(amountDue);
} else {
amountDue = (hoursWorked * baseRate);
System.out.println(amountDue);
}
}
}
However i am getting the following:
Quote:
run:
Please enter your basic hourly rate:
10.49
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at latesttasks.Task2.main(Task2.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Re: Simple Employee Payment Program
Now you specified it, thank you for that! .
the problem is this code : baseRate = scan.nextInt();
it takes the next integer value, but when you input a double value an error occurs.
so just change it to > baseRate = scan.nextDouble();
and this should work !
hope this helped !