Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5.     static Scanner scanner;
  6.     static Map<String, String> cardToDefinition;
  7.     static Map<String, String> definitionToCard;
  8.  
  9.     public static void main(String[] args) {
  10.             scanner = new Scanner(System.in);
  11.             cardToDefinition = new HashMap<>();
  12.             definitionToCard = new HashMap<>();
  13.            
  14.             while (true) {
  15.             System.out.println("Input the action (add, remove, import, export, ask, exit):");
  16.             String input = scanner.nextLine();
  17.             switch(input) {
  18.             case "add":
  19.             add();
  20.             break;
  21.             case "remove":
  22.             remove();
  23.             break;
  24.             case "import":
  25.             importCards();
  26.             break;
  27.             case "export":
  28.             exportCards();
  29.             break;
  30.             case "ask":
  31.             ask();
  32.             break;
  33.             case "exit":
  34.             System.out.println("Bye bye!");
  35.             System.exit(0);
  36.             break;
  37.        
  38.     }
  39.     }
  40.     }
  41.  
  42.  
  43.     public static void add() {
  44.         System.out.println("The card:");
  45.         String userCard = scanner.nextLine();
  46.         if (cardToDefinition.containsKey(userCard)) {
  47.             System.out.println("The card "+ "\"" + userCard + "\"" +" already exists.");
  48.             return;
  49.         }
  50.         System.out.println("The definition of the card:");
  51.         String userDefinition = scanner.nextLine();
  52.         if (cardToDefinition.containsValue(userDefinition)) {
  53.             System.out.println("The definition " + "\"" + userDefinition + "\"" + " already exists.");
  54.         } else {
  55.             cardToDefinition.put(userCard, userDefinition);
  56.             definitionToCard.put(userDefinition, userCard);
  57.             System.out.println("The pair (" + "\"" + userCard + "\"" + ":" + "\"" + userDefinition + "\"" + ") has been added.");
  58.         }
  59.     }
  60.     public static void remove() {
  61.         System.out.println("The card:");
  62.         String userCard = scanner.nextLine();
  63.         if (cardToDefinition.containsKey(userCard)) {
  64.             definitionToCard.remove(cardToDefinition.get(userCard));
  65.             cardToDefinition.remove(userCard);
  66.             System.out.println("The card has been removed.");
  67.         } else {
  68.             System.out.println("Can't remove " + "\"" +userCard + "\"" + ": there is no such card.");
  69.         }
  70.     }
  71.  
  72.     public static void ask() {
  73.         System.out.println("How many times to ask?");
  74.         int numOfAsk = Integer.parseInt(scanner.nextLine());
  75.         int numb = 0;
  76.         for (Map.Entry<String, String> card : cardToDefinition.entrySet()) {
  77.             if(numb == numOfAsk) {
  78.                 break;
  79.             }
  80.            
  81.             System.out.println("Print the definition of " + "\"" + card.getKey() + "\"" + ":");
  82.             String answer = scanner.nextLine();
  83.             if (answer.equals(card.getValue())) {
  84.                 System.out.println("Correct answer.");
  85.             } else if (cardToDefinition.containsValue(answer)) {
  86.            
  87.                 System.out.println("Wrong answer . The correct one is " + "\"" + card.getValue() + "\"" + ", you've just written a definition of " + "\"" + definitionToCard.get(answer) + "\"" + " card.");
  88.             } else {
  89.                 System.out.println("Wrong answer . The correct one is " + "\"" + card.getValue() + "\"" + ".");
  90.             }
  91.            numb++;
  92.         }
  93.        
  94.            
  95.        
  96.     }
  97.  
  98.  
  99.     public static void importCards() {
  100.         System.out.println("File name:");
  101.         String pathToFile = scanner.nextLine();
  102.         File file = new File(pathToFile);
  103.         int counter=0;
  104.         try (Scanner reader = new Scanner(file)) {
  105.             while (reader.hasNext()) {
  106.                 String userCard = reader.next();
  107.                 String userDefinition = reader.next();
  108.                 counter++;
  109.                 if (cardToDefinition.containsKey(userCard) || cardToDefinition.containsValue(userDefinition)) {
  110.                     System.out.println("Don't use a duplicated card or a definition");
  111.                     counter--;
  112.                 } else {
  113.                     cardToDefinition.put(userCard, userDefinition);
  114.                     definitionToCard.put(userDefinition, userCard);
  115.                 }
  116.             }
  117.             System.out.println(counter + " cards have been loaded.");
  118.         } catch (FileNotFoundException e) {
  119.             System.out.println("File not found.");
  120.         }
  121.     }
  122.  
  123.     public static void exportCards() {
  124.         System.out.println("File name:");
  125.         String pathToFile = scanner.nextLine();
  126.         File file = new File(pathToFile);
  127.         try (PrintWriter printWriter = new PrintWriter(file)) {
  128.             for (Map.Entry<String, String> card : cardToDefinition.entrySet()) {
  129.                 printWriter.println(card.getKey());
  130.                 printWriter.println(card.getValue());
  131.             }
  132.             System.out.println(cardToDefinition.size() + " cards have been saved.");
  133.         } catch (IOException e) {
  134.             System.out.printf("An exception occurs %s", e.getMessage());
  135.         }
  136.     }
  137.    
  138.    
  139.        
  140.  
  141.    
  142.    
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement