In: Computer Science
Previous info: Cost Is No Object is a car rental service that specializes in lending antique and luxury cars to clients on a short-term basis. A typical customer might rent a vintage convertible to transport out-of-town clients to a business meeting, or rent a luxury car to transport a wedding party. The service currently has three employees and ten vehicles that it rents.
Case Projects CH.4 Case: Cost Is No Object
In earlier chapters, you developed classes needed for Cost Is No Object—a car rental service that specializes in lending antique and luxury cars to clients on a short-term basis. The rental service produces computerized paychecks for its employees every week. Write a program that gets data for each of the following:
An employee ID number
A first name
A last name
A street address
A zip code
An hourly pay rate
Number of hours worked this week
An insurance plan code
Create an application that prompts the user for employee data; the application continues to accept data for new employees until the user enters 0 for an ID number to quit. While the ID number is not zero, prompt the user for a value for each field in turn. Any time the user enters an invalid value, continue to reprompt the user for the same data. Continue with the next data item only when the previous item is valid, as follows:
An employee ID must be between 100 and 999 inclusive.
A zip code must not be greater than 99999.
An hourly pay rate must be between $6.00 and $25.00 inclusive.
The number of hours worked in a week cannot be negative or more than 70.
An insurance plan code must be 1 or 2.
When all the needed data has been entered correctly for an employee, display a copy of all the data fields for the employee as well as the following:
Gross pay, calculated as hours worked times pay rate
Income tax, which is calculated as 15% of the gross pay if the gross pay is $400 or less; otherwise, it is 20% of the gross pay
An insurance premium, which is $60 for insurance plan code 1 and $100 for insurance plan code 2
Net pay, which is calculated as gross pay minus income tax, minus insurance premium; if the net pay is negative (the employee did not earn enough to cover the tax and insurance), then the net pay should be $0
TWO CLASSES SO TO KEEP CODE CLEAN AND IN OBJECT ORIENTED APPROACH
EmployeeMain.java class
import java.util.Scanner; public class EmployeeMain { private static double grossPay; private static double incomeTax; private static double insurancePre; private static double netPay; public static void main(String[] args ){ //variable initialization String firstName; String lastName ; String address; int zipCode; double payRate; int numberOfHours; int planCode; int employeeID; EmployeeX employeeX = new EmployeeX(); Scanner scanner = new Scanner(System.in); while ( scanner.hasNext() ){ employeeID = scanner.nextInt(); if( employeeID != 0 ){ //checks for valid ID while (!( employeeID <= 999 && employeeID >= 100 )){ System.out.println(" Enter valid ID "); employeeID = scanner.nextInt(); } employeeX.setEmployeeID(employeeID); scanner.nextLine(); //take the name and address System.out.println(" Enter the first Name "); firstName = scanner.nextLine(); employeeX.setFirstName(firstName); System.out.println(" Enter the last Name "); lastName = scanner.nextLine(); employeeX.setLastName(lastName); System.out.println(" Enter the address "); address = scanner.nextLine(); employeeX.setAddress(address); //takes the valid zipcode do{ System.out.println(" Enter the ZipCode "); zipCode = scanner.nextInt(); employeeX.setZipcode(zipCode); }while (!( zipCode < 99999 )); //take the valid pay rate do{ System.out.println(" Enter the Pay Rate "); payRate = scanner.nextDouble(); employeeX.setPayRate(payRate); }while (!( payRate <= 25.00 && payRate >= 6.00 )); //takes the valid number of hour of work do{ System.out.println(" Enter the number of Hours of work "); numberOfHours = scanner.nextInt(); employeeX.setNumberOfHours(numberOfHours); }while(!( numberOfHours >= 0 && numberOfHours <= 70 )); //takes the valid plan Code do{ System.out.println(" Enter the plan Code "); planCode = scanner.nextInt(); employeeX.setPlanCode(planCode); }while( !( planCode == 1 || planCode == 2 )); //calculates the grossPay grossPay = numberOfHours*payRate; //calculates the income Tax if( grossPay <= 400 ){ incomeTax = 0.15*grossPay; } else { incomeTax = 0.20*grossPay; } //checks for insurance Premium according to planCode if( employeeX.getPlanCode() == 1 ){ insurancePre = 100; } else { insurancePre = 60; } //calculates the netPay netPay = grossPay-incomeTax-insurancePre; if( netPay < 0 ){ netPay = 0; } print(); } else{ break; } } } //Prints the data for the user public static void print(){ EmployeeX employeeX = new EmployeeX(); System.out.println( " Name = " + employeeX.getFirstName() + " " + employeeX.getLastName() + "\n Address = " + employeeX.getAddress() + "\n ZipCode = " + employeeX.getZipcode() + "\n Employee ID Number = " + employeeX.getEmployeeID() + "\n Pay Rate = $" + employeeX.getPayRate() + "\n Number Of Hours Worked = "+ employeeX.getNumberOfHours() + "\n Plan Code = " + employeeX.getPlanCode() ); System.out.println( " GrossPay = " + grossPay + "\n Net Pay = " + netPay ); } }
EmployeeX.java class
public class EmployeeX { //variable initialization private static int employeeID; private static int zipcode; private static double payRate; private static int numberOfHours; private static int planCode; private static String firstName; private static String lastName; private static String address; //public getters and setters public static int getEmployeeID() { return employeeID; } public static void setEmployeeID(int employeeID) { EmployeeX.employeeID = employeeID; } public static int getZipcode() { return zipcode; } public static void setZipcode(int zipcode) { EmployeeX.zipcode = zipcode; } public static double getPayRate() { return payRate; } public static void setPayRate(double payRate) { EmployeeX.payRate = payRate; } public static int getNumberOfHours() { return numberOfHours; } public static void setNumberOfHours(int numberOfHours) { EmployeeX.numberOfHours = numberOfHours; } public static int getPlanCode() { return planCode; } public static void setPlanCode(int planCode) { EmployeeX.planCode = planCode; } public static String getFirstName() { return firstName; } public static void setFirstName(String firstName) { EmployeeX.firstName = firstName; } public static String getLastName() { return lastName; } public static void setLastName(String lastName) { EmployeeX.lastName = lastName; } public static String getAddress() { return address; } public static void setAddress(String address) { EmployeeX.address = address; } }