Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Initializes the program and acts as the UI (Every member on Team 10)
- import java.util.Scanner;
- public class Main {
- public static void main(String args[]) throws Exception {
- Scanner scan = new Scanner(System.in);
- ContactList clist = new ContactList(); // new ContactList object clist
- clist.init(); // initializes the program
- System.out.println("Welcome to Team 10's Contact List");
- int programRunning = 1;
- while (programRunning == 1) { // while the program is still running
- System.out.println("");
- System.out.println("");
- System.out.println("What do you wish to do?");
- System.out.println("1. Add a new Contact ");
- System.out.println("2. Print the Contact List");
- System.out
- .println("3. Search for a Contact by Last Name, Email, or Zipcode");
- System.out.println("4. Delete a Contact");
- System.out.println("5. Quit and Save the new Contact");
- String choice = (String) scan.nextLine();
- int number = 0;
- //
- // parses the user input (string) into an int
- //
- try {
- number = Integer.parseInt(choice);
- }
- //
- // if the string cannot be parsed into an int, displays a message
- // saying so
- //
- catch (NumberFormatException nfe) {
- System.out.println("I don't know how to " + choice);
- continue;
- }
- switch (number) {
- case 1:
- clist.addContact();
- clist.writeLinesToFile("contacts.txt", false);
- clist.readContactsFromFile();
- break;
- case 2:
- clist.sortLastName();
- clist.printContactList();
- break;
- case 3:
- clist.search();
- break;
- case 4:
- clist.delete();
- clist.writeLinesToFile("contacts.txt", false);
- clist.readContactsFromFile();
- break;
- case 5:
- clist.writeLinesToFile("contacts.txt", false);
- System.out.println("See you soon. :)");
- programRunning = 0;
- break;
- default:
- System.out.println("I don't know how to " + number);
- break;
- }
- }
- }
- }
- // USE CASE 1: ENTER NEW CONTACT
- // Reading in Contacts from File...
- // EOF: Finished reading in Contacts
- // Welcome to Team 10's Contact List
- //
- //
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 1
- // Please enter the Contact information.
- // What is your First name? Sarah
- // What is your Last name? (Required)
- // What is your Last name? (Required)Port
- // What is your Email? (xxxx@xxx.com)email
- // What is your Email? (xxxx@xxx.com)website.com
- // What is your Email? (xxxx@xxx.com)email@website
- // What is your Email? (xxxx@xxx.com)email@website.com
- // What is your Phone Number? (xxx-xxx-xxxx)1234567899
- // What is your Phone Number? (xxx-xxx-xxxx)123-456-7899
- // What is your street address?
- // What city do you live in?
- // What State do you live in?
- // What is your Zip Code? 94040
- // Any notes?
- // Are you sure you want to enter the following information?
- // Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // Y/N: y
- // Entering new Contact:
- // Writing Contacts to file...
- // Reading in Contacts from File...
- // EOF: Finished reading in Contacts
- // USE CASE 2: PRINT ENTIRE CONTACT LIST SORTED BY LAST NAME
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 2
- // Printing Contact List
- // ===========================
- // Name: joe atom
- // Email:
- // Phone:
- // Address:
- // Notes: no notes
- // ===========================
- // Name: Wilson Ng
- // Email: hisemail@derp.com
- // Phone:
- // Address: 123 Sesame St.
- // Mountain View Ca 94040
- // Notes: nope
- // ===========================
- // Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View Ca 94040
- // Notes: no notes
- // ===========================
- // Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View CA 94040
- // Notes: Nothing to see here folks.
- // ===========================
- // Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // USE CASE 3: SEARCH BY LAST NAME
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 3
- // Please type the last name, email, or zip code you are searching for: port
- //
- // 1: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View Ca 94040
- // Notes: no notes
- //
- // 2: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View CA 94040
- // Notes: Nothing to see here folks.
- //
- // 3: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // USE CASE 4: SEARCH BY EMAIL
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 3
- // Please type the last name, email, or zip code you are searching for:
- // email@website.com
- //
- // 1: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View Ca 94040
- // Notes: no notes
- //
- // 2: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View CA 94040
- // Notes: Nothing to see here folks.
- //
- // 3: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // USE CASE 5: SEARCH BY ZIP CODE
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 3
- // Please type the last name, email, or zip code you are searching for: 94040
- //
- // 1: Name: Wilson Ng
- // Email: hisemail@derp.com
- // Phone:
- // Address: 123 Sesame St.
- // Mountain View Ca 94040
- // Notes: nope
- //
- // 2: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View Ca 94040
- // Notes: no notes
- //
- // 3: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View CA 94040
- // Notes: Nothing to see here folks.
- //
- // 4: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // CONDITION FOR USE CASES 3-5: SEARCH NOT FOUND
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 3
- // Please type the last name, email, or zip code you are searching for:
- // nonexistant search term
- // No matches found for: nonexistant search term
- // USE CASE 6: DELETE A CONTACT
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 4
- // Please type the first and last name of the contact you would like to delete:
- // First Name: sarah
- // Last Name: port
- //
- // 1: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View Ca 94040
- // Notes: no notes
- // Are you sure you'd like to delete Sarah Port? Y/N
- // n
- // SarahPort was not deleted.
- //
- // 2: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address: 217 College St.
- // Mountain View CA 94040
- // Notes: Nothing to see here folks.
- // Are you sure you'd like to delete Sarah Port? Y/N
- // y
- //
- // 3: Name: Sarah Port
- // Email: email@website.com
- // Phone: 123-456-7899
- // Address:
- // 94040
- // Notes:
- // Are you sure you'd like to delete Sarah Port? Y/N
- // y
- // Writing Contacts to file...
- // Reading in Contacts from File...
- // EOF: Finished reading in Contacts
- //
- //
- // What do you wish to do?
- // 1. Add a new Contact
- // 2. Print the Contact List
- // 3. Search for a Contact by Last Name, Email, or Zipcode
- // 4. Delete a Contact
- // 5. Quit and Save the new Contact
- // 5
- // Writing Contacts to file...
- // See you soon. :)
- //CONTACTLIST
- import java.io.EOFException;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- //
- //Controller class. Has all the verbs of the program, and defines the array of contacts. (everyone)
- //
- public class ContactList {
- private static final long serialVersionUID = 1L;
- static final String STORE_FILE = "contacts.txt";
- private static final int LIMIT = 100; // static because each instance doesn't have to have a different value.
- Contact[] list; // array that gets and stores all the contact objects
- private int listSpot = 0; // holds the next empty spot in the array
- //
- // This method sets the limit of the list with LIMIT values of type integer.
- //
- public ContactList() {
- list = new Contact[LIMIT];
- }
- //
- // This method adds a new contact object to the array list.
- //
- public void addContact() {
- if (listSpot <= LIMIT) {
- Contact newContact = new Contact(); // defines the array
- Scanner scanner = new Scanner(System.in);
- System.out.println("Please enter the Contact information.");
- String input = "";
- System.out.print("What is your First name? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setFirstName(input);
- while (null == newContact.getLastName() || newContact.getLastName().isEmpty()) {
- System.out.print("What is your Last name? (Required)");
- input = scanner.nextLine();
- newContact.setLastName(input);
- }
- boolean emailValid = false;
- String tempEmail = "";
- while (!emailValid) {
- System.out.print("What is your Email? (xxxx@xxx.com)");
- tempEmail = scanner.nextLine();
- emailValid = testEmail(tempEmail);
- }
- if (null != tempEmail)
- newContact.setEmail(tempEmail);
- boolean phoneValid = false;
- String tempPhone = "";
- while (!phoneValid) {
- System.out.print("What is your Phone Number? (xxx-xxx-xxxx)");
- tempPhone = scanner.nextLine();
- phoneValid = testPhone(tempPhone);
- }
- if (null != tempPhone)
- newContact.setPhone(tempPhone);
- System.out.print("What is your street address? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setStreet(input);
- System.out.print("What city do you live in? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setCity(input);
- System.out.print("What State do you live in? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setState(input);
- System.out.print("What is your Zip Code? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setZipCode(input);
- System.out.print("Any notes? ");
- input = scanner.nextLine();
- if (null != input)
- newContact.setNotes(input);
- System.out
- .println("Are you sure you want to enter the following information?");
- newContact.printContact();
- String choice;
- System.out.print("Y/N: ");
- choice = scanner.nextLine();
- if (choice.equalsIgnoreCase("Y")) {
- System.out.println("Entering new Contact: ");
- // Enter Contact into ContactList only after fields have been validated (i.e. lastName is not Null, phone number format, email format)
- list[listSpot] = newContact;
- listSpot++;
- }
- }
- }
- //
- //tests the syntax of the tempEmail. If it is correct it returns true, if it is incorrect it returns false.
- //
- private boolean testEmail(String tempEmail) {
- // Test if email is in the form name@domain. Also check there is only one @ in the String.
- int firstAt = tempEmail.indexOf("@");
- int lastAt = tempEmail.lastIndexOf("@");
- int com = tempEmail.indexOf(".com");
- if (null == tempEmail || tempEmail.isEmpty()) {
- return true;
- } else if (firstAt > 0 && lastAt < tempEmail.length() - 1
- && firstAt == lastAt && com > 0) {
- return true;
- }
- return false;
- }
- //
- //tests the syntax of the tempPhone number. If it is correct it returns true, if it is incorrect it returns false.
- //
- private boolean testPhone(String tempPhone) {
- int firstDash = tempPhone.indexOf("-");
- int secondDash = tempPhone.lastIndexOf("-");
- int phoneLength = tempPhone.length();
- if (null == tempPhone || tempPhone.isEmpty()) {
- return true;
- } else if (firstDash == 3 && secondDash == 7 && phoneLength == 12) {
- return true;
- }
- return false;
- }
- //
- // This method prints ContactsList list.
- //
- public void printContactList() {
- System.out.println("Printing Contact List");
- for (int i = 0; i < listSpot; i++) {
- if (list[i] != null) { // print list[i] only if list[i] IS NOT(!=)
- // null
- System.out.println("===========================");
- list[i].printContact();
- }
- }
- }
- //
- // method deletes contacts
- //
- public void delete() {
- String searchFirst;
- String searchLast;
- System.out.println("Please type the first and last name of the contact you would like to delete: ");
- Scanner scan = new Scanner(System.in);
- System.out.print("First Name: ");
- searchFirst = scan.nextLine();
- System.out.print("Last Name: ");
- searchLast = scan.nextLine();
- int e = 0;
- int count = 0;
- while (e < listSpot) {
- if (searchFirst.equalsIgnoreCase(list[e].getFirstName())
- && searchLast.equalsIgnoreCase(list[e].getLastName())) {
- System.out.print("\n" + ++count + ": ");
- list[e].printContact();
- System.out.println("Are you sure you'd like to delete "
- + list[e].getFirstName() + " " + list[e].getLastName()
- + "? Y/N");
- String delete;
- delete = scan.nextLine();
- if (delete.equalsIgnoreCase("y")) {
- list[e] = null;
- } else {
- System.out.println(list[e].getFirstName()
- + list[e].getLastName() + " was not deleted.");
- }
- }
- e++;
- }
- if (count < 1) {
- System.out.println("No matches found for: " + searchFirst + searchLast);
- }
- }
- //
- // This method searches by Last Name, Email, or Zip Code. if found it prints the contact's information searched or prints "not found". (WNG)
- //
- public void search() {
- String search;
- System.out
- .print("Please type the last name, email, or zip code you are searching for: ");
- Scanner scan = new Scanner(System.in);
- search = scan.nextLine();
- int e = 0;
- int count = 0;
- while (e < listSpot) {
- if (search.equalsIgnoreCase(list[e].getLastName())
- || search.equalsIgnoreCase(list[e].getEmail())
- || search.equalsIgnoreCase(list[e].getZipCode())) {
- System.out.print("\n" + ++count + ": ");
- list[e].printContact();
- }
- e++;
- }
- if (count < 1) {
- System.out.println("No matches found for: " + search);
- }
- }
- //
- // This method sorts the array by Last names (WNG)
- //
- public void sortLastName() {
- int in;
- int out;
- for (out = 1; out < listSpot; out++) {
- Contact temp = list[out];
- in = out;
- while (in > 0 && list[in - 1].getLastName().toLowerCase().compareTo(temp.getLastName().toLowerCase()) > 0) {
- list[in] = list[in - 1]; // shift item to the right
- --in; // go left one position
- }
- list[in] = temp;
- }
- }
- //
- // This method writes and saves the information into a text file to disk.
- //
- public void writeLinesToFile(String filePath, boolean appendToFile) {
- FileOutputStream outFileStream;
- ObjectOutputStream outObjectStream;
- try {
- outFileStream = new FileOutputStream(filePath);
- outObjectStream = new ObjectOutputStream(outFileStream);
- for (int i = 0; i < listSpot; i++) {
- outObjectStream.writeObject(list[i]);
- }
- outObjectStream.flush();
- outObjectStream.close();
- outFileStream.close();
- System.err.println("Writing Contacts to file...");
- } catch (IOException ioe) {
- System.out.println("Error writing objects to the file: "
- + ioe.getMessage());
- ioe.printStackTrace();
- }
- }
- //
- // This method reads the saved file into the array Contact[] list
- //
- public void readContactsFromFile() throws Exception {
- FileInputStream inFileStream = null;
- ObjectInputStream inObjectStream = null;
- System.err.println("Reading in Contacts from File...");
- try {
- inFileStream = new FileInputStream("contacts.txt");
- inObjectStream = new ObjectInputStream(inFileStream);
- // Reset listSpot to read in Contacts
- listSpot = 0;
- // Read in each Contact from the store file and assign to list
- // Contact[].
- // Assign current read Contact position to listSpot.
- boolean keepGoing = true;
- while (keepGoing) {
- Contact readContact = (Contact) inObjectStream.readObject();
- if (null == readContact) {
- } else {
- list[listSpot] = readContact;
- listSpot++;
- }
- }
- } catch (EOFException eof) {
- System.err.println("EOF: Finished reading in Contacts");
- } catch (IOException ioe) {
- System.err.println("Error reading from the file: "
- + ioe.getMessage());
- } catch (ClassNotFoundException cnfe) {
- System.err.println("Error in casting to Object: " + cnfe);
- } finally {
- if (inFileStream != null)
- inFileStream.close();
- if (inObjectStream != null)
- inObjectStream.close();
- }
- }
- //
- //initiates the program.
- //
- public void init() {
- try {
- readContactsFromFile();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //CONTACT
- //Creates contacts's information with first name, last name, phone number, email, and home address. (everyone)
- import java.io.Serializable;
- //
- //model class
- //
- public class Contact implements Serializable {
- private static final long serialVersionUID = 1L;
- private String firstName;
- private String lastName;
- private String email;
- private String phone;
- private String street;
- private String city;
- private String state;
- private String zipCode;
- private String notes;
- //
- //the following set methods sets the local variables for the current instance in the array.
- //
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public void setStreet(String street) {
- this.street = street;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public void setState(String state) {
- this.state = state;
- }
- public void setZipCode(String zipCode) {
- this.zipCode = zipCode;
- }
- public void setNotes(String notes) {
- this.notes = notes;
- }
- public String getFirstName() {
- return firstName;
- }
- //
- //the following get methods return the local variables for use outside of this class.
- //
- public String getLastName() {
- return lastName;
- }
- // This method gets the values for email
- public String getEmail() {
- return email;
- }
- // This method gets the values for phone
- public String getPhone() {
- return phone;
- }
- // This method gets the values for street
- public String getStreet() {
- return street;
- }
- // This method gets the values for city
- public String getCity() {
- return city;
- }
- // This method gets the values for state
- public String getState() {
- return state;
- }
- // This method gets the values for zipCode
- public String getZipCode() {
- return zipCode;
- }
- // This method gets the values for notes
- public String getNotes() {
- return notes;
- }
- // This method prints the all the Contact's information
- public void printContact() {
- System.out.println("Name: " + firstName + " " + lastName);
- System.out.println("Email: " + email);
- System.out.println("Phone: " + phone);
- if (street.length() == 0 && city.length() == 0 && state.length() == 0
- && zipCode.length() == 0) { // If all address information is missing, the punctuation will not be printed.
- System.out.println("Address:");
- } else {
- System.out.println("Address: " + street + "\n" + " " + city
- + " " + state + " " + zipCode);
- }
- System.out.println("Notes: " + notes);
- }
- }
Add Comment
Please, Sign In to add comment