Advertisement
iamaamir

PhoneBook

Sep 14th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import static java.lang.System.out;
  6. import java.util.LinkedHashMap;
  7. import java.util.Map;
  8.  
  9. /**
  10.  *
  11.  * @author Aamir khan
  12.  */
  13. public class PhoneBook {
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.  
  17.         //Constant
  18.         final short NUMBER_LENGTH = 10;
  19.  
  20.         //Create Map
  21.         Map<String, Long> m = new LinkedHashMap<>();
  22.  
  23.         //vars
  24.         String name, num;
  25.         Long Phno;
  26.         BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
  27.  
  28.         //Menu
  29.         while (true) {
  30.             out.println("1 Add Phone Number");
  31.             out.println("2 Search");
  32.             out.println("3 Lookup Book");
  33.             out.println("4 Exit");
  34.             out.println("Enter Your Choice:");
  35.  
  36.             char choice = console.readLine().charAt(0);
  37.  
  38.             switch (choice) {
  39.  
  40.                 case '1':
  41.                     out.println("Enter Name: ");
  42.                     name = console.readLine();
  43.  
  44.                     name = (name.equals("")) ? "Unknown name" : name;
  45.  
  46.                     out.println("Enter Phone Number: ");
  47.                     num = console.readLine();
  48.  
  49.                     int length = num.length();
  50.                     if (length < NUMBER_LENGTH || length > NUMBER_LENGTH) {
  51.                         throw new IllegalArgumentException("Number Must be " + NUMBER_LENGTH + " Digits Long");
  52.                     }
  53.  
  54.                     Phno = new Long(num);
  55.                     //Add Name and Numbers
  56.                     m.put(name, Phno);
  57.                     break;
  58.  
  59.                 case '2':
  60.                     out.println("Enter Name :");
  61.                     name = console.readLine();
  62.                     name = name.trim();
  63.                     Phno = m.get(name);
  64.  
  65.                     if (Phno == null) {
  66.                         System.err.println("No Entry Found please add One");
  67.                     } else {
  68.                         out.println(Phno);
  69.                     }
  70.                     break;
  71.  
  72.                 case '3':
  73.                     m.entrySet().stream().forEach((en) -> {
  74.                         Object Name = en.getKey();
  75.                         Object no = en.getValue();
  76.                         System.out.printf("Name = %s and No = %s\n", Name, no);
  77.                     });
  78.                     break;
  79.                 case '4':
  80.                     return;
  81.             }//switch end
  82.         }//loop end
  83.     }//main end
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement