/** * The CustomeDatabase class is used to store the customers, and create * methods to retrieve, update, and validate customer and product information. * * @author Emma Schultz */ import java.util.*; public class CustomerDatabase extends Customer { // list of customer names in the database private List customerlist; // number of customers in the database private int dbsize; /** * Constructs an empty customer database. */ public CustomerDatabase() { super("CustomerDatabase"); // used because the class extends Customer customerlist = new ArrayList(); dbsize = 0; } /** * Add a customer with the given username c to the end of the database. * If a customer with username c is already in the database, just return. * * @param c the username of the customer */ public void addCustomer(String c) { // validate parameter if (c == null) { throw new IllegalArgumentException(); } // check to see whether customer c is already in the database if (!this.containsCustomer(c)) { return; } // add customer Customer temp = new Customer(c); customerlist.add(temp) ; dbsize++; } /** * Add the given product p to the wish list for customer c in the * database. If customer c is not in the database throw a * java.lang.IllegalArgumentException. If p is already in the wish * list for customer c, just return. * * @param c the username of the customer * @param p the product name */ public void addProduct(String c, String p) { // validate parameters if (c == null) { throw new IllegalArgumentException(); } if (p == null) { throw new IllegalArgumentException(); } // check to see whether customer c is already in the database if (!this.containsCustomer(c)) { throw new IllegalArgumentException(); } // iterate over customers to find customer c Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; String name = temp.getUsername() ; if (name.equals(c)) { List wish = temp.getWishlist(); // iterate over wish list contents Iterator wishIter = wish.iterator(); while(!wishIter.hasNext()) { String tempWish = wishIter.next() ; if (tempWish.equals(p)) { return; // if p is in wish list, just return } } wish.add(p); // haven't returned, therefore add p to wish list } } } /** * Return true iff customer c is in the database. * * @param c the username of the customer */ public boolean containsCustomer(String c) { // validate parameters if (c == null) { throw new IllegalArgumentException(); } if (dbsize > 0) { // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; String name = temp.getUsername() ; if (name.equals(c)) { // match on username return true ; } } } return false ; } /** * Return true iff product p appears in at least one customer's wish * list in the database. * * @param p the product name */ public boolean containsProduct(String p) { // validate parameters if (p == null) { throw new IllegalArgumentException(); } if (dbsize > 0) { // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; List wish = temp.getWishlist(); // iterate over wish list contents Iterator wishIter = wish.iterator(); while(!wishIter.hasNext()) { String tempWish = wishIter.next() ; if (tempWish.equals(p)) { return true; // at least one p in a wish list } } } } return false ; } /** * Returns true iff product p is in the wish list for customer c. * If customer c is not in the database, return false. * * @param c the username of the customer * @param p the product name */ public boolean hasProduct(String c, String p) { // validate parameters if (c == null) { throw new IllegalArgumentException(); } if (p == null) { throw new IllegalArgumentException(); } // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; String name = temp.getUsername() ; if (name.equals(c)) { List wish = temp.getWishlist(); // iterate over wish list for customer c Iterator wishIter = wish.iterator(); while(!wishIter.hasNext()) { String tempWish = wishIter.next() ; if (tempWish.equals(p)) { // found p in wish list return true ; } } } } return false ; } /** * Return the list of customers who have product p in their wish list. * If product p is not in the database, return null. * * @param p the product name */ public List getCustomers(String p) { // validate parameter if (p == null) { throw new IllegalArgumentException(); } List output = new ArrayList(); // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; List wish = temp.getWishlist(); // iterate over wish list contents Iterator wishIter = wish.iterator(); while(!wishIter.hasNext()) { String tempWish = wishIter.next() ; if (tempWish.equals(p)) { // found p in wish list output.add(temp.getUsername()); // add customer to output } } } if (output.size() > 0) { // i.e. if p was found in a customer wish list return output; } else { return null; } } /** * Return the wish list for the customer c. If a customer c is not in * the database, return null. * * @param c the username of the customer * @return */ public List getProducts(String c) { // validate parameter if (c == null) { throw new IllegalArgumentException(); } // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; if (temp.getUsername().equals(c)) { // found customer return temp.getWishlist(); // return corresponding wish list } } return null; } /** * Return an Iterator over the Customer objects in the database. The * customers should be returned in the order they were added to the * database (resulting from the order in which they are in the text file). */ public Iterator iterator() { return this.iterator(); } /** * Remove customer c from the database. If customer c is not in the * database, return false; otherwise (i.e., the removal is successful) * return true. * * @param c the username of the customer */ public boolean removeCustomer(String c) { // validate parameter if (c == null) { throw new IllegalArgumentException(); } // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; if (temp.getUsername().equals(c)) { customerlist.remove(temp) ; // perform removal dbsize--; // decrement size of customer database return true ; } } return false ; } /** * Remove product p from the database, i.e., remove product p from * every wish list in which it appears. If product p is not in the * database, return false; otherwise (i.e., the removal is successful) * return true. * * @param p - the product name */ public boolean removeProduct(String p) { // validate parameter if (p == null) { throw new IllegalArgumentException(); } // define boolean for return boolean found; found = false; // iterate over customers Iterator iter = customerlist.iterator() ; while(!iter.hasNext()) { Customer temp = iter.next() ; List wish = temp.getWishlist(); // iterate over wish list contents Iterator wishIter = wish.iterator(); while(!wishIter.hasNext()) { String tempWish = wishIter.next() ; if (tempWish.equals(p)) { // found product to remove wish.remove(p); found = true; // note that removal occurred } } } if (found) return true; else return false; } /** * Return the number of customers in this database. */ public int size() { return dbsize; } }