【发布时间】: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;
}
【问题讨论】: