Here is what I have to do:
Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate.
I will post the code in 2 seperate comments as it will not all fit in here.
I need to use my exsisting code, just make modifcation to it. import java.util.Scanner;
public class Payroll2
{
public static void main (String args[])
{
double rate, total; // Local variables to hold the rate and total salary
int hours; // Local variable to hold the amount of hours worked
String name; // Local variable to hold the employee's name
Scanner input = new Scanner(System.in); // create a new instance of a Scanner object type
do // Begin main loop - continue until name equals stop
{
System.out.printf("Please enter the employee name (enter stop to quit): ");
name = input.next();
if (name.equalsIgnoreCase("stop")) // compare the value in the string to the literal string "stop"
System.out.printf("\nProgram termination requested.\n");
else
{
do // repeat these steps until the user enters a rate above 0...
{
System.out.printf("Please enter %s's hourly rate of pay: $", name); Didn't change up your program to terribly much, just added the extra Employee object at the end and slightly modified your original program to use it.
import java.util.Scanner;
public class Payroll2 {
public static void main (String args[]){
double rate; // Local variables to hold the rate and total salary
int hours; // Local variable to hold the amount of hours worked
String name; // Local variable to hold the employee's name
Scanner input = new Scanner(System.in); // create a new instance of a Scanner object type
do { // Begin main loop - continue until name equals stop
System.out.printf("Please enter the employee name (enter stop to quit): ");
name = input.next();
if (name.equalsIgnoreCase("stop"))
// compare the value in the string to the literal string "stop"
System.out.printf("\nProgram termination requested.\n");
else {
do { // repeat these steps until the user enters a rate above 0...
System.out.printf("Please enter %s's hourly rate of pay: $", name);
rate = input.nextDouble();
if (rate <= 0)
System.out.printf("\n *** Hourly rate of pay can only be entered in POSITIVE numbers! Try again!\n");
} while (rate <= 0);
do { // repeat these steps until the user enters more than 0 hours...
System.out.printf("Please enter %s's amount of hours worked: ", name);
hours = input.nextInt();
if (hours <= 0)
System.out.printf("\n *** Hours worked can only be entered in POSITIVE numbers! Try again!\n");
} while (hours <= 0);
Employee employee = new Employee(name, hours, rate);
System.out.printf("\nIf %s gets paid $%.2f per hour, for %d hours of work...........", employee.getName(), employee.getRate(), employee.getHours());
System.out.printf("\n%s's weekly gross pay will be $%.2f\n\n", employee.getName(), employee.getTotal());
}
} while (!(name.equalsIgnoreCase("stop")));
// User entered stop for the name, end loop.
}
}
public class Employee {
private String name;
private int hours;
private double rate;
/**
* Employee object for use with the payroll program.
* @param name Name of the Employee
* @param hours Hours the employee worked
* @param rate Rate of pay for the Employee
*/
public Employee(String name, int hours, double rate){
// constructor for an employee object
this.name = name;
this.hours = hours;
this.rate = rate;
}
public Employee(String name, double rate){
// overload of the constructor to set a default 40 hour workweek
this(name, 40, rate);
}
// next 3 methods are generic getter methods
public String getName(){
return name;
}
public int getHours(){
return hours;
}
public double getRate(){
return rate;
}
// getter method to calculate and return the total
public double getTotal(){
return (hours * rate);
}
} |