Guest User

Untitled

a guest
Apr 16th, 2018
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. //Test class ContactList and Contact by printing the values inside the object (Every member on Team 10)
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     public static void main(String args[]) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String add; // for test purposes only
  8.         add = "Y"; // for test purposes only
  9.         ContactList list = new ContactList();
  10.         while (add.equals("y") || add.equals("Y")) { // tests that multiple users may be entered
  11.             list.addContact(); // calls add contact method
  12.             System.out.println("Want to add another contact? Y/N:"); // for test purposes
  13.             add = scan.next();
  14.         }
  15.         list.printContactList(); // prints contact list, for test purposes
  16.  
  17.     }
  18. }
  19.  
  20. // This is the test run. The "What to add a contact" loop is present only to
  21. // test that multiple objects may be added.
  22. // In this test run we tested first a full contact object, then we tested an
  23. // object missing last name, which was not added to the contact list.
  24. // Then we tested two objects with some missing fields but with a last name,
  25. // both of which were added to the contact list and printed.
  26. /*
  27.  * Want to add another contact? Y/N: y Want to add another contact? Y/N: y Want
  28.  * to add another contact? Y/N: y Want to add another contact? Y/N: n Name:
  29.  * Sarah Port Email: sarahe.port@gmail.com Phone: 123-456-7899 Address: 217
  30.  * College St., Mountain View, CA 94040 Notes: Hello! I'm awesome. Name:
  31.  * Yasukazu Kashii Email: Phone: Address: Notes: Most of these fields will just
  32.  * show up blank, since they were left blank. BUT he is still entered because
  33.  * his last name was present. Name: Wilson Ng Email: Phone: 123-456-7899
  34.  * Address: Notes: He thinks he's unnecessary in this test. HAH!
  35.  */
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //Creates contacts's information with first name, last name, phone number, email, and home address. (Wilson Ng)
  43. import javax.swing.JOptionPane;
  44.  
  45. public class Contact {
  46.     boolean testLast;
  47.     private String firstName;
  48.     private String lastName;
  49.     private String email;
  50.     private String phone;
  51.     private String street;
  52.     private String city;
  53.     private String state;
  54.     private String zipCode;
  55.     private String notes;
  56.  
  57.     // This method sets the values for Contact
  58.     public void setContact() {
  59.         firstName = JOptionPane.showInputDialog("What is your First name?");
  60.         lastName = JOptionPane.showInputDialog("What is your Last name?");
  61.         email = JOptionPane.showInputDialog("What is your Email?");
  62.         phone = JOptionPane.showInputDialog("What is your Phone Number?");
  63.         street = JOptionPane.showInputDialog("What is your street address?");
  64.         city = JOptionPane.showInputDialog("What city do you live in?");
  65.         state = JOptionPane.showInputDialog("What State do you live in?");
  66.         zipCode = JOptionPane.showInputDialog("What is your Zip Code?");
  67.         notes = JOptionPane.showInputDialog("Any notes?");
  68.     }
  69.    
  70.     public void testLastName() {
  71.        
  72.         if (lastName != null
  73.                 && lastName.length() == 0) { // tests if last name is present, deletes the object if it is not.
  74.             testLast = false;
  75.         } else {
  76.             testLast = true;
  77.         } // moves to next spot in array if last name is present.
  78.     }
  79.  
  80.     // This method prints the all the Contact's information
  81.     public void printContact() {
  82.         System.out.println("Name: " + firstName + " " + lastName);
  83.         System.out.println("Email: " + email);
  84.         System.out.println("Phone: " + phone);
  85.         if (street.length() == 0 && city.length() == 0 && state.length() == 0
  86.                 && zipCode.length() == 0) { // If all address information is missing, the punctuation will not be printed.
  87.             System.out.println("Address:");
  88.         } else {
  89.             System.out.println("Address: " + street + ", " + city + ", "
  90.                     + state + " " + zipCode);
  91.         }
  92.         System.out.println("Notes: " + notes);
  93.     }
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. //Define a list of contacts, where each list entry stores a new contact object. (Sarah Port)
  102. public class ContactList {
  103.  
  104.     private static final int LIMIT = 100; // static because each instance doesn't have to have a different value.
  105.     Contact[] list; // array that gets and stores all the contact objects
  106.     int listSpot = 0; // holds the next empty spot in the array
  107.  
  108.     // This method sets the limit of the list with LIMIT values of type integer.
  109.     public ContactList() {
  110.         list = new Contact[LIMIT];
  111.     }
  112.  
  113.     // This method adds a new contact object to the array list.
  114.     public void addContact() {
  115.         list[listSpot] = new Contact(); // defines the array
  116.         list[listSpot].setContact(); // gets new contact object
  117.         list[listSpot].testLastName();
  118.          if (list[listSpot].testLast == false){
  119.              list[listSpot] = null;
  120.         }
  121.          else{
  122.              listSpot ++;
  123.          }
  124.     }
  125.  
  126.     // This method prints ContactsList list. It is for test purposes.
  127.     public void printContactList() {
  128.         for (int i = 0; i < list.length; i++) {
  129.             if (list[i] != null) // print list[i] only if list[i] IS NOT(!=) null
  130.                 list[i].printContact();
  131.         }
  132.     }
  133. }
Add Comment
Please, Sign In to add comment