Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 37 additions & 24 deletions com/Customer/CustomerDatabase.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
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.
private ArrayList<Customer> customerList;

public CustomerDatabase() {
customerList = new ArrayList<Customer>();

}

/**
* 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
Expand All @@ -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<Customer> 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<Customer> list = new ArrayList<Customer>();
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<Customer> list = (ArrayList<Customer>) customerList.stream().filter(s -> s.getEmail().contains(domain)).collect(Collectors.toList());
return list;
}

/**
* Gets all the customers in the customers list
* @return
*/
public ArrayList<Customer> 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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -110,12 +125,10 @@ public static void main(String[] args) {
System.out.println("\nFinding all customers who have a google email account" +
"\n=======================================================");
ArrayList<Customer> 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));


}
Expand Down
9 changes: 4 additions & 5 deletions com/Customer/CustomerGUI.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,7 +47,6 @@ public static void main(String arg[]){
gui.setTitle("Customer Database");
gui.pack();
gui.setVisible(true);

}

/*********************************************************************
Expand Down Expand Up @@ -144,7 +140,10 @@ public CustomerGUI(){
private void displayCustomers(ArrayList <Customer> 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());
}
Expand Down