1

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)

2 Answers2

1

In my opinion it is better to make the ArrayList a private member of the class and have an add method for adding employees

public class Company {
    private ArrayList<Employee> employees;

    public Company() {
        employees = new ArrayList<Employee>();
    }

    public void addEmployee(Employee e) {
        employees.add(e);
    }
    //rest of code
}

and in main

public static void main(String[] args) {
    Company businesstown = new Company();
    HourlyEmployee george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
    MonthlyEmployee ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
    businesstown.add(george);
    businesstown.add(ambrose);
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

You can add them to the getEmployees returned List :

businesstown.getEmployees().add(george);
businesstown.getEmployees().add(ambrose);

But make sure to initialise the instance and the employees within it before accessing businesstown.

Company businesstown = new Company(); // since for you this is initialisin the list as well
Naman
  • 27,789
  • 26
  • 218
  • 353
  • @MuratKaragöz Be sure to instantiate `businesstown` eg `businesstown = new Company();` aswell. – Mark Dec 05 '18 at 14:36
  • 1
    @MuratKaragöz You caught it before the edit :) – Naman Dec 05 '18 at 14:36
  • Thank you! What would be the print command? Obviously I can't just do System.out.println(businesstown). Would I need to create a toString method? –  Dec 05 '18 at 14:51
  • @kotsoteka Two points - 1. That's a separate question and should be decoupled. 2. You can iterate on the list if you'are just interested to print the list, in case you're interested to print the object and get the information of the list as well, then yes you would have to override the `toString` as well. – Naman Dec 05 '18 at 14:52