Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1.  
  2. import java.util.HashMap;
  3.  
  4. public class SimpleDictionary {
  5.  
  6.     private HashMap<String, String> translations;
  7.  
  8.     public SimpleDictionary() {
  9.         this.translations = new HashMap<>();
  10.     }
  11.  
  12.     public String translate(String word) {
  13.         return this.translations.get(word);
  14.     }
  15.  
  16.     public void add(String word, String translation) {
  17.         this.translations.put(word, translation);
  18.     }
  19.    
  20. }
  21.  
  22. /////////////////////////////////////////////////////////
  23.  
  24. import java.util.Scanner;
  25.  
  26. public class TextUI {
  27.  
  28.     private Scanner scanner;
  29.     private SimpleDictionary dictionary = new SimpleDictionary();
  30.  
  31.     public TextUI(Scanner scanner, SimpleDictionary dictionary) {
  32.         this.scanner = scanner;
  33.         this.dictionary = dictionary;
  34.     }
  35.  
  36.     public void start() {
  37.  
  38.         while (true) {
  39.             System.out.print("Command: ");
  40.             String word = scanner.nextLine();
  41.  
  42.             if (word.equals("end")) {
  43.                 System.out.println("Bye bye!");
  44.                 break;
  45.             }
  46.  
  47.             if (word.equals("add")) {
  48.                 System.out.print("Word: ");
  49.                 String word2 = scanner.nextLine();
  50.                 System.out.print("Translation: ");
  51.                 String translation = scanner.nextLine();
  52.                 dictionary.add(word2, translation);
  53.                 continue;
  54.             }
  55.  
  56.             if (word.equals("search")) {
  57.                 System.out.print("To be translated: ");
  58.                 String search = scanner.nextLine();
  59.                 String result = dictionary.translate(search);
  60.                 if (result == null) {
  61.                     System.out.println("Word " + search + " was not found");
  62.                 } else {
  63.                     System.out.println(result);
  64.                 }
  65.                 continue;
  66.             }
  67.  
  68.             System.out.println("Unknown command");
  69.  
  70.         }
  71.  
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement