简体   繁体   English

如何将 ArrayList 参数添加到 ArrayList 然后添加到当前 class ArrayList

[英]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.我有一个名为 Service 的 ArrayList,它接受枚举 ServiceType、双倍价格和 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我正在尝试创建一种将服务元素添加到服务 ArrayList 的方法,但我收到一条错误消息

Service() in Service cannot be applied to Expected parameters Actual Arguments serviceName: serviceType serviceName price double price new Employee(mechanicName) (Employee) Service 中的 Service() 不能应用于预期参数 实际 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. 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 ///事务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 ///服务 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 ///员工 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;
    }


    }

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.但是导致错误的最重要的一个是,您的Service有一个构造函数,它只接受两个参数ServiceTypeprice ,但是您试图在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.您会看到我更改了代码,只为Service object 传递了两个参数,这是其构造函数所需要的。 And then used createEmployee method you already have in your Service class.然后使用您在Service class 中已有的createEmployee方法。 And then add the new service to the list of services.然后将新服务添加到服务列表中。

Other minor improvements ( that I could find ):其他小的改进(我能找到):

  1. findService method findService方法
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.看到你不需要每次都使用this.services.get(i) You already got it once in chkdServise .您已经在chkdServise中获得过一次。

  1. Similar improvement with findEmployee : 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;
    }

There is mismatch in the constructor arguments of Service class. Service class 的构造函数 arguments 不匹配。 What IDE what are using?什么IDE是用什么? You should have got the error at the compilation step.您应该在编译步骤中遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM