Skip to content
Advertisement

How to add ArrayList parameter to an ArrayList then then add to current class ArrayList

I have an ArrayList called Service that accepts an enum ServiceType, a double price, and an ArrayList Employee.

I’m trying to create a method that adds a service element to the Service ArrayList but I’m getting an error stating

Service() in Service cannot be applied to Expected parameters Actual Arguments serviceName: serviceType serviceName price double price new Employee(mechanicName) (Employee)

I have tried creating the ArrayList object for Employee inside the Service ArrayList object and then adding it to the Transactions ArrayList but that is not working.

///Transactions class

public class Transaction {
    private ArrayList<Service> services;

    public Transaction() {
        this.services = new ArrayList<Service>();
    }

    
    public boolean createNewService(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
        Service existingService = findService(serviceName, price, mechanic);
        if(existingService == null){

            this.services.add(new Service(serviceName, price, new Employee(mechanicName)));
        }
        return false;
    }

    
    private Service findService(ServiceType serviceName, double price,
                                Employee machanic){
        for(int i=0; i<this.services.size(); i++){
            Service chkdServise = this.services.get(i);
            if(this.services.get(i).getServiceName().equals(serviceName) &&
            this.services.get(i).getPrice() == price &&
            this.services.get(i).getMachanics().equals(machanic)){
                return chkdServise;
            }
        }
        return null;
    }

///Service Class

public class Service {
    private ServiceType serviceName;
    private double price; 
    private ArrayList<Employee> machanics;


    public Service(ServiceType serviceName, double price) {
        this.serviceName = serviceName;
        this.price = price;
        this.machanics = new ArrayList<Employee>();
    }

public boolean createEmployee(String name){
        Employee existingEmployee = findEmployee(name);
        if(existingEmployee == null){
            this.machanics.add(new Employee(name));
            return true;
        }
        return false;
    }


    private Employee findEmployee(String name){
        for(int i=0; i<machanics.size(); i++){
            Employee chkedEmployee = this.machanics.get(i);
            if(this.machanics.get(i).getName().equals(name)){
                return chkedEmployee;
            }

        }
        return null;
    }

///Employee Class

public class Employee {
    private String name;
    private int salary;


    public Employee(String name) {
        this.name = name;
        this.salary = 50000;
    }

    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }


    }

Advertisement

Answer

There are some issues with your code. But the most important one that is causing the error is, you have a constructor for your Service which accepts only two parameters ServiceType and price, but you are trying to pass in three parameters in your Transaction class. What you need is :

    public boolean createNewService(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
        Service existingService = findService(serviceName, price, mechanic);
        if(existingService == null){
            Service newService = new Service(serviceName, price);
            newService.createEmployee(new Employee(mechanicName));
            this.services.add(newService);
        }
        return false;
    }

You see that I changed code to pass in two parameters only for the Service object which is what is needed by its constructor. And then used createEmployee method you already have in your Service class. And then add the new service to the list of services.

Other minor improvements ( that I could find ):

  1. findService method
private Service findService(ServiceType serviceName, double price,
                            Employee machanic) {
    for(int i=0; i<this.services.size(); i++) {
        Service chkdServise = this.services.get(i);
        if(chkdServise.getServiceName().equals(serviceName) &&
        chkdServise.getPrice() == price &&
        chkdServise.getMachanics().equals(machanic)){
            return chkdServise;
        }
    }
    return null;
}

See that you don’t need to use this.services.get(i) everytime. You already got it once in chkdServise.

  1. Similar improvement with findEmployee:
private Employee findEmployee(String name){
        for(int i=0; i<machanics.size(); i++){
            Employee chkedEmployee = this.machanics.get(i);
            if(chkedEmployee.getName().equals(name)){
                return chkedEmployee;
            }

        }
        return null;
    }

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement