Reputation: 195
I'm working on a Java program where I need to find a specific transaction based on service details. The program includes three classes: Customer, Transaction, and Service. A Customer has an ArrayList of Transaction objects, and each Transaction contains an ArrayList of Service objects. The Service class has attributes like ServiceType (an enum), serviceName, price, and an ArrayList of Employee objects named mechanics.
My challenge is to implement a method, findTransaction, in the Customer class. This method should locate a Transaction based on service details provided as parameters. However, I'm struggling with how to compare the Service objects in the nested ArrayLists with the given parameters.
I'm encountering a "Cannot resolve Symbol" error when trying to access the Service class getters from the Customer class. I suspect my approach to accessing and comparing nested ArrayList objects might be incorrect, but I'm unsure how to proceed.
Here's a simplified version of my current implementation:
CUSTOMER CLASS
public class Customer { private ArrayList transactions;
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 CLASS
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;
}
}
SERVICE CLASS
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);
}
}
Upvotes: 0
Views: 66
Reputation: 1576
from what i gather you try to compare service objects in the transactions
i would implement a custom comperator object that compares service objects based on the fields that you want to use, then create an object containing the parameters you have then iterate through it
using your code it would look something like :
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;
}
}
and your comparing method:
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
}
Upvotes: 0