Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.15 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. public class Person implements Comparable{
  4.  
  5.     private static int nextId = 0;
  6.     private int id;
  7.    
  8.     private String fname;
  9.     private String lname;
  10.     private String sAddress;
  11.     private String city;
  12.     private String state;
  13.     private String zip;
  14.     private String phone;
  15.     private Date ModDay;
  16.    
  17.     //employee constructor
  18.     public Person(String newfName, String newlName, String newsAddress, String newcity, String newstate,
  19.                        String newzip, String newphone, Date newDay)
  20.     {
  21.         this.fname = newfName;
  22.         this.lname = newlName;
  23.         this.sAddress = newsAddress;
  24.         this.city = newcity;
  25.         this.state = newstate;
  26.         this.zip = newzip;
  27.         this.phone = newphone;
  28.         this.ModDay = newDay;
  29.        
  30.         this.id = ++nextId;
  31.     }
  32.     public String getfName() {
  33.         return fname;
  34.     }
  35.     public String getlName() {
  36.         return lname;
  37.     }
  38.     public String getsAddress() {
  39.         return sAddress;
  40.     }
  41.     public String getCity() {
  42.         return city;
  43.     }
  44.     public String getState() {
  45.         return state;
  46.     }
  47.     public String getZip() {
  48.         return zip;
  49.     }
  50.     public String getPhone() {
  51.         return phone;
  52.     }
  53.     public int getId() {
  54.         return id;
  55.     }
  56.     public Date getModDay() {
  57.         return ModDay;
  58.     }
  59.     //public void raiseSalary(double newValue){
  60.     //  salary = newValue;
  61.     //}
  62.     public void setfName(String newValue){
  63.         fname = newValue;
  64.     }
  65.     public void setlName(String newValue){
  66.         lname = newValue;
  67.     }
  68.     public void setsAddress(String newValue){
  69.         sAddress = newValue;
  70.     }
  71.     public void setCity(String newValue){
  72.         city = newValue;
  73.     }
  74.     public void setState(String newValue){
  75.         state = newValue;
  76.     }
  77.     public void setZip(String newValue){
  78.         zip = newValue;
  79.     }
  80.     public void setPhone(String newValue){
  81.         phone = newValue;
  82.     }
  83.     public void setModDay(Date newValue){
  84.         ModDay = newValue;
  85.     }
  86.     public boolean equals(Object o) {
  87.         if (!(o instanceof Person))
  88.             return false;
  89.         Person a = (Person)o;
  90.         return a.fname.equals(fname) &&
  91.                a.lname.equals(lname);
  92.     }
  93.     public int compareTo (Object other) {
  94.         int result;
  95.         if (lname.equals(((Person)other).lname)) {
  96.             result = fname.compareTo(((Person)other).fname);
  97.         }
  98.         else {
  99.             result = lname.compareTo(((Person)other).lname);
  100.         }
  101.         return result;
  102.     }
  103.     public String toString() {
  104.         //tostring method for array
  105.         String expectedPattern = "MM/dd/yyyy";
  106.         SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
  107.         //DecimalFormat form = new DecimalFormat("$##,###.00");
  108.         return "\nID#: " + id + "\nName: " + fname + " " + lname + "\nAddress: " + sAddress + " " + city + " " + state + " " + zip +
  109.                 "\nPhone: " + phone  + "\nModified Day: " + formatter.format(ModDay);
  110.     }
  111.  
  112.  
  113. }//end class
  114.  
  115.  
  116.  
  117. //import java.util.Scanner;
  118. import java.util.*;
  119. import java.text.DecimalFormat;
  120. import java.text.ParsePosition;
  121. import java.text.SimpleDateFormat;
  122.  
  123. public class AddressBook{
  124. //create array
  125.     private static ArrayList <Person> AddressBook = new ArrayList <Person>();
  126.     private static Scanner scanner = new Scanner(System.in);
  127.     private static String expectedPattern = "MM/dd/yyyy";
  128.     private static SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
  129.     private static Person atemp;
  130.         public static void main(String[] args) {
  131.         System.out.println("\tAddress Book\n");
  132.         System.out.println("List size is " + AddressBook.size());
  133.         //System.out.print("How many people to process?: ");
  134.         //size = Integer.parseInt(scanner.nextLine());
  135.         char choice = ' ';
  136.         char userChoice;
  137.         String choiceStr;
  138.         do {
  139.         System.out.println("\nAddress Book Menu");
  140.         System.out.print("\tEnter A to (A)dd Person\n" +
  141.                  "\tEnter D to (D)elete Person\n" +
  142.                  "\tEnter M to (M)odify Person\n" +
  143.                  "\tEnter S to (S)earch Person\n " +
  144.                  "\tEnter L to (L)ist All (sorted)\n" +
  145.                  "\tEnter Q to (Q)uit\n" +
  146.                  "Please enter your choice: ");
  147.         choiceStr = scanner.nextLine();
  148.         //assigns the first letter from the string
  149.         choice = choiceStr.charAt(0);
  150.         //converts (one) character to upper case character
  151.         userChoice = Character.toUpperCase(choice);
  152.         //user input validation
  153.         while (userChoice != 'A' && userChoice != 'D' &&
  154.            userChoice != 'M' && userChoice != 'S' &&
  155.            userChoice != 'L' && userChoice != 'Q') {
  156.         System.out.println("\nAddress Book Menu");
  157.         System.out.print("\tEnter A to (A)dd Person\n" +
  158.                               "\tEnter D to (D)elete Person\n" +
  159.                               "\tEnter M to (M)odify Person\n" +
  160.                               "\tEnter S to (S)earch Person\n " +
  161.                               "\tEnter L to (L)ist All (sorted)" +
  162.                               "\tEnter Q to (Q)uit\n" +
  163.                  "Invalid Choice! Please enter your choice: ");
  164.         choiceStr = scanner.nextLine();
  165.             choice = choiceStr.charAt(0);
  166.             userChoice = Character.toUpperCase(choice);
  167.         }
  168.         //determine user choice
  169.         switch (userChoice){
  170.         case 'A':
  171.             addPerson();
  172.             break;
  173.         case 'D':
  174.             deletePerson();
  175.             break;
  176.         case 'M':
  177.             modifyPerson();
  178.             break;
  179.         case 'S':
  180.         searchPerson();
  181.         break;
  182.         case 'L':
  183.         listPerson();
  184.         break;
  185.         case 'Q':
  186.         System.exit(0);
  187.         }
  188.         } while (userChoice != 'Q');
  189.     }//ends main
  190.     public static void searchPerson() {
  191.         System.out.print("\nEnter full name: ");
  192.         String pName = scanner.nextLine();
  193.         int locationIndex = findIndex(pName);
  194.         if (locationIndex == -1 ) {
  195.             System.out.println(pName + "not found");
  196.         }
  197.         else {
  198.             System.out.println(AddressBook.get(locationIndex));
  199.         }
  200.     }
  201.     public static void deletePerson() {
  202.         System.out.print("\nEnter full name: ");
  203.         String pName = scanner.nextLine();
  204.         int locationIndex = findIndex(pName);
  205.         if (locationIndex == -1 ) {
  206.             System.out.println(pName + "not found");
  207.         }
  208.         else {
  209.             AddressBook.remove(locationIndex);
  210.             System.out.println("\nPerson Deleted!");
  211.         }
  212.     }
  213.     public static void addPerson() {
  214.         System.out.print("\nEnter first name: ");
  215.         String fname = scanner.nextLine();
  216.         System.out.print("\nEnter last name: ");
  217.         String lname = scanner.nextLine();
  218.         System.out.print("Enter street address : ");
  219.         String sAddress = scanner.nextLine();
  220.         System.out.print("Enter city: ");
  221.         String city = scanner.nextLine();
  222.         System.out.print("Enter state: ");
  223.         String state = scanner.nextLine();
  224.         System.out.print("Enter zip: ");
  225.         String zip = scanner.nextLine();
  226.         System.out.print("Enter phone: ");
  227.         String phone = scanner.nextLine();
  228.         System.out.print("Enter modified date (mm/dd/yyy): ");
  229.         String strDay = scanner.nextLine();
  230.         Date lastModified = formatter.parse(strDay, new ParsePosition(0));
  231.         AddressBook.add(new Person(fname, lname, sAddress, city, state, zip, phone, lastModified));
  232.     }
  233.     public static void modifyPerson(){
  234.         // DecimalFormat form = new DecimalFormat("$##,###.00");
  235.         System.out.print("\nEnter full name: ");
  236.         String pName = scanner.nextLine();
  237.         int locationIndex = findIndex(pName);
  238.         Person atemp = (Person) AddressBook.get(locationIndex);
  239.         //shows current person info and asks for new person info
  240.         System.out.println("\nCurrent first name: " + atemp.getfName());
  241.         System.out.print("Enter new first name: ");
  242.         String fname = scanner.nextLine();
  243.         atemp.setfName(fname);
  244.         System.out.println("\nCurrent last name: " + atemp.getlName());
  245.         System.out.print("Enter new first name: ");
  246.         String lname = scanner.nextLine();
  247.         atemp.setlName(lname);
  248.         System.out.println("Current street address: " + atemp.getsAddress());
  249.         System.out.print("Enter new street address: ");
  250.         String sAddress = scanner.nextLine();
  251.         atemp.setsAddress(sAddress);
  252.         System.out.println("Current city: " + atemp.getCity());
  253.         System.out.print("Enter new city: ");
  254.         String city = scanner.nextLine();
  255.         atemp.setCity(city);
  256.         System.out.println("Current state: " + atemp.getState());
  257.         System.out.print("Enter new state: ");
  258.         String state = scanner.nextLine();
  259.         atemp.setState(state);
  260.         System.out.println("Current zip: " + atemp.getZip());
  261.         System.out.print("Enter new zip: ");
  262.         String zip = scanner.nextLine();
  263.         atemp.setZip(zip);
  264.         System.out.println("Current phone: " + atemp.getPhone());
  265.         System.out.print("Enter new phone: ");
  266.         String phone = scanner.nextLine();
  267.         atemp.setPhone(phone);
  268.         System.out.println("Current modified day: " + formatter.format(atemp.getModDay()));
  269.         System.out.print("Enter new modified day: ");
  270.         String strModDay = scanner.nextLine();
  271.         String expectedPattern = "MM/dd/yyyy";
  272.         SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
  273.         Date modDay = formatter.parse(strModDay, new ParsePosition(0));
  274.         atemp.setModDay(modDay);
  275.     }//ends modify
  276.     public static void listPerson(){
  277.         Collections.sort(AddressBook);
  278.         for (int i = 0; i < AddressBook.size(); i++) {
  279.         System.out.println(AddressBook.get(i));
  280.         }
  281.     }//ends print
  282.     public static int findIndex(String name) {
  283.         String[] names = name.split(" ");
  284.         int i = 0;
  285.         boolean done = false;
  286.         while ( i < AddressBook.size() && !done) {
  287.             if (names[0].equals(AddressBook.get(i).getfName())) {
  288.                 if (names[1].equals(AddressBook.get(i).getlName())) {
  289.                     done = true;
  290.                 }
  291.                 else {
  292.                     i++;
  293.                 }
  294.             }
  295.             else {
  296.                 i++;
  297.             }
  298.         }
  299.         if (i == AddressBook.size()) {
  300.             return -1;
  301.         }
  302.         else {
  303.             return i;
  304.         }
  305.     }
  306. }//ends class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement