read attached pdf
CIS 1068 Lab Assignment 8.pdf CIS 1068 Lab Assignment 8 This assignment requires you to code a project that reads a file into an array of customers into a custom coded CustomerList. A sample input line CSV from the file Customers.csv is shown below. 34,2723,Waylon,Johnson,440 Richmond Park East,Hamden,CT,06517-2703 A sample Customer toString is below. CustomerID: 34 GrossSales: 2723.0 FirstName: Waylon LastName: Johnson Address: 440 Richmond Park East City: Hamden State: CT ZipCode: 06517-2703 The Person hierarchy The first two lines are fields from the Customer class. The remaining fields are from the Person class. The first task is to complete the Person hierarchy. Begin by writing the Person class because it is the superclass. The person class should have: 1. The six private fields (Give these the same name as above except begin with a lower case letter) 2. A parameterized constructor 3. Getters and setters for all fields 4. A toString() that prints field data as shown above 5. A toCSV() that returns a CSV string for the fields as shown above 6. A copy(p) method that accepts a Person p and copies all data to this 7. A copy(…) that accepts all field data as individual variables and copies them to this 8. A clone() that returns a Person with a copy if this data (Hint: instantiate a Person and copy this) Create a Customer class that inherits Person and implements Comparable. The Customer class should have: 1. The two private fields (give these the same name as above except begin with a lower case letter) 2. A parameterized constructor (Make sure to pass data to Person) 3. Getters and setters for all fields 4. A toString() that prints field data as shown above (Make sure to call Person to get its toString) 5. A toCSV() that returns a CSV string for the fields as shown above (Make sure to call Person to get its toCSV) 6. A copy(c) method that accepts a Customer c and copies all data to this (Make sure to call Person to copy its own data) 7. A copy(…) that accepts all field data and copies them to this (Make sure to call Person to copy its own data) 8. A clone() that returns a Customer with a copy if this data (Hint: instantiate a Customer and copy this) 9. A compareTo(c) that compares Customers by customerID a. If for a.compareTo(b), a.id < b.id,="" return="" -1="" b.="" else="" if="" for="" a.compareto(b),="" a.id=""> b.id, return 1 c. Else return 0 10. A copyCSV(s) that accepts a CSV string s shown above, splits the CSV on commas into an array of String and copies data to this (Hint: use the copy(…) method that accepts variables. The String split method splits on a delimiter. There’s an example at the end of the Ch 13 code) If you have completed all the required Person and Customer methods, add a protected default constructor to both. In addition to the default constructor, add another constructor to Customer that accepts a CSV String s and calls copyCSV(s) top copy the data to this. The CustomerList The next task is to code a CustomerList. You wil write this to mimic the ArrayList in Java. There are only two fields: an array of Customer and the size (the number of Customers in the array) 1. Code a reference to an array of Customer and the size 2. Code a default constructor that instantiates the array with a length of 4 3. A method named size() to return the size field 4. A method get(i) that accepts an integer i and returns the Customer at that array index (make sure the index is not out of bounds – return null if it is) 5. A method set(c, i) that accepts a Customer c and an integer i and sets array index i to c (returns true if successful and null if index is out of bounds) 6. An add(c) That accepts a Customer c and inserts it at the end of the array and returns nothing. The array should grow dynamically to accept as many Customers that are added. a. If the array length is equal to size, create a temp array that is double the size of the current array and copy all Customers to the temp array. Set the field array equal to the temp array. Automatic garbage collection will delete the old array b. Copy c to the field array and increment size 7. A remove(i) method that removes the Customer at array index i and returns the Customer removed. a. If index is out of bounds return null b. Start at the index i to be removed and copy the i+1 Customer to i, increment i while it is less than size 8. A toString() method that returns a String that contains the toString() for all Customers in the array and the sum of all grossSales added to the end of the String. 9. A read(fn) static method that accepts the fileName as a String, reads the data from the file to the CustomerList and returns the CustomerList. (Return null if read fails) a. In the try{} loop to read all Customers from the file, read a CSV String from the file, instantiate a Customer using the constructor that accepts a CSV String and add it to the CustomerList (Only 3 lines of code in the loop body) b. Remember that this is a static method that instantiates and returns a CustomerList 10. A write(cl, fn) static method that accepts a CustomerList cl and fileName fn and writes the CustomerList to a file. (return true for success and false for fail) a. In the try{}, loop through all Customers in the CustomerList cl calling the Customer’s toCSV() method to create the CSV String. (Only one line of code in the loop body) 11. A sort() method that sorts the CustomerList by customerID. (You can call Arrays sort because Customer has a compareTo) 12. An indexOf(id) That accepts a customerID and returns the index in the array if found and insert point if not. (You can copy and paste the iterative binary search in the Ch 13 code and modify it to work with a Customer) 13. An update(id, amt) method that accepts the customerID id and the amount to add to the Customer’s grossSales. (Hint: Use the indexOf to get the index. If the update is successful, id found, return true and if id does not exist, return false) Free stuff! Here’s a free main to test you project. public static void main(String[] args) throws FileNotFoundException { boolean b = false; // Read file System.out.println("Read file"); CustomerList cl = CustomerList.read("Customers.csv"); if(cl != null){ System.out.println("Read: " + cl.size() + " records"); System.out.println(cl.toString() + "\n\n\n"); } else{ System.out.println("File read error."); return; } // Test get and set for CustomerList System.out.println("Test get and set for CustomerList"); System.out.println("x = " + cl.get(0)); Customer c = cl.get(0); c.setFirstName("Homer"); cl.set(c, 0); System.out.println("x = " + cl.get(0)); System.out.println("\n\n\n"); // Test indexOf and update System.out.println("Test indexOf and update"); System.out.println("idx = " + cl.indexOf(34)); System.out.println("Customer with id 35 = \n" + cl.get(cl.indexOf(35))); // Update an existing Customer b = cl.update(35, 100.0); if(b) System.out.println("Update successful"); else System.out.println("Update not successful"); System.out.println("Customer with id 35 = \n" + cl.get(cl.indexOf(35))); cl.update(41, 150.0); cl.update(59, 200.0); cl.update(72, 250.0); // Update a non-existing Customer b = cl.update(350, 100.0); if(b) System.out.println("Update successful"); else System.out.println("Update not successful"); System.out.println("\n\n\n"); // Test remove