/************************************************ Class CustomerCollection - Practice Quiz You are to implement a method named insert and a method named remove to this CustomerCollection class as described in the exam instructions. You may NOT alter the signature of these methods. You may add additional helper (private) methods if you choose, but you may NOT add additional public methods, nor may you change any methods that we have already defined. Name: Section: Date: andrew e-mail: *************************************************/ public class CustomerCollection { private Customer[] customerArray; // unfilled array for valid customers private int numCustomers; // number of valid customers /** * Constructor: allocates an array of length 1 * sets numCustomers to 0 * does not construct any individual customers */ public CustomerCollection() { customerArray = new Customer[1]; numCustomers = 0; } /************************************************ * TO THE STUDENT: IMPLEMENT THIS INSERT METHOD * ************************************************/ /** * Inserts a value, newCustomer, into this CustomerCollection * as described in the exam instructions. * * @param newCustomer (Customer) the new Customer to be inserted into the collection * @returns void */ // Complete the insert method here public void insert(Customer newCustomer) { // See if can find newCustomer in the customerArray // Since we don't know if we need to look at the whole array // we use a while loop. int position = 0; while (position < numCustomers && !(customerArray[position].getName() .equals(newCustomer.getName()))) { position++; } // If found newCustomer in customerArray, then position < numCustomers // otherwise it is not in the array if (position < numCustomers) { // found in the array, so swap it with the customer preceding it, // if there is one. // OK to write over customer at position because have a copy in // newCustomer. if (position > 0) { customerArray[position] = customerArray[position-1]; customerArray[position-1] = newCustomer; } // Since this not an addition to customerArray, we do not // increment numCustomers } else { // newCustomer is not in the array // add newCustomer to the end of the array insertAtEnd(newCustomer); } } /************************************************ * TO THE STUDENT: IMPLEMENT THIS REMOVE METHOD * ************************************************/ /** * Removes a customer from the CustomerCollection * as described in the exam instructions. * * @param customerName (String) the name of the customer to be removed from the collection * @returns void */ //Write the remove method here public void remove(String customerName) { // See if can find a customer in the customerArray with the // name customerName // Since we don't know if we need to look at the whole array // we use a while loop. int position = 0; while (position < numCustomers && !(customerArray[position].getName().equals(customerName))) { position++; // Try the next customer } // If found such a customer in customerArray, then position // < numCustomers // otherwise it is not in the array and we do nothing. if (position < numCustomers) { // Customer to delete is at index position. // Shift all the customer references after index position towards // the front one slot, overwriting the customer reference at // index position. for (int fromPos = position+1; fromPos < numCustomers; fromPos++) { customerArray[fromPos-1] = customerArray[fromPos]; } // Now we have one less customer and we can remove the reference // that was at the end of all the customer references. numCustomers--; customerArray[numCustomers] = null; } } /************************************************ * TO THE STUDENT: EXTRA PRACTICE PROBLEM * ************************************************/ /** * Creates a new CustomerCollection * as described in the exam instructions. * * @param customerName (String) the name of the customer to be removed from the collection * @returns void */ //Write the remove method here public CustomerCollection filter(int usage) { // Create a new CustomerCollection in which to put the customers // selected. The constructor initializes the collection to have // no customers and creates its own customerArray of length 1. CustomerCollection newCollection = new CustomerCollection(); // For each customer in this collection that has at least // as much telephone usage as specified in the parameter, // add the customer to the new collection for (int i = 0; i < numCustomers; i++) { if (customerArray[i].getMinutes() >= usage) { // Found a customer, so add the customer to the collection // in the same order as in this collection. // Since newCollection is an object of the CustomerCollection class // it can invoke the private methods of the CustomerCollection class. // The insertAtEnd method handles doubling the array and updating its // numCustomer field as needed. newCollection.insertAtEnd(customerArray[i]); } } return newCollection; } /************************************************ * TO THE STUDENT: * * DO NOT MODIFY ANY CODE BELOW THESE COMMENTS * ************************************************/ /** * Inserts the new item at the next available slot in the array, * resizing the array as necessary. * @return void */ public void insertAtEnd(Customer newitem) { if (numCustomers >= customerArray.length) doubleLength(); customerArray[numCustomers] = newitem; numCustomers++; } /** * Changes the array to have twice as many available cells as it has now. * All references in the original array are moved to the new array. * * @return void */ private void doubleLength() { Customer[] newarray = new Customer[customerArray.length * 2]; for (int i = 0; i < numCustomers; i++) newarray[i] = customerArray[i]; customerArray = newarray; } /** * Returns a string representing the entire contents of the collection * @return this collection's string representation */ public String toString() { String result = " CustomerCollection[ " + "numCustomers=" + numCustomers + "/length=" + customerArray.length; for (int i = 0; i < numCustomers; i++) result += "\n customer["+i+"]= " + customerArray[i]; result += "\n ]"; return result; } }