I'm trying to modify the following 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. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated.
Here is my latest code...
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. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated.
Here is my latest code...
Code:
// Payroll program that calculates an employee's weekly pay.
import java.util.Scanner; // program uses class Scanner
class EmployeeData {
EmployeeData(String 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 Payroll3
{
private String EmployeeData; // employee information for this Payroll
// main method begins execution of java application
public static void main( String args[] )
{
boolean stop = false; // This flag will control whether we 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.println(); // outputs a blank line
System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
String empName = input.nextLine(); // read employee name
if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
{
System.out.println( "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
System.out.print( "Please enter hourly rate: $" ); // prompt
hourlyRate = input.nextFloat(); // read hourly rate from user
while (hourlyRate <= 0) // prompt until a positive value is entered
{
System.out.print( "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.print( "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.print( "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(empName, hourlyRate, hoursWorked);
///weeklyPay = hourlyRate * hoursWorked; // multiply
System.out.print( employee.getName() ); // display employee name
System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() ); // display weekly pay
}
}
} // end method main
} // end class Payroll3
Comment