Advertisement
YavorGrancharov

Phonebook_Upgrade

Jan 6th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class Phonebook_Upgrade {
  7.     public static void main(String[] args) {
  8.         Scanner console = new Scanner(System.in);
  9.  
  10.         String line = console.nextLine();
  11.  
  12.         Map<String, String> phonebook = new LinkedHashMap<>();
  13.  
  14.         while (!line.equals("END")) {
  15.             String[] tokens = line.split("\\s");
  16.             if (tokens[0].equals("A")) {
  17.                 String name = tokens[1];
  18.                 String phone = tokens[2];
  19.                 if (!phonebook.containsKey(name)) {
  20.                     phonebook.put(name, phone);
  21.                 }
  22.                 phonebook.put(name, phone);
  23.             }
  24.             else if (tokens[0].equals("S")) {
  25.                 String name = tokens[1];
  26.                 if (phonebook.containsKey(name)) {
  27.                     System.out.println(name + " -> " + phonebook.get(name));
  28.                 }
  29.                 else {
  30.                     System.out.printf("Contact %s does not exist.\n", name);
  31.                 }
  32.             }
  33.             if (tokens[0].equals("ListAll")) {
  34.                 TreeMap<String,String> upgrade = new TreeMap<>(phonebook);
  35.                 for (Map.Entry<String,String> entry : upgrade.entrySet()) {
  36.                     System.out.println(entry.getKey() + " -> " + entry.getValue());
  37.                 }
  38.             }
  39.             line = console.nextLine();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement