Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Simple Employee Payment Program

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    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.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Simple Employee Payment Program

    How exactly is your program misbehaving?

  3. #3
    Junior Member CosmicCode's Avatar
    Join Date
    Nov 2012
    Location
    KSA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 !

    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);
            }
        }
    }

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Employee Payment Program

    Here was the error i was getting:

    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:

    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:

    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)

  5. #5
    Junior Member CosmicCode's Avatar
    Join Date
    Nov 2012
    Location
    KSA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 !

Similar Threads

  1. trying to write program to display details of employee
    By prathipati in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 13th, 2012, 12:20 AM
  2. Monthly Mortage Payment Calculator
    By lockdown in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2011, 11:54 PM
  3. Program to read & write the Employee Records
    By arvindk.vij in forum Member Introductions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM
  4. Program to read & write the Employee Records
    By arvindk.vij in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM
  5. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM