Modify payroll program so that it uses a class to store and retrive the employee's name, 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. Make sure the code is readable and well documented.
This is what I have done is this correct, as per the parameters above:
[CODE=java]import java.util.Array List;
import java.util.Scann er; // program uses class Scanner
class EmployeeData {
EmployeeData(St ring newName, float newHourlyRate, float newHoursWorked) {
name = newName; hourlyRate = newHourlyRate; hoursWorked = newHoursWorked;
}
public String getName() { return name; }
public float getWeeklyPay() { return hourlyRate * hoursWorked; }
private String name;
private float hourlyRate, hoursWorked;
} // end of EmployeeData class
public class payroll4a
{
private String EmployeeData; // employee information for this Payroll
private static int ArrayList;
private static int i;
// main method begins execution of java application
public static void main( String args[] )
{
ArrayList employees = new ArrayList (); // employee information to be stored for all employees
boolean stop = false; // This flag will control whether you exit the loop below
// Loop until user types "stop" as the employee name:
while (!stop)
{
// create scanner to obtain input from command window
Scanner input = new Scanner ( System.in );
System.out.prin tln(); // outputs a blank line
System.out.prin tln( "Please enter the employee name or 'stop' to end program: " ); // prompt
String empName = input.nextLine( ); // read employee name
if ( empName.compare To("stop") == 0) // Check whether user indicated to stop program
{
System.out.prin tln( "Program ended." );
stop = true;
}
else
{
// User did not indicate to stop, so continue reading info for this iteration:
EmployeeData employee;
float hourlyRate; // first number to multiply
float hoursWorked; // second number to multiply
float weeklyPay; // product of hourlyRate and hoursWorked
// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
{
System.out.prin tln( "Please enter hourly rate: $" ); // prompt
hourlyRate = input.nextFloat (); // read hourly rate from user
if (hourlyRate <= 0) // prompt until a positive value is entered
{
System.out.prin tln( "Hourly rate must be a positive value. " +
"Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
hourlyRate = input.nextFloat (); // read hourly rate again
}
System.out.prin tln( "Please enter hours worked: " ); // prompt
hoursWorked = input.nextFloat (); // read number of hours worked from user
while (hoursWorked <= 0) // prompt until a positive value is entered
{
System.out.prin tln( "Hours worked must be a positive value. " +
"Please enter the hours worked again: " ); // prompt for positive value for hours worked
hoursWorked = input.nextFloat (); // read hours worked again
}
employee = new EmployeeData(em pName, hourlyRate, hoursWorked) {
///weeklyPay = hourlyRate * hoursWorked; // multiply
};
System.out.prin t( employee.getNam e() ); // display employee name
System.out.prin tf( "'s weekly pay is: $%,.2f\n", employee.getWee klyPay() ); // display weekly pay
}
}
} // end method main
}} // end class Payroll4a[/CODE]
This is what I have done is this correct, as per the parameters above:
[CODE=java]import java.util.Array List;
import java.util.Scann er; // program uses class Scanner
class EmployeeData {
EmployeeData(St ring newName, float newHourlyRate, float newHoursWorked) {
name = newName; hourlyRate = newHourlyRate; hoursWorked = newHoursWorked;
}
public String getName() { return name; }
public float getWeeklyPay() { return hourlyRate * hoursWorked; }
private String name;
private float hourlyRate, hoursWorked;
} // end of EmployeeData class
public class payroll4a
{
private String EmployeeData; // employee information for this Payroll
private static int ArrayList;
private static int i;
// main method begins execution of java application
public static void main( String args[] )
{
ArrayList employees = new ArrayList (); // employee information to be stored for all employees
boolean stop = false; // This flag will control whether you exit the loop below
// Loop until user types "stop" as the employee name:
while (!stop)
{
// create scanner to obtain input from command window
Scanner input = new Scanner ( System.in );
System.out.prin tln(); // outputs a blank line
System.out.prin tln( "Please enter the employee name or 'stop' to end program: " ); // prompt
String empName = input.nextLine( ); // read employee name
if ( empName.compare To("stop") == 0) // Check whether user indicated to stop program
{
System.out.prin tln( "Program ended." );
stop = true;
}
else
{
// User did not indicate to stop, so continue reading info for this iteration:
EmployeeData employee;
float hourlyRate; // first number to multiply
float hoursWorked; // second number to multiply
float weeklyPay; // product of hourlyRate and hoursWorked
// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
{
System.out.prin tln( "Please enter hourly rate: $" ); // prompt
hourlyRate = input.nextFloat (); // read hourly rate from user
if (hourlyRate <= 0) // prompt until a positive value is entered
{
System.out.prin tln( "Hourly rate must be a positive value. " +
"Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
hourlyRate = input.nextFloat (); // read hourly rate again
}
System.out.prin tln( "Please enter hours worked: " ); // prompt
hoursWorked = input.nextFloat (); // read number of hours worked from user
while (hoursWorked <= 0) // prompt until a positive value is entered
{
System.out.prin tln( "Hours worked must be a positive value. " +
"Please enter the hours worked again: " ); // prompt for positive value for hours worked
hoursWorked = input.nextFloat (); // read hours worked again
}
employee = new EmployeeData(em pName, hourlyRate, hoursWorked) {
///weeklyPay = hourlyRate * hoursWorked; // multiply
};
System.out.prin t( employee.getNam e() ); // display employee name
System.out.prin tf( "'s weekly pay is: $%,.2f\n", employee.getWee klyPay() ); // display weekly pay
}
}
} // end method main
}} // end class Payroll4a[/CODE]
Comment