Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- public class Phonebook {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- LinkedHashMap<String, String> phonebook = new LinkedHashMap<>();
- String input = console.nextLine();
- while (!(input == "END")) {
- String[] commandArgs = input.split(" ");
- String command = commandArgs[0];
- switch (command){
- case "A":
- String name = commandArgs[1];
- String phoneNum = commandArgs[2];
- AddToPhonebook(phonebook, name, phoneNum);
- break;
- case "S":
- name = commandArgs[1];
- SearchInPhonebook(phonebook, name); break;
- }
- input = console.nextLine();
- }
- }
- private static void SearchInPhonebook(LinkedHashMap<String, String> phonebook, String name) {
- if (phonebook.containsKey(name)) {
- System.out.printf("%s -> %s\n", name, phonebook.get(name));
- }
- else System.out.printf("Contact %s does not exist.\n", name);
- }
- private static void AddToPhonebook(LinkedHashMap<String, String> phonebook, String name, String phoneNum) {
- if (!phonebook.containsKey(name)) {
- phonebook.put(name, phoneNum);
- }
- else {
- phonebook.replace(name, phonebook.get(name), phoneNum);
- }
- }
- }
Add Comment
Please, Sign In to add comment