I have this class which is meant to take values of type double
from arrays within an ArrayList
and increase them by a defined rate:
import java.util.ArrayList;
public class Company {
static ArrayList<Employee> employees;
public Company() {
Company.employees = new ArrayList<Employee>();
}
public static ArrayList<Employee> getEmployees() {
return employees;
}
public static void setEmployees(ArrayList<Employee> employees) {
Company.employees = employees;
}
public void increaseSalaries(double rate) {
if (rate <= 0.0) {
throw new IllegalArgumentException();
} else {
for (int i = 0 ; i < employees.size() ; i++) {
employees.get(i).increaseSalaries(rate);
}
}
}
}
I want to write a main method which builds an arraylist and calls the method on it, but I'm not sure how. So far I have
public static void main(String[] args) {
Company businesstown;
HourlyEmployee george;
MonthlyEmployee ambrose;
george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
}
but I'm unsure about how to add these arrays to the ArrayList
businesstown. I've tried a lot of combinations of .add(george);
but I can't get it right. Either it says that
the method add(HourlyEmployee) is not defined for type Company
or it compiles and it throws a NullPointerException
.
(I should add that both HourlyEmployee
and MonthlyEmployee
are classes which extend Employee
)