Payroll-Control02: Payroll With a Loop - Exercise

Contact Us or call 1-877-932-8228
Payroll-Control02: Payroll With a Loop - Exercise

Payroll-Control02: Payroll With a Loop

Duration: 20 to 40 minutes.
  1. Revise your payroll program to use only one Employee variable.
  2. Use a loop to repeatedly create a new instance to store in it, populate it from the keyboard, and display it on the screen (you can ask after each one if the user wants to enter another, or just use a loop that has a fixed number of iterations).
  3. Also, change the logic for reading the pay rate and department values from the keyboard to use do . . . while loops that will continue to loop as long as the user enters invalid values (note that you will need to separate declaring the variables from populating them - declare each of them before their loop starts, in order for the variable to be available to the test at the end of the loop).


import employees.*;
import util.*;

public class Payroll {	
	public static void main(String[] args) {
		Employee e = null;
		String fName, lName;
		int dept;
		double salary;

		for (int i = 0; i < 3; i++) {
			fName = KeyboardReader.getPromptedString("Enter first name: ");
			lName = KeyboardReader.getPromptedString("Enter last name: ");
			>do {
				dept = KeyboardReader.getPromptedInt("Enter department: ");
				if (dept <= 0) System.out.println("Department must be >= 0");
			} while (dept <= 0);
			do {
				salary = KeyboardReader.getPromptedDouble("Enter pay rate: ");
				if (salary < 0.0) System.out.println("Pay rate must be >= 0");
			} while (salary < 0.0);
			e = new Employee(fName, lName, dept, salary);