Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.text.*;
- class PhoneBook {
- Scanner reader;
- public static void main(String args[])
- {
- PhoneBook pb = new PhoneBook(); // Creates a phonebook object, which then does everything.
- }
- public PhoneBook()
- {
- String name;
- ArrayList aryList = new ArrayList(); //creates an array list
- do //begins loop for array entry
- {
- reader = new Scanner(System.in);
- name = getValidName();
- if(!name.equalsIgnoreCase("EXIT")) //Adds condition to Exit loop
- {
- nameNumber info = new nameNumber(name, getValidNumber(true));
- aryList.add(info); //ands nameNumber obj into arraylist
- }
- }while(!name.equalsIgnoreCase("EXIT")); //ends array input
- System.out.println("");
- System.out.println("How would you wish to have this list sorted?"); //prepares switch statement
- System.out.println(" 1. Ordered by Name");//1 to order the arraylist by name
- System.out.println(" 2. Ordered by Number");//2 to order the arraylist by phone number
- System.out.println(" 3. No Change");//returns arraylist
- long selection= getValidNumber(false);
- switch((int)selection)//inputs user's selection into switch statment
- {
- case 1://order the arraylist by name
- System.out.println("test");
- break;
- case 2://order the arraylist by number
- System.out.println("test");
- break;
- case 3://returns an unsorted arraylist
- System.out.print(aryList);
- break;
- default:
- System.out.println("Choose 1, 2, or 3 only");//not needed, but just in case
- break;
- }
- }
- public String getValidName()
- {
- String name;
- System.out.print("Please enter the name to whom the Phone Number belongs (\"Exit\" to abort): ");
- name = reader.nextLine();
- return name;
- } // Call this to get a name. "valid" is a relative term here.
- public long getValidNumber(boolean phone)
- {
- long n = 0;
- String input;
- boolean valid = false;
- while (!valid)
- {
- if (phone)
- askNumber();
- else
- askChoice();
- input = reader.nextLine();
- try
- {
- n = Long.parseLong(input);
- valid = true;
- }
- catch (NumberFormatException e)
- {
- System.out.println("Please enter a valid number!");
- valid = false;
- n = 0;
- }
- }
- return n;
- } // Call this to get a valid number, true for Phone numbers, false for selection
- public void askNumber()
- {
- System.out.print("Please enter the phone number of the individual: ");
- }
- public void askChoice()
- {
- System.out.print("Please Enter a valid number selection (1-3): ");
- }
- class nameNumber {
- public nameNumber(String na, long nu)//constructor
- {
- this.na = na;
- this.nu = nu;
- }
- private String na;
- private long nu;
- public String toString()
- {
- return "[" + na + " , " + nu + "]";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement