提问人:YHapticY 提问时间:7/21/2020 最后编辑:YHapticY 更新时间:11/15/2023 访问量:61
比较 Java 中的嵌套 ArrayList 对象:根据服务详细信息查找事务
Comparing Nested ArrayList Objects in Java: Finding Transactions Based on Service Details
问:
我正在开发一个 Java 程序,我需要根据服务详细信息找到特定的事务。该计划包括三个类:客户、交易和服务。Customer 有一个 Transaction 对象的 ArrayList,每个 Transaction 都包含一个 Service 对象的 ArrayList。Service 类具有 ServiceType(枚举)、serviceName、price 和名为 mechanics 的 Employee 对象的 ArrayList 等属性。
我的挑战是在 Customer 类中实现一个方法 findTransaction。此方法应根据作为参数提供的服务详细信息来查找事务。但是,我正在努力将嵌套 ArrayLists 中的 Service 对象与给定的参数进行比较。
尝试从 Customer 类访问 Service 类 getter 时遇到“无法解析符号”错误。我怀疑我访问和比较嵌套 ArrayList 对象的方法可能不正确,但我不确定如何继续。
这是我当前实现的简化版本:
客户类
public class Customer { 私有 ArrayList 事务;
public Customer() {
this.transactions = new ArrayList<>();
}
// Method to find a transaction based on service details
public Transaction findTransaction(ServiceType serviceName, double price, Employee mechanic) {
for (Transaction transaction : transactions) {
if (transaction.containsService(serviceName, price, mechanic)) {
return transaction;
}
}
return null;
}
}
TRANSACTIONS 类
public class Transaction {
private ArrayList<Service> services;
public Transaction() {
this.services = new ArrayList<>();
}
// Helper method to check if a specific service exists in this transaction
public boolean containsService(ServiceType serviceName, double price, Employee mechanic) {
for (Service service : services) {
if (service.matches(serviceName, price, mechanic)) {
return true;
}
}
return false;
}
}
服务等级
public class Service {
private ServiceType serviceName;
private double price;
private ArrayList<Employee> mechanics;
public Service(ServiceType serviceName, double price) {
this.serviceName = serviceName;
this.price = price;
this.mechanics = new ArrayList<>();
}
// Helper method to check if this service matches the given details
public boolean matches(ServiceType serviceName, double price, Employee mechanic) {
return this.serviceName.equals(serviceName) &&
this.price == price &&
this.mechanics.contains(mechanic);
}
}
答:
0赞
Gabriel H
7/21/2020
#1
从我收集到的信息中,您可以尝试比较事务中的服务对象
我将实现一个自定义 Comperator 对象,该对象根据您要使用的字段比较服务对象, 然后创建一个包含您拥有的参数的对象,然后遍历它
使用您的代码,它看起来像这样:
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
}
评论