0

so I have class Customer and class Bank , a bank class has methods of adding Customer to an arraylist of Customers , and also must have a method to search a Customer in an arraylist and delete it (Remove from arraylist) , How can I do it?

Bank class -

private ArrayList<Customer> customers = new ArrayList<>();

public void addCustomer(String name){

    Customer customer = new Customer(name);
    customers.add(customer);
    System.out.println("new customer " + customer.getName() + " added");
}

public void deleteAccount(String name){

}

Customer class -

private String name;
private double balance;

public Customer(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return "Customer " +
            "name :'" + name + '\'' +
            '}';
}

Main -

public static void deleteAccount(){
    System.out.println("Enter the name you want to delete");
    String name = scanner.nextLine();
    scanner.nextLine();
    bank.deleteAccount(name);
}

As you can see, the main class has method which takes input type String from user and then calles Bank class's method of deleteAccount with that input , but I don't know how to proceed with the deleteAccount method, how to make it work ?

I need something that firstly checks if the user input is in the Arraylist, and then remove it from there if there is.

I do realize that input is String and Arraylist is Customer instances , but Customer only takes String name in constractor, so can I make it work this way?

1
  • loop, compare, if it´s equal, remove. Commented Dec 4, 2017 at 9:04

5 Answers 5

3

I suggest to use the HashMap object instead of an ArrayList. A HashMap maps a set of keys (costumer names in your case) to a set of objects (the costumer)

HashMap<String,Customer> customers = new HashMap<String,Customer>();

In addCustomer you can do this.customers.put(name,customer) and in delete account you can do this.customers.remove(name)

Otherwise you can iterate over the list as suggested by Timothy, but this is more natural and more efficient

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

1 Comment

I think HashMap is the best way to do this, ...just note that as soon as you add a "duplicate" Customer (same name), the first Customer get's replaced
1

I prefer using Map so delete/exists will be in O(1) instead of O(n)

public class Bank {
private Map<String,Customer> customers = new HashMap<>();

public void addCustomer(String name){

    Customer customer = new Customer(name);
    customers.put(customer.getName(),customer);
    System.out.println("new customer " + customer.getName() + " added");
}

// O(1) -- no need to iterate over an array for deleting customer
public boolean deleteAccount(String name){
    if(customers.containsKey(name)){
        customers.remove(name);
        return true;
    }
    return false;
}

@Override
public String toString() {
    return "Bank{" +
            "customers=" + customers +
            '}';
}

public static void main(String[] args){

    Bank bank = new Bank();                 // create Bank object

    bank.addCustomer("Jhon");       // insert some customer
    bank.addCustomer("Doe");
    System.out.println(bank);
    bank.deleteAccount("Yossi");    // not exists - return false
    bank.deleteAccount("Doe");      // removed - return true

    System.out.println(bank);

}

}

Hop this help a bit...

2 Comments

Thanks , I read through all the answers and I think yeah, best way to do is to make Map and not Arraylist .
I'm currently using your code and now I understand why map was perfect for my example, thanks a lot .
1

Iterate over the list and check if the supplied name is equal to the name retrieved from customer in the list , if yes then delete it.

public void deleteAccount(String name){

      Iterator<Customer> itr=  customers.iterator();
      while(itr.hasNext()){

          String cuName = itr.next().getName();
          if(cuName.equalsIgnoreCase(name)){
              //delete it from list
              //break out of loop
              itr.remove();
              break;
          }
      }

    }

Comments

1

Deleting account by name is not proper way. Because there is chance of multiple customers with same name. So delete account wiht account number will be right option.

Still you want delete account by customer name then check following:

Customer custToDelete = null;
for(Customer customer:customers){
        if(customer.getName().equals(name))
        custToDelete = customer;
}

if(custToDelete==null)
    System.out.println("No customer found");
else
    customers.remove(custToDelete);

2 Comments

If you use Map then you can easily achieve this.
Thanks , using Map's is probably best way to do this .
0

deletion Java 8:

  Optional<Customer> customerToDelete = customers.stream().filter(cust->cust.getName().equals(name)).findFirst();

  if(customerToDelete.isPresent()){
    customers.remove(customers);
  }else{
    System.out.println("Customer does not exist");
  }

Btw. I think you should check at "addCustomer" if a customer with the given name already exists... something like

 customers.stream().filter(cust->cust.getName().equals(name)).count()

should do the trick

3 Comments

This makes the program run through the list twice, though. Also, requires the Customer class to implement hashCode and equals, which it doesn't seem to do currently.
where is the second "run through" if you want to delete a Customer? and why does my solution need hashCode and equals... ArrayList.remove does a pointer comparison which is fine for this 'problem'
Agree on the second point. Reference equality should be sufficient. First run is finding the customer based on name. Second run is customers.remove, which runs through the list until it encounters the instance that needs removal.

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.