Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Test class ContactList and Contact by printing the values inside the object (Every member on Team 10)
- import java.util.Scanner;
- public class Main {
- public static void main(String args[]) {
- Scanner scan = new Scanner(System.in);
- String add; // for test purposes only
- add = "Y"; // for test purposes only
- ContactList list = new ContactList();
- while (add.equals("y") || add.equals("Y")) { // tests that multiple users may be entered
- list.addContact(); // calls add contact method
- System.out.println("Want to add another contact? Y/N:"); // for test purposes
- add = scan.next();
- }
- list.printContactList(); // prints contact list, for test purposes
- }
- }
- // This is the test run. The "What to add a contact" loop is present only to
- // test that multiple objects may be added.
- // In this test run we tested first a full contact object, then we tested an
- // object missing last name, which was not added to the contact list.
- // Then we tested two objects with some missing fields but with a last name,
- // both of which were added to the contact list and printed.
- /*
- * Want to add another contact? Y/N: y Want to add another contact? Y/N: y Want
- * to add another contact? Y/N: y Want to add another contact? Y/N: n Name:
- * Sarah Port Email: sarahe.port@gmail.com Phone: 123-456-7899 Address: 217
- * College St., Mountain View, CA 94040 Notes: Hello! I'm awesome. Name:
- * Yasukazu Kashii Email: Phone: Address: Notes: Most of these fields will just
- * show up blank, since they were left blank. BUT he is still entered because
- * his last name was present. Name: Wilson Ng Email: Phone: 123-456-7899
- * Address: Notes: He thinks he's unnecessary in this test. HAH!
- */
- //Creates contacts's information with first name, last name, phone number, email, and home address. (Wilson Ng)
- import javax.swing.JOptionPane;
- public class Contact {
- boolean testLast;
- 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;
- // This method sets the values for Contact
- public void setContact() {
- firstName = JOptionPane.showInputDialog("What is your First name?");
- lastName = JOptionPane.showInputDialog("What is your Last name?");
- email = JOptionPane.showInputDialog("What is your Email?");
- phone = JOptionPane.showInputDialog("What is your Phone Number?");
- street = JOptionPane.showInputDialog("What is your street address?");
- city = JOptionPane.showInputDialog("What city do you live in?");
- state = JOptionPane.showInputDialog("What State do you live in?");
- zipCode = JOptionPane.showInputDialog("What is your Zip Code?");
- notes = JOptionPane.showInputDialog("Any notes?");
- }
- public void testLastName() {
- if (lastName != null
- && lastName.length() == 0) { // tests if last name is present, deletes the object if it is not.
- testLast = false;
- } else {
- testLast = true;
- } // moves to next spot in array if last name is present.
- }
- // 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 + ", " + city + ", "
- + state + " " + zipCode);
- }
- System.out.println("Notes: " + notes);
- }
- }
- //Define a list of contacts, where each list entry stores a new contact object. (Sarah Port)
- public class ContactList {
- 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
- 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() {
- list[listSpot] = new Contact(); // defines the array
- list[listSpot].setContact(); // gets new contact object
- list[listSpot].testLastName();
- if (list[listSpot].testLast == false){
- list[listSpot] = null;
- }
- else{
- listSpot ++;
- }
- }
- // This method prints ContactsList list. It is for test purposes.
- public void printContactList() {
- for (int i = 0; i < list.length; i++) {
- if (list[i] != null) // print list[i] only if list[i] IS NOT(!=) null
- list[i].printContact();
- }
- }
- }
Add Comment
Please, Sign In to add comment