Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class Store { public static final int MAX_CUSTOMERS = 500; public static int LATEST_ID = 1000000; private String name ; private Customer[] customers ;
public class Store { public static final int MAX_CUSTOMERS = 500; public static int LATEST_ID = 1000000; private String name; private Customer[] customers; private int customerCount; public Store(String n) { name = n; customers = new Customer[MAX_CUSTOMERS]; customerCount = 0; } public void addCustomer(Customer c) { if (customerCount MAX_CUSTOMERS) { customers[customerCount++] = c; c.setId(LATEST_ID++); } } public Customer[] getCustomers() { return customers; } public int getCustomerCount() { return customerCount; } public void listCustomers() { for (int i=0; icustomerCount; i++) System.out.println(customers[i]); } public int averageCustomerAge() { int avg = 0; for (int i=0; icustomerCount; i++) { avg += customers[i].getAge(); } return avg / customerCount; } public char mostPopularGender() { int mCount = 0, fCount = 0; for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == 'M') mCount++; else fCount++; } if (mCount > fCount) return 'M'; return 'F'; } public Customer richestCustomer() { Customer richest = customers[0]; for (int i=1; icustomerCount; i++) { if (customers[i].hasMoreMoneyThan(richest)) richest = customers[i]; } return richest; } public Customer[] getCustomersWithGender(char g) { int matchCount = 0; // Count them first for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == g) matchCount++; } // Now make the array Customer[] matches = new Customer[matchCount]; matchCount = 0; for (int i=0; icustomerCount; i++) { if (customers[i].getGender() == g) matches[matchCount++] = customers[i]; } return matches; } public Customer[] friendsFor(Customer lonelyCustomer) { int friendCount = 0; // Count them first for (int i=0; icustomerCount; i++) { if ((customers[i] != lonelyCustomer) && (customers[i].getGender() == lonelyCustomer.getGender()) && (Math.abs(customers[i].getAge() - lonelyCustomer.getAge()) // Now make the array Customer[] friends = new Customer[friendCount]; friendCount = 0; for (int i=0; icustomerCount; i++) { if ((customers[i] != lonelyCustomer) && (customers[i].getGender() == lonelyCustomer.getGender()) && (Math.abs(customers[i].getAge() - lonelyCustomer.getAge()) customers[i]; } } return friends; } }
public class Customer { private int id; private String name; private int age; private char gender; private float money; // A simple constructor public Customer(String n, int a, char g, float m) { name = n; age = a; gender = g; money = m; id = -1; } // Return a String representation of the object public String toString() { String result; result = "Customer " + name + ": a " + age + " year old "; if (gender == 'F') result += "fe"; return result + "male with $" + money; } public boolean hasMoreMoneyThan(Customer c) { return money > c.money; } // Get methods public String getName() { return name; } public int getAge() { return age; } public char getGender() { return gender; } public int getId() { return id; } // Set Methods public void setId(int newID) { id = newID; } }
public class StoreTestProgram { public static void main(String args[]) { Customer[] result; Store walmart; walmart = new Store("Walmart off Innes"); walmart.addCustomer(new Customer("Amie", 14, 'F', 100)); walmart.addCustomer(new Customer("Brad", 15, 'M', 0)); walmart.addCustomer(new Customer("Cory", 10, 'M', 100)); walmart.addCustomer(new Customer("Dave", 5, 'M', 48)); walmart.addCustomer(new Customer("Earl", 21, 'M', 500)); walmart.addCustomer(new Customer("Flem", 18, 'M', 1)); walmart.addCustomer(new Customer("Gary", 8, 'M', 20)); walmart.addCustomer(new Customer("Hugh", 65, 'M', 30)); walmart.addCustomer(new Customer("Iggy", 43, 'M', 74)); walmart.addCustomer(new Customer("Joan", 55, 'F', 32)); walmart.addCustomer(new Customer("Kyle", 16, 'M', 88)); walmart.addCustomer(new Customer("Lore", 12, 'F', 1000)); walmart.addCustomer(new Customer("Mary", 17, 'F', 6)); walmart.addCustomer(new Customer("Nick", 13, 'M', 2)); walmart.addCustomer(new Customer("Omar", 18, 'M', 24)); walmart.addCustomer(new Customer("Patt", 24, 'F', 45)); walmart.addCustomer(new Customer("Quin", 42, 'M', 355)); walmart.addCustomer(new Customer("Ruth", 45, 'F', 119)); walmart.addCustomer(new Customer("Snow", 74, 'F', 20)); walmart.addCustomer(new Customer("Tamy", 88, 'F', 25)); walmart.addCustomer(new Customer("Ulsa", 2, 'F', 75)); walmart.addCustomer(new Customer("Vern", 9, 'M', 90)); walmart.addCustomer(new Customer("Will", 11, 'M', 220)); walmart.addCustomer(new Customer("Xeon", 17, 'F', 453)); walmart.addCustomer(new Customer("Ying", 19, 'F', 76)); walmart.addCustomer(new Customer("Zack", 22, 'M', 35)); System.out.println("Here are the customers: "); walmart.listCustomers(); // Find average Customer age System.out.println(" Average age of customers: " + walmart.averageCustomerAge()); // Find most popular gender System.out.println(" Most popular gender is: " + walmart.mostPopularGender()); // Find richest customer System.out.println(" Richest customer is: " + walmart.richestCustomer()); // Find male customers System.out.println(" Here are all the male customers:"); result = walmart.getCustomersWithGender('M'); for (Customer c: result) System.out.println(c); // Find female customers System.out.println(" Here are all the female customers:"); result = walmart.getCustomersWithGender('F'); for (Customer c: result) System.out.println(c); // Find friends for Amie System.out.println(" Friends for 14 year old female Amie:"); result = walmart.friendsFor(walmart.getCustomers()[0]); for (Customer c: result) System.out.println(c); // Find friends for Brad System.out.println(" Friends for 15 year old male Brad:"); result = walmart.friendsFor(walmart.getCustomers()[1]); for (Customer c: result) System.out.println(c); } }Au Document1 - Word Picture Tools Sign in -0 File Home Insert Design Layout References Mailings Review View Help FormatTell me what you want to do Share Cut ?Copy Find ece Paste Blu, abEX, X, A-2-a- ,3.0, TAlommallTNoSpac Heading 1 Heading 2 Title Subtitle Subtle Erm Emphasis Select Clipboard Font Paragraph Styles Editing 5) We will now switch over to our Customer/Store example. Run the StoreTestProgram to make sure that it works. Also, create, save and compile the following test program: public class CustonerTestProgram ( public static void main IString args[) Customer ci new Customer "Amie", 14, F,100) Customer c2 -new Customer ("Brad", 15, 'N,0) Recall that we can create a DataOutputStream that allows us to output primitive types to a file as follows: Dataoutputstream out new Datadutputstrean (new FileoutputStream"customerl.txt")) out.writeUF(e1.getName )) 7 USF is short or "Unicode Text Format" out.writelnt (cl.getAge() out.close Add such code to the CustomerTestProgram so that the first customer Amie is saved to a file called customer1.txt and Brad is saved to a file called customer2.txt. You will need to write all 5 attributes for each person. This will require you to add a getMoney0 method in the Customer class. You will need to import java.io.; and don't forget to close the files when you are done using the close) method. The code that you write, may not compile likely, you will get some errors indicating that you need to handle java.io.FileNotFoundException and java.io.IOException. Recall that we can handle these using try/catch blocks. Place your file-related code in a try block and catch the exceptions, doing nothing for now when they are caught try i ) catch (FileNotFoundException e) ) catchI0Exception e) Weite your code here Do Nothing Do Nothing Page 1 of 5 0 words + 100% Au Document1 - Word Picture Tools Sign in -0 File Home Insert Design Layout References Mailings Review View Help FormatTell me what you want to do Share Cut ?Copy Find ece Paste Blu, abEX, X, A-2-a- ,3.0, TAlommallTNoSpac Heading 1 Heading 2 Title Subtitle Subtle Erm Emphasis Select Clipboard Font Paragraph Styles Editing 5) We will now switch over to our Customer/Store example. Run the StoreTestProgram to make sure that it works. Also, create, save and compile the following test program: public class CustonerTestProgram ( public static void main IString args[) Customer ci new Customer "Amie", 14, F,100) Customer c2 -new Customer ("Brad", 15, 'N,0) Recall that we can create a DataOutputStream that allows us to output primitive types to a file as follows: Dataoutputstream out new Datadutputstrean (new FileoutputStream"customerl.txt")) out.writeUF(e1.getName )) 7 USF is short or "Unicode Text Format" out.writelnt (cl.getAge() out.close Add such code to the CustomerTestProgram so that the first customer Amie is saved to a file called customer1.txt and Brad is saved to a file called customer2.txt. You will need to write all 5 attributes for each person. This will require you to add a getMoney0 method in the Customer class. You will need to import java.io.; and don't forget to close the files when you are done using the close) method. The code that you write, may not compile likely, you will get some errors indicating that you need to handle java.io.FileNotFoundException and java.io.IOException. Recall that we can handle these using try/catch blocks. Place your file-related code in a try block and catch the exceptions, doing nothing for now when they are caught try i ) catch (FileNotFoundException e) ) catchI0Exception e) Weite your code here Do Nothing Do Nothing Page 1 of 5 0 words + 100%
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started