//File: Employee2.java
import CSLib.*;
/**
* This class represents Employees
* @author Bill Steinmetz
*/
public class Employee2
{
//Attributes (data) of Employee - Instance Variables
private String firstName,
lastName;
private double hoursWorked,
payRate,
salary; //added to attributes
//Behavior (operations) of Employee- Instance Methods
//Accessor methods
//The toString() method allows direct output of an Employee object
//through System.out.print(employee)
/**
* @return a String representing all of
* the Employee's data
*/
public String toString()
{
return firstName + " " + lastName + " worked " + hoursWorked
+ " hours at $ " + payRate + " per hour and earned $ "
+ salary + ".";
}
/**
* @return a String representing the Employee's full name
*/
public String getName()
{
return firstName + " " + lastName;
}
//Mutator methods
/**
* Calculates the Employee's salary including time worked
* beyond 40 hours per week (overtime)
*/
public void calculateSalary ()
{
if (hoursWorked < 40)
{
salary = payRate * hoursWorked;
}
else
{
salary = payRate * 40 + 1.5 * payRate * (hoursWorked - 40);
}
}
/**
* Interactively assigns the Employee's first and last names
*/
public void setName()
{
InputBox in = new InputBox();
in.setPrompt ("First name please: ");
firstName = in.readString();
in.setPrompt ("Last name please: ");
lastName = in.readString();
}
/**
* Interactively assigns the number of hours worked
*/
public void setHoursWorked()
{
InputBox in = new InputBox();
in.setPrompt ("How many hours did you work last week? ");
hoursWorked = in.readDouble();
}
/**
* Interactively assigns the Employee's rate of pay
*/
public void setPayRate()
{
InputBox in = new InputBox();
in.setPrompt ("What is your hourly pay rate? ");
payRate = in.readDouble();
}
}