diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf8a19c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# CustomerGUI +Reads a text file with which it extracts the content from a file and it allows the enduser to see the records. +The end user is also able to search for people within the gui by first name, last name or by email address. diff --git a/com/Customer/CustomerDatabase.java b/com/Customer/CustomerDatabase.java index 8ddcd34..e7b25cf 100644 --- a/com/Customer/CustomerDatabase.java +++ b/com/Customer/CustomerDatabase.java @@ -1,10 +1,11 @@ package com.Customer; -import java.io.FileInputStream; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; +import java.util.stream.Collectors; public class CustomerDatabase { // FIX ME: define your private ArrayList of customers here. @@ -12,9 +13,14 @@ public class CustomerDatabase { public CustomerDatabase() { customerList = new ArrayList(); - } + /** + * Find a particular customer based on the first and last name + * @param firstName search for + * @param lastName to search + * @return + */ public Customer findCustomer(String firstName, String lastName) { // FIX ME: iterate through your ArrayList of customers and return the // one Customer whose firstName and lastName match the input parameters, or @@ -26,39 +32,48 @@ public Customer findCustomer(String firstName, String lastName) { } return null; } + + /** + * Find a customer based on the email domain + * @param domain to search for + * @return + */ public ArrayList findCustomersWithSameEmailDomain(String domain) { - // FIX ME: iterate through your ArrayList of customers and return all those whose - // have the same domain. For example, if domain is @google, you should return an - // ArrayList of all the Customers that contain @google in the email - ArrayList list = new ArrayList(); - for (Customer x: customerList) { - if (x.getEmail().contains(domain)) { - list.add(x); - } - } + // Replaced this with a stream of the collection to make the code cleaner + ArrayList list = (ArrayList) customerList.stream().filter(s -> s.getEmail().contains(domain)).collect(Collectors.toList()); return list; } + + /** + * Gets all the customers in the customers list + * @return + */ public ArrayList getDB() { - // FIX ME: return the ArrayList of customers. - // ONE line of code return customerList; - } + /** + * Gets the total count of customer in the database + * @return + */ public int getNumberCustomers () { // FIX ME: return the number of customers in the // ArrayList - ONE line of code return customerList.size(); } + /** + * Reads the customer data file and add its to the arraylist for later use + * @param filename to look at to retrieve information + */ public void readCustomerData(String filename) { // Read the full set of data from a text file - try{ + try { // open the text file and use a Scanner to read the text - FileInputStream fileByteStream = new FileInputStream("src/" + filename); - Scanner scnr = new Scanner(fileByteStream); + // Changed this into one line rather than 2 + Scanner scnr = new Scanner(new File(filename)); scnr.useDelimiter("[,\r\n]+"); // keep reading as long as there is more data @@ -76,9 +91,9 @@ public void readCustomerData(String filename) { customerList.add(c); } - fileByteStream.close(); + scnr.close(); } - catch(IOException e) { + catch(FileNotFoundException e) { System.out.println("Failed to read the data file: " + filename); } } @@ -110,12 +125,10 @@ public static void main(String[] args) { System.out.println("\nFinding all customers who have a google email account" + "\n======================================================="); ArrayList domainCustomers = - customers.findCustomersWithSameEmailDomain("@google") ; + customers.findCustomersWithSameEmailDomain("@deviantart") ; System.out.println("Found " + domainCustomers.size() + " records total:"); - - for(Customer c : domainCustomers) { - System.out.println(c); - } + // Used a lambda expression again to make cleaner + domainCustomers.forEach(s -> System.out.println(s)); } diff --git a/com/Customer/CustomerGUI.java b/com/Customer/CustomerGUI.java index a139038..218bcb4 100644 --- a/com/Customer/CustomerGUI.java +++ b/com/Customer/CustomerGUI.java @@ -1,13 +1,10 @@ package com.Customer; -import com.Customer.CustomerDatabase; - import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import java.io.*; -import java.text.*; /*********************************************************************** * GUI front end for lab10 - A customer database @@ -50,7 +47,6 @@ public static void main(String arg[]){ gui.setTitle("Customer Database"); gui.pack(); gui.setVisible(true); - } /********************************************************************* @@ -144,7 +140,10 @@ public CustomerGUI(){ private void displayCustomers(ArrayList list){ resultsArea.setText(""); for(Customer c : list){ - resultsArea.append("\n" + c.toString()); + //resultsArea.append("\n" + c.toString()); + // Its easier to read this way, probably best to edit the toString() to this format so that + // its cleaner to use just toString() rather than the below + resultsArea.append(c.getFirstName() + ", " + c.getLastName() + ", " + c.getEmail() + "\n"); } resultsArea.append ("\nNumber of Customers: " + list.size()); }