首页 > 解决方案 > 使用 getter 访问 Arraylist 的 Arraylist 字段

问题描述

我正在使用比较器来评估 ArrayLists 中的 2 个对象的等效性。当我尝试使用来自目标 ArrayList 的 getter 获取 ArrayList(Transactions)的 ArrayList(Services)的字段值时,我试图使用这些 getter 来访问 Service 类中的字段。我创建了服务类的吸气剂,但我不确定如何从两次删除的 ArrayLists 中正确访问该字段。

这是在客户类

ArrayList<Service> services = chckedTransaction.getServices(getServiceName, getPrice, getMechanics);

客户类

public class Customer {

    private String name;
    private String address;
    private int phoneNumber;
    private ArrayList<Transaction> transactioins;
    private ArrayList<Car> cars;


    public Customer(String name, String address, int phoneNumber, String carMake,
                    String carModel, int manufactureYear) {
        this.name = name;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.transactioins = new ArrayList<Transaction>();
        this.cars = new ArrayList<Car>();
        createNewCar(carMake, carModel, manufactureYear);
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public ArrayList<Transaction> getTransactioins() {
        return transactioins;
    }

    public ArrayList<Car> getCars() {
        return cars;
    }

private Transaction findTransaction(ServiceType serviceName, double price, Employee mechanic){
        ServiceComperator comperator = new ServiceComperator();
        Service dummyService = createDummyService(serviceName, price);
        for(int i=0; i<this.transactioins.size(); i++){
            Transaction chckedTransaction = this.transactioins.get(i);
            ArrayList<Service> services = chckedTransaction.getServices(getServiceName, getPrice, getMechanics);
            for(Service currentService : services){
                if(comperator.compare(dummyService, chckedTransaction.getServices())){

                }
            }
        }

private Service createDummyService(ServiceType serviceName, double price){
        Service dummyServices = new Service(serviceName, price);
        return dummyServices;
    }

交易类

public class Transaction {
    private ArrayList<Service> services;

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


    public ArrayList<Service> getServices() {
        return services;
    }

    

服务等级

public class Service {
    private ServiceType serviceName;
    private double price; //might need to use a link list double
    private ArrayList<Employee> machanics;


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


    public ServiceType getServiceName() {
        return serviceName;
    }

    public double getPrice() {
        return price;
    }

    public ArrayList<Employee> getMachanics() {
        return machanics;
    }

    public void addEmployee(String mechanicName){
        machanics.add(new Employee(mechanicName));
    }

标签: javaooparraylist

解决方案


Turns out I just had some missing parameters for the create and find methods. Thanks, guys for pointing out the missing parameters. I made the changes and now it compiles just fine.

Customer class

public boolean createNewTransaction(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
    Transaction existingTransaction = findTransaction(serviceName, price, mechanic);
    if(existingTransaction == null){
        Transaction newTransaction = new Transaction();
        newTransaction.createNewService(serviceName, price, mechanic, mechanicName);
        this.transactioins.add(newTransaction);
    }
    return false;
}

    private Transaction findTransaction(ServiceType serviceName, double price, Employee mechanic){
            ServiceComperator comperator = new ServiceComperator();
            Service dummyService = createDummyService(serviceName, price);
            for(int i=0; i<this.transactioins.size(); i++){
                Transaction chckedTransaction = this.transactioins.get(i);
                ArrayList<Service> services = chckedTransaction.getServices();//.getSericeName();
                for(Service currentService : services){
                    if(comperator.compare(dummyService, currentService) == 0){
                        return this.transactioins.get(i); // not sure if this is correct or not
    
                    }
    
                }
    
            }return null;
        }

推荐阅读