【问题标题】:How to check equivalence of an Object of an Object with a methods parameter如何使用方法参数检查对象的对象的等效性
【发布时间】:2020-11-10 04:29:03
【问题描述】:

我需要使用具有给定参数的方法来检查 ArrayList 中的对象是否存在。我拥有的方法将采用与 ArrayList 类中对象的对象的值相关的参数。

Customer 有一个 Transaction 类型的 ArrayList。事务有一个字段 Service,它是一个 ArrayList。 Service 类具有以下字段 ServiceType(enum) serviceName、double price 和一个 Employee 类型的 Arraylist,称为机械。

在客户类中,我尝试从服务类访问 getter,以与传递给“findTransaction”方法的参数进行比较。我得到“无法解析符号”,所以我确定这不是这样做的。我不知道如何从客户类中检查 Service ArrayList 对象的值,以将它们与从“findTransactions”传递的参数进行比较。通过尝试评估 ArrayLists in ArrayLists in ArrayLists in ArrayLists 与传递的参数的等效性,我可能会搞错这一切,但我不确定。

客户类别

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 boolean createNewTransaction(ServiceType serviceName, double price,
                                        Employee mechanic, String mechanicName){
        this.transactioins.add(createNewTransaction(serviceName, price, mechanic, mechanicName));


        return false;
    }
   

    private Transaction findTransaction(Service serviceName, double price, Employee mechanic){
        for(int i=0; i<this.transactioins.size(); i++){
            Transaction chkedTransaction = this.transactioins.get(i);
            if(chkedTransaction.getServices(getServiceName, getPrice, getMechanics).equals(serviceName, price, mechanic));
        }
    }

交易类

public class Transaction {
    private ArrayList<Service> services;

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


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

    //when called service will be creted with new
    public boolean createNewService(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
        Service existingService = findService(serviceName, price, mechanic);
        if(existingService == null){//MAKE A CREATE EMPLOYEE METHOD
            Service newService = new Service(serviceName, price);
            newService.createEmployee(mechanicName);
            this.services.add(newService);
//            this.services.add(new Service(serviceName, price, new Employee(mechanicName)));
        }
        return false;
    }

    // to see if the service exists
    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;
    }

服务类

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 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(chkedEmployee.getName().equals(name)){
                return chkedEmployee;
            }

        }
        return null;
    }

【问题讨论】:

    标签: java oop arraylist


    【解决方案1】:

    根据我收集到的信息,您尝试比较交易中的服务对象

    我将实现一个自定义比较器对象,该对象根据您要使用的字段比较服务对象, 然后创建一个包含参数的对象,然后遍历它

    使用您的代码,它看起来像:

    class ServiceComperator implements Comparator<Service>{
          
    
           // Overriding the compare method to sort the age 
           public int compare(Service s1, Service s2) {
              if (s1.field==s2.field&&s1.field1==s2.field1 etc...)
               {
                  return 0;
               }
              else
              {
                 return -1;
              }
           }
    

    和你的比较方法:

    private Transaction findTransaction(Service serviceName, double price, Employee mechanic){
               ServiceComperator comperator=new ServiceComperator();
               Service dummyService=createDummyService();
               for(int i=0; i<this.transactioins.size(); i++){
                    Transaction chkedTransaction = this.transactioins.get(i);
                    List<Service> services=chkedTransaction.getServices(getServiceName, getPrice, getMechanics);
                    for (Service currentService : services) {
                        if (comperator.compare(dummyService, chkedTransaction.service())==0)
                        {
                            //they are equal do something
                        }
                        else
                        {
                            //they arent do something else
                        }
                    }
                    
                }
           }
    
     private Service createDummyService(params)
           {
           //create a service object based on the fields you want to compare
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 2020-05-30
      • 1970-01-01
      • 2020-04-07
      • 2020-11-06
      相关资源
      最近更新 更多