0

I have a customer object with three properties: last name, first name, and SIN number

If the user enters the customer's last name and first name, and the object is found in the arraylist, the objects gets removed.

private static void deleteCustomer (String lastName, String firstName, List<Customer> accounts)
{
    for (int i = 0; i < accounts.size(); i++)
    {            
        accounts.get(i);
        public int compare (Customer c1, Customer c2) 
        {
            Customer customerOne = (Customer) c1;
            Customer customerTwo = (Customer) c2;
            if (lastName.equals(CustomerOne.getLastName()) && firstName.equals(CustomerOne.getFirstName()))
            {

            }
        }
    }
}

If two customers have the same first and last name, the user is asked to enter the SIN number, that's why I have the compare method. I'm not sure what to do after this.

Updated method:

public static void deleteCustomer (String lastName, String firstName, List<Customer> accounts)
{
    for (Iterator<Customer> iterator = accounts.iterator(); iterator.hasNext();)
    {
        Customer customer = iterator.next();
        if(lastName.equals(customer.getLastName()) && firstName.equals(customer.getFirstName()))
        {
            iterator.remove();
        }
    }
}

This works, but it removes all customers with the same first and last name

4
  • Before you think about what to write inside the compare method, think about where to place it. Hint: inside another method is not a good place. Commented Jan 17, 2015 at 18:10
  • Does this compile? Your compare method is inside the deleteCustomer method? Commented Jan 17, 2015 at 18:11
  • Yes, it compiles. I have updated the original post with the new method Commented Jan 17, 2015 at 18:19
  • Yes, it compiles. No, it doesn't. The updated code maybe does, but not the first one. Commented Jan 17, 2015 at 18:22

1 Answer 1

1

If you simply wish to delete one customer as you have described in the question you should break the loop after you have found the correct customer and removed it:

iterator.remove();
break; // breaks the loop

But, since it seems as if there may be multiple customers with the same first+lastname the algorithm is not exactly bullet-proof. Maybe a customer id or similar should be used to distinguish between customers. In the case of a customer id the data structure to use should quite possibly be a Map<CustomerId, Customer>. Customers can then be quickly accessed via the id and you still can retrieve a collection of all customers by invoking the values() method.

In addition to this, if you don't want to modify the original List you can also use a Java 8 construct where you stream, filter and collect your data.

final List<Customer> newAccountList = accounts.stream()
            .filter(c -> !(Objects.equals(c.getFirstName(), firstName) &&
                    Objects.equals(c.getLastName(), lastName)))
            .collect(Collectors.toList());

The List above contains everything from the original List except for those entries where first+lastname matches.

Sign up to request clarification or add additional context in comments.

2 Comments

Every customer has a unique SIN number. Would I still have to implement Map<> to distinguish between them?
@user3479783 Either you use a Map<String, Customer> where the key is the SIN (a good aproach). Or, you can remove customers based on the SIN instead of first+lastname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.