Advertisement
desislava_topuzakova

05. Phonebook

Jan 18th, 2022
672
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package SetsAndMapAdvanced_Exercise;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Phonebook_05 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         //име на човек -> телефонен номер
  11.         Map<String, String> phonebook = new HashMap<>();
  12.         String data = scanner.nextLine();
  13.  
  14.         //1. запълване на phonebook
  15.         while (!data.equals("search")) {
  16.             //data = "John-0888080808".split("-") -> ["John", "0888080808"]
  17.             String name = data.split("-")[0];
  18.             String phoneNumber = data.split("-")[1];
  19.             phonebook.put(name, phoneNumber);
  20.             data = scanner.nextLine();
  21.         }
  22.  
  23.         //2. търсене в phonebook
  24.         String name = scanner.nextLine();
  25.         while (!name.equals("stop")) {
  26.             //ако името го има в phonebook (име -> номер): "{name} -> {number}"
  27.             if (phonebook.containsKey(name)) {
  28.                 System.out.println(name + " -> " + phonebook.get(name));
  29.             }
  30.             //ако името го няма в phonebook: "Contact {name} does not exist."
  31.             else {
  32.                 System.out.printf("Contact %s does not exist.%n", name);
  33.             }
  34.  
  35.             name = scanner.nextLine();
  36.         }
  37.  
  38.     }
  39. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement