Home > Software design >  Check for duplicate in a List of class objects and return list of boolean values
Check for duplicate in a List of class objects and return list of boolean values

Time:10-17

I want a list of boolean values corresponding to the values in an ArrayList. For ex- I have a class of customer with data like name, address, phone, etc. The phono is in String Format. Now I'm taking an ArrayList to store customer objects. Now I'd want to know which customers are new and which customers are existing based on their phono, if their phono is repeated in ArrayList then a method should return false or else true.

package cabServiceProgram;

class CabCustomer
{
private int custId, distance;
private String customerName, pickupLocation, dropLocation, phone;

public CabCustomer()
{
    this.custId         = 0;
    this.distance       = 0;
    this.customerName   = null;
    this.pickupLocation = null;
    this.dropLocation   = null;
    this.phone          = null;
}

public CabCustomer(int custId, String customerName,String pickupLocation,String dropLocation, int distance,
        String phone)
{
    this.custId         = custId;
    this.distance       = distance;
    this.customerName   = customerName;
    this.pickupLocation = pickupLocation;
    this.dropLocation   = dropLocation;
    this.phone          = phone;
}
public int getCustId()
{
    return custId;
}
public void setCustId(int custId)
{
    this.custId = custId;
}
public int getDistance()
{
    return distance;
}
public void setDistance(int distance)
{
    this.distance = distance;
}
public String getCustomerName()
{
    return customerName;
}
public void setCustomerName(String customerName)
{
    this.customerName = customerName;
}
public String getPickupLocation()
{
    return pickupLocation;
}
public void setPickupLocation(String pickupLocation)
{
    this.pickupLocation = pickupLocation;
}
public String getDropLocation()
{
    return dropLocation;
}
public void setDropLocation(String dropLocation)
{
    this.dropLocation = dropLocation;
}
public String getPhone()
{
    return phone;
}
public void setPhone(String phone)
{
    this.phone = phone;
}
}

package cabServiceProgram;

class CabCustomerService
{
    private ArrayList<CabCustomer> customerList = new ArrayList<CabCustomer>();

    public void addCabCustomer(CabCustomer customer)
    {
        customerList.add(customer);
    }
}

package cabServiceProgram;

public class CabCustomerServiceTester
{
public static void main(String[] args)
{
    CabCustomer cb=new CabCustomer(101, "David", "Jersey", "New York", 1, "123");
    CabCustomer cb1=new CabCustomer(102, "John", "Houston", "Boston", 3, "000");
    CabCustomer cb2=new CabCustomer(103, "Connor","California", "Alaska", 4, "123");
    CabCustomer cb3=new CabCustomer(104, "Mark", "Los Angeles", "Nevada", 5, "321");
    CabCustomerService ccs= new CabCustomerService();
    ccs.addCabCustomer(cb);
    ccs.addCabCustomer(cb1);
    ccs.addCabCustomer(cb2);
    ccs.addCabCustomer(cb3);
}
}

CodePudding user response:

If I understood the problem correctly, you want to reject existing Customers based on their phone while adding a new customer int the CabCustomerService.

For that you can maintain a HashSet, which a hash-based collection of unique elements, containing a phone of all current customers and before adding a new customer to the list offer their phone to the set. The customer should be added only if their phone would not be rejected by the set, i.e. if Set.add() would return true.

public class CabCustomerService {
    private List<CabCustomer> customerList = new ArrayList<>();
    private Set<String> customerPhones = new HashSet<>();
    
    public void addCabCustomer(CabCustomer customer) {
        if (customerPhones.add(customer.getPhone())) {
            customerList.add(customer);
        }
    }
}

You also might want to learn what are the benefits of using abstractions like List instead of concrete classes. See What does it mean to "program to an interface"?

CodePudding user response:

You can try the below approach in order to get the desired list of boolean values corresponding to the customer data.

Approach:

Here in addCabCustomer(CabCustomer customer) method, I have checked whether the customerList contains any customer with the same phone number of the customer that needs to be add.If not then add the upcoming customer in the customerList and updated the flag accordingly

CabCustomerService.java

public class CabCustomerService {
    
    private List<CabCustomer> customerList = new ArrayList<>();

    public boolean addCabCustomer(CabCustomer customer) {
        boolean added = false;
        if(!customerList.stream().map(CabCustomer::getPhone)
                .collect(Collectors.toList()).contains(customer.getPhone())){
            added = true;
            customerList.add(customer);
        }
        return added;
    }

    public List<CabCustomer> getCustomerList() {
        return customerList;
    }
}

CabCustomerServiceTester.java

public class CabCustomerServiceTester {
    public static void main(String[] args) {
        CabCustomer cb=new CabCustomer(101, "David", "Jersey", "New York", 1, "123");
        CabCustomer cb1=new CabCustomer(102, "John", "Houston", "Boston", 3, "000");
        CabCustomer cb2=new CabCustomer(103, "Connor","California", "Alaska", 4, "123");
        CabCustomer cb3=new CabCustomer(104, "Mark", "Los Angeles", "Nevada", 5, "321");
    
        List<CabCustomer> listInitial = Arrays.asList(cb,cb1,cb2,cb3);
    
        CabCustomerService ccs= new CabCustomerService();
        List<Boolean> customerAddedFlagList = new ArrayList<>();
    
        listInitial.forEach(cabCustomer -> customerAddedFlagList.add(ccs.addCabCustomer(cabCustomer)));
        System.out.println("list indicating whether customer is added or not:: "   customerAddedFlagList);
        System.out.println("list with unique customer data:: "   ccs.getCustomerList());
        }
}

Output::

list indicating whether customer is added or not:: [true, true, false, true]
list with unique customer data:: 
[CabCustomer{custId=101, distance=1, customerName='David', pickupLocation='Jersey', dropLocation='New York', phone='123'}, CabCustomer{custId=102, distance=3, customerName='John', pickupLocation='Houston', dropLocation='Boston', phone='000'}, CabCustomer{custId=104, distance=5, customerName='Mark', pickupLocation='Los Angeles', dropLocation='Nevada', phone='321'}]
  • Related