Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- public class SimpleDictionary {
- private HashMap<String, String> translations;
- public SimpleDictionary() {
- this.translations = new HashMap<>();
- }
- public String translate(String word) {
- return this.translations.get(word);
- }
- public void add(String word, String translation) {
- this.translations.put(word, translation);
- }
- }
- /////////////////////////////////////////////////////////
- import java.util.Scanner;
- public class TextUI {
- private Scanner scanner;
- private SimpleDictionary dictionary = new SimpleDictionary();
- public TextUI(Scanner scanner, SimpleDictionary dictionary) {
- this.scanner = scanner;
- this.dictionary = dictionary;
- }
- public void start() {
- while (true) {
- System.out.print("Command: ");
- String word = scanner.nextLine();
- if (word.equals("end")) {
- System.out.println("Bye bye!");
- break;
- }
- if (word.equals("add")) {
- System.out.print("Word: ");
- String word2 = scanner.nextLine();
- System.out.print("Translation: ");
- String translation = scanner.nextLine();
- dictionary.add(word2, translation);
- continue;
- }
- if (word.equals("search")) {
- System.out.print("To be translated: ");
- String search = scanner.nextLine();
- String result = dictionary.translate(search);
- if (result == null) {
- System.out.println("Word " + search + " was not found");
- } else {
- System.out.println(result);
- }
- continue;
- }
- System.out.println("Unknown command");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement