Ok I have everything working for the most part. I think. Here is what I am trying to do. I am to allow the user to enter the name of an employee, hours worked over 5 days, and pay rate. I have done that. I am then supposed to generate a table of names, total hours worked and total pay. I don't think I am using the EmployeeModel class correctly. I am to use this class.
public class EmployeeModel extends Object {
public static final int MAX_EMPLOYEES = 20;
private Employee[] employees;
private int employeeCount;
public EmployeeModel(){
employees = new Employee[MAX_EMPLOYEES];
employeeCount = 0;
}
public Employee getEmployee(int position){
return employees[position];
}
public String addEmployee(Employee emp){
if (employeeCount == MAX_EMPLOYEES)
return ("Sorry - no room in array");
else{
employees[employeeCount] = emp;
employeeCount++;
return null;
}
}
public int getCount(){
return employeeCount++;
}
}
And this
public class Employee extends Object {
// Class variables (actually, constants)
static public final int MAX_DAYS = 5;
static public final int MAX_REGULAR_HOURS = MAX_DAYS * 8;
static public final double OVERTIME_RATE = 1.5;
// Instance variables
private String name;
private double payRate;
private int[] days;
// Constructor methods
// Initialize a new employee's name to the empty string and her
// pay rate and hours worked to 0
public Employee(){
name = "";
payRate = 0;
days = new int[MAX_DAYS];
for (int i = 0; i < MAX_DAYS; i++)
days[i] = 0;
}
// Initialize a new employee's attributes to the given parameters
// Preconditions: payRate > 0
// size of array == MAX_DAYS
public Employee(String name, double payRate, int[] hoursWorked){
if (hoursWorked.length != MAX_DAYS)
throw new IllegalArgumentException(
"Array must have " + MAX_DAYS + " days");
if (payRate <= 0)
throw new IllegalArgumentException("Pay rate must be > 0");
this.name = name;
this.payRate = payRate;
days = new int[MAX_DAYS];
for (int i = 0; i < MAX_DAYS; i++)
days[i] = hoursWorked[i];
}
// Set an employee's name
public void setName (String name){
this.name = name;
}
// Get an employee's name
public String getName (){
return name;
}
// Set an employee's pay rate
// Precondition: newRate > 0
public void setPayRate (double newRate){
if (newRate < 0)
throw new IllegalArgumentException("new rate must be > 0");
payRate = newRate;
}
// Get an employee's pay rate
public double getPayRate (){
return payRate;
}
// Set the hours worked on the indicated day
// Preconditions: 1 <= whichDay <= MAX_DAYS
// hours >= 0
public void setHours (int whichDay, int hours){
if (whichDay < 1 || whichDay > MAX_DAYS)
throw new IllegalArgumentException(
"Day must be >= 1 and <= " + MAX_DAYS);
if (hours < 0)
throw new IllegalArgumentException("hours must be >= 0");
days[whichDay - 1] = hours;
}
// Get the hours worked on the indicated day
public int getHours (int whichDay){
if (whichDay < 1 || whichDay > MAX_DAYS)
throw new IllegalArgumentException(
"Day must be > 1 and <= " + MAX_DAYS);
return days[whichDay - 1];
}
// Compute and return an employee's total hours
public int getTotalHours(){
int total = 0;
for (int i = 0; i < MAX_DAYS; i++)
total += days[i];
return total;
}
// Compute and return an employee's weekly pay
public double computePay(){
int total = getTotalHours();
int overtimeHours = total - MAX_REGULAR_HOURS;
if (overtimeHours > 0)
return payRate * OVERTIME_RATE * overtimeHours +
payRate * MAX_REGULAR_HOURS;
else
return payRate * total;
}
// Return a string representation of a employee's name, pay rate
// and hours worked.
public String toString(){
String str = "Name: " + name + "\n" +
"Pay rate: " + payRate + "\n" +
"Hours worked:\n";
for (int i = 0; i < MAX_DAYS; i++)
str += "Day " + (i + 1) + ": " + days[i] + "\n";
return str;
}
// Compare two employees for equality
public boolean equals(Object other){
if (! (other instanceof Employee))
throw new IllegalArgumentException("Object not an employee");
Employee employee = (Employee)other;
return name.equals(employee.name);
}
// Compare two employees for order
public int compareTo(Object other){
if (! (other instanceof Employee))
throw new IllegalArgumentException("Object not an employee");
Employee employee = (Employee)other;
return name.compareTo(employee.name);
}
// Clone a new employee
public Object clone(){
int[] newDays = new int[MAX_DAYS];
for (int i = 0; i < MAX_DAYS; i++)
newDays[i] = days[i];
return new Employee (name, payRate, newDays);
}
}
This is my main.
import java.util.*;
public class EmployeeDriver{
public static void main(String[] args){
//Creates a default EmployeeModel object
EmployeeModel emp1 = new EmployeeModel();
Scanner keyboard = new Scanner (System.in);
System.out.println("Do you have Employee to enter. Yes or No");
String option = keyboard.next();
while(option.equals("Yes")){
System.out.println("Please Enter the Empoyee name.");
String name = keyboard.next();
System.out.println("Please enter the number of hours worked for each day");
int [] hours = new int [5];
for(int i=0; i<hours.length; i++){
hours[i] = keyboard.nextInt();
}
System.out.println("Please enter the pay rate.");
double rate = keyboard.nextDouble();
emp1.getCount();
Employee employeeCount = new Employee(name, rate, hours);
System.out.println(employeeCount.toString());
emp1.addEmployee(employeeCount);
System.out.println("Do you have another Employee to enter. Yes or No");
option = keyboard.next();
employeeCount.getName();
}
}
}
My problem is that it is not creating more than one Employee and doesn't keep track of the different name, rate, and hours. Any idea of what I am doing wrong.