0

I need to write a program for my CIS class, I feel I have the majority set up. Being 95%, To sum it all up simply. I have to write a program that prompts the user to input their name, pay rate, hours worked. It then takes the data, calculates the gross pay, then subtracts the tax, then prints it all to the screen. The program should allow multiple users to add data for however number of employee's, which forces me to believe I need to set a value they must enter to end the program. Problem is I am regrettably stumped on how to code it so when the value is entered, the program ends. I'm almost certain a while loop is needed, but any sort of feedback would be much appreciated.

package program1;

import java.util.*;


public class Program1 {

static Scanner console = new Scanner (System.in);

public static void main(String[] args) {

    String firstName, lastName ;
    double payRate;
    int hoursWorked;
    double netPay;
    double grossPay;
    String formatNet;


    System.out.println("Please enter the employee's name. (Enter a -1 when finished): ")          ;
    firstName = console.nextLine();
    lastName  = console.nextLine();

    System.out.println("Please enter the employee's pay rate. ");
    payRate = console.nextDouble();

    System.out.println("Please enter the employee's hours worked. ");
    hoursWorked = console.nextInt();

    if(hoursWorked > 40)
    {
        grossPay = payRate * hoursWorked * 1.5;
    }
    else
    {
        grossPay = payRate * hoursWorked;
    }

    netPay = grossPay - (grossPay * .15);
    formatNet = String.format("%.2f", netPay);
    System.out.println(firstName +" "+ lastName + "'s net pay is " + formatNet);

 }

}
lakshman
  • 2,641
  • 6
  • 37
  • 63
Leoinu
  • 91
  • 2
  • 4
  • 17
  • 2
    This indentation for the `;` is very ugly. – Maroun Jan 29 '14 at 11:30
  • Is it now? On the site it may seem awful. But on programs like NetBeans it seems organized and much more clean for me. I suppose its a habit of mine that I picked up from a previous professor who told me having a clean organized code is always a good habit to pick up. Your thoughts? – Leoinu Jan 29 '14 at 11:32
  • If I want to indent lines, I indent the variables, not the `;`. – Maroun Jan 29 '14 at 11:33
  • 1
    I see, that does make more sense now that I've done so myself. Thank you very much for the input! – Leoinu Jan 29 '14 at 11:38

3 Answers3

0

Yes, you need a loop. Try this code below.

Also note the presence of console.nextLine(); after
console.nextDouble(); and console.nextInt();.

public static void main(String[] args) {

    String firstName, lastName;
    double payRate;
    int hoursWorked;
    double netPay;
    double grossPay;
    String formatNet;

    while (true){
        System.out.println("Please enter the employee's name. (Enter a -1 when finished): ");
        firstName = console.nextLine();
        if ("-1".equals(firstName.trim())) break;
        lastName = console.nextLine();

        System.out.println("Please enter the employee's pay rate. ");
        payRate = console.nextDouble();
        console.nextLine();

        System.out.println("Please enter the employee's hours worked. ");
        hoursWorked = console.nextInt();
        console.nextLine();

        if (hoursWorked > 40) {
            grossPay = payRate * hoursWorked * 1.5;
        } else {
            grossPay = payRate * hoursWorked;
        }

        netPay = grossPay - (grossPay * .15);
        formatNet = String.format("%.2f", netPay);
        System.out.println(firstName + " " + lastName + "'s net pay is " + formatNet);
    }

}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Use do while loop and by using some condition you can terminate the loop like you can prompt user for "Do you want to continue(y/n)" and depending upon value you can iterate again.

EDIT

  do
    {
        //your code goes here
        System.out.println("Do you want to continue(y/n)?");
        isContinue = console.next();
    }while(isContinue = "Y" || isContinue = "y")

you can use break statement after checking firstname is -1 in if condition

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
  • I figured as much! Would I have to use a NOT operator during the while()? – Leoinu Jan 29 '14 at 11:49
  • It cannot prompt a user if they want to continue. Students must be very specific which is why when it prompts for the name it says "Enter a -1 when finished" else I'd would have done that in the first place. – Leoinu Jan 29 '14 at 11:54
  • you mean to say when firstname is -1 then stop? – eatSleepCode Jan 29 '14 at 11:58
  • break statement will help you in this case as it is used to `break` current running loop. – eatSleepCode Jan 29 '14 at 12:01
  • Could you give an example? The code is a general review of what we've learned in the previous course. Which was a beginners course in teach basics of Java. – Leoinu Jan 29 '14 at 12:11
  • isn't adding `if ("-1".equals(firstName.trim())) break;` working? – eatSleepCode Jan 30 '14 at 04:50
0

Here is a solution. I threw in some Object-Orientation, method extraction and resource management to always close the Scanner after use.

import java.util.Scanner;

public class Program1 {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        try {
            do {
                enterEmployee(console);
                System.out.println("Another employee? (q to quit)");
            } while (!"q".equals(console.nextLine()));
        } finally {
            console.close();
        }
    }

    private static void enterEmployee(Scanner console) {
        Employee employee = new Employee();
        System.out.println("Please enter the employee's first name: ");
        employee.setFirstName(console.nextLine());

        System.out.println("Please enter the employee's last name: ");
        employee.setLastName(console.nextLine());

        System.out.println("Please enter the employee's pay rate: ");
        employee.setPayRate(console.nextDouble());
        console.nextLine();

        System.out.println("Please enter the employee's hours worked: ");
        employee.setHoursWorked(console.nextInt());
        console.nextLine();

        System.out.println(employee + "'s net pay is " + String.format("%.2f", employee.getNetPay()));
    }

    public static class Employee {
        private String firstName;
        private String lastName;
        private double payRate;
        private int hoursWorked;
        private double netPay;
        private double grossPay;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public double getPayRate() {
            return payRate;
        }

        public void setPayRate(double payRate) {
            this.payRate = payRate;
        }

        public int getHoursWorked() {
            return hoursWorked;
        }

        public void setHoursWorked(int hoursWorked) {
            this.hoursWorked = hoursWorked;
            if (hoursWorked > 40) {
                grossPay = payRate * hoursWorked * 1.5;
            } else {
                grossPay = payRate * hoursWorked;
            }

            netPay = grossPay - (grossPay * .15);
        }

        public double getNetPay() {
            return netPay;
        }

        public double getGrossPay() {
            return grossPay;
        }

        @Override
        public String toString() {
            return firstName + " " + lastName;
        }
    }
}
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60