Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Person implements Comparable{
- private static int nextId = 0;
- private int id;
- private String fname;
- private String lname;
- private String sAddress;
- private String city;
- private String state;
- private String zip;
- private String phone;
- private Date ModDay;
- //employee constructor
- public Person(String newfName, String newlName, String newsAddress, String newcity, String newstate,
- String newzip, String newphone, Date newDay)
- {
- this.fname = newfName;
- this.lname = newlName;
- this.sAddress = newsAddress;
- this.city = newcity;
- this.state = newstate;
- this.zip = newzip;
- this.phone = newphone;
- this.ModDay = newDay;
- this.id = ++nextId;
- }
- public String getfName() {
- return fname;
- }
- public String getlName() {
- return lname;
- }
- public String getsAddress() {
- return sAddress;
- }
- public String getCity() {
- return city;
- }
- public String getState() {
- return state;
- }
- public String getZip() {
- return zip;
- }
- public String getPhone() {
- return phone;
- }
- public int getId() {
- return id;
- }
- public Date getModDay() {
- return ModDay;
- }
- //public void raiseSalary(double newValue){
- // salary = newValue;
- //}
- public void setfName(String newValue){
- fname = newValue;
- }
- public void setlName(String newValue){
- lname = newValue;
- }
- public void setsAddress(String newValue){
- sAddress = newValue;
- }
- public void setCity(String newValue){
- city = newValue;
- }
- public void setState(String newValue){
- state = newValue;
- }
- public void setZip(String newValue){
- zip = newValue;
- }
- public void setPhone(String newValue){
- phone = newValue;
- }
- public void setModDay(Date newValue){
- ModDay = newValue;
- }
- public boolean equals(Object o) {
- if (!(o instanceof Person))
- return false;
- Person a = (Person)o;
- return a.fname.equals(fname) &&
- a.lname.equals(lname);
- }
- public int compareTo (Object other) {
- int result;
- if (lname.equals(((Person)other).lname)) {
- result = fname.compareTo(((Person)other).fname);
- }
- else {
- result = lname.compareTo(((Person)other).lname);
- }
- return result;
- }
- public String toString() {
- //tostring method for array
- String expectedPattern = "MM/dd/yyyy";
- SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
- //DecimalFormat form = new DecimalFormat("$##,###.00");
- return "\nID#: " + id + "\nName: " + fname + " " + lname + "\nAddress: " + sAddress + " " + city + " " + state + " " + zip +
- "\nPhone: " + phone + "\nModified Day: " + formatter.format(ModDay);
- }
- }//end class
- //import java.util.Scanner;
- import java.util.*;
- import java.text.DecimalFormat;
- import java.text.ParsePosition;
- import java.text.SimpleDateFormat;
- public class AddressBook{
- //create array
- private static ArrayList <Person> AddressBook = new ArrayList <Person>();
- private static Scanner scanner = new Scanner(System.in);
- private static String expectedPattern = "MM/dd/yyyy";
- private static SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
- private static Person atemp;
- public static void main(String[] args) {
- System.out.println("\tAddress Book\n");
- System.out.println("List size is " + AddressBook.size());
- //System.out.print("How many people to process?: ");
- //size = Integer.parseInt(scanner.nextLine());
- char choice = ' ';
- char userChoice;
- String choiceStr;
- do {
- System.out.println("\nAddress Book Menu");
- System.out.print("\tEnter A to (A)dd Person\n" +
- "\tEnter D to (D)elete Person\n" +
- "\tEnter M to (M)odify Person\n" +
- "\tEnter S to (S)earch Person\n " +
- "\tEnter L to (L)ist All (sorted)\n" +
- "\tEnter Q to (Q)uit\n" +
- "Please enter your choice: ");
- choiceStr = scanner.nextLine();
- //assigns the first letter from the string
- choice = choiceStr.charAt(0);
- //converts (one) character to upper case character
- userChoice = Character.toUpperCase(choice);
- //user input validation
- while (userChoice != 'A' && userChoice != 'D' &&
- userChoice != 'M' && userChoice != 'S' &&
- userChoice != 'L' && userChoice != 'Q') {
- System.out.println("\nAddress Book Menu");
- System.out.print("\tEnter A to (A)dd Person\n" +
- "\tEnter D to (D)elete Person\n" +
- "\tEnter M to (M)odify Person\n" +
- "\tEnter S to (S)earch Person\n " +
- "\tEnter L to (L)ist All (sorted)" +
- "\tEnter Q to (Q)uit\n" +
- "Invalid Choice! Please enter your choice: ");
- choiceStr = scanner.nextLine();
- choice = choiceStr.charAt(0);
- userChoice = Character.toUpperCase(choice);
- }
- //determine user choice
- switch (userChoice){
- case 'A':
- addPerson();
- break;
- case 'D':
- deletePerson();
- break;
- case 'M':
- modifyPerson();
- break;
- case 'S':
- searchPerson();
- break;
- case 'L':
- listPerson();
- break;
- case 'Q':
- System.exit(0);
- }
- } while (userChoice != 'Q');
- }//ends main
- public static void searchPerson() {
- System.out.print("\nEnter full name: ");
- String pName = scanner.nextLine();
- int locationIndex = findIndex(pName);
- if (locationIndex == -1 ) {
- System.out.println(pName + "not found");
- }
- else {
- System.out.println(AddressBook.get(locationIndex));
- }
- }
- public static void deletePerson() {
- System.out.print("\nEnter full name: ");
- String pName = scanner.nextLine();
- int locationIndex = findIndex(pName);
- if (locationIndex == -1 ) {
- System.out.println(pName + "not found");
- }
- else {
- AddressBook.remove(locationIndex);
- System.out.println("\nPerson Deleted!");
- }
- }
- public static void addPerson() {
- System.out.print("\nEnter first name: ");
- String fname = scanner.nextLine();
- System.out.print("\nEnter last name: ");
- String lname = scanner.nextLine();
- System.out.print("Enter street address : ");
- String sAddress = scanner.nextLine();
- System.out.print("Enter city: ");
- String city = scanner.nextLine();
- System.out.print("Enter state: ");
- String state = scanner.nextLine();
- System.out.print("Enter zip: ");
- String zip = scanner.nextLine();
- System.out.print("Enter phone: ");
- String phone = scanner.nextLine();
- System.out.print("Enter modified date (mm/dd/yyy): ");
- String strDay = scanner.nextLine();
- Date lastModified = formatter.parse(strDay, new ParsePosition(0));
- AddressBook.add(new Person(fname, lname, sAddress, city, state, zip, phone, lastModified));
- }
- public static void modifyPerson(){
- // DecimalFormat form = new DecimalFormat("$##,###.00");
- System.out.print("\nEnter full name: ");
- String pName = scanner.nextLine();
- int locationIndex = findIndex(pName);
- Person atemp = (Person) AddressBook.get(locationIndex);
- //shows current person info and asks for new person info
- System.out.println("\nCurrent first name: " + atemp.getfName());
- System.out.print("Enter new first name: ");
- String fname = scanner.nextLine();
- atemp.setfName(fname);
- System.out.println("\nCurrent last name: " + atemp.getlName());
- System.out.print("Enter new first name: ");
- String lname = scanner.nextLine();
- atemp.setlName(lname);
- System.out.println("Current street address: " + atemp.getsAddress());
- System.out.print("Enter new street address: ");
- String sAddress = scanner.nextLine();
- atemp.setsAddress(sAddress);
- System.out.println("Current city: " + atemp.getCity());
- System.out.print("Enter new city: ");
- String city = scanner.nextLine();
- atemp.setCity(city);
- System.out.println("Current state: " + atemp.getState());
- System.out.print("Enter new state: ");
- String state = scanner.nextLine();
- atemp.setState(state);
- System.out.println("Current zip: " + atemp.getZip());
- System.out.print("Enter new zip: ");
- String zip = scanner.nextLine();
- atemp.setZip(zip);
- System.out.println("Current phone: " + atemp.getPhone());
- System.out.print("Enter new phone: ");
- String phone = scanner.nextLine();
- atemp.setPhone(phone);
- System.out.println("Current modified day: " + formatter.format(atemp.getModDay()));
- System.out.print("Enter new modified day: ");
- String strModDay = scanner.nextLine();
- String expectedPattern = "MM/dd/yyyy";
- SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
- Date modDay = formatter.parse(strModDay, new ParsePosition(0));
- atemp.setModDay(modDay);
- }//ends modify
- public static void listPerson(){
- Collections.sort(AddressBook);
- for (int i = 0; i < AddressBook.size(); i++) {
- System.out.println(AddressBook.get(i));
- }
- }//ends print
- public static int findIndex(String name) {
- String[] names = name.split(" ");
- int i = 0;
- boolean done = false;
- while ( i < AddressBook.size() && !done) {
- if (names[0].equals(AddressBook.get(i).getfName())) {
- if (names[1].equals(AddressBook.get(i).getlName())) {
- done = true;
- }
- else {
- i++;
- }
- }
- else {
- i++;
- }
- }
- if (i == AddressBook.size()) {
- return -1;
- }
- else {
- return i;
- }
- }
- }//ends class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement