Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. package flashcards;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8.  
  9. public class Main {
  10.     static Map<String, String> myCards = new LinkedHashMap<>();
  11.     static String fileName;
  12.     static List <String> stringTerAndDef = new ArrayList<>();
  13.     static List <String> termin = new ArrayList<>();
  14.     static List <String> definition = new ArrayList<>();
  15.  
  16.     static String input(){
  17.         Scanner scanner = new Scanner(System.in);
  18.         String input = scanner.nextLine();
  19.         return input;
  20.     }
  21.     static void remove() throws IOException {
  22.         System.out.println("The card");
  23.         String remove = input();
  24.         if (myCards.containsKey(remove)){
  25.             myCards.remove(remove);
  26.             System.out.println("The card has been removed.");
  27.         } else {
  28.             System.out.println("Can't remove \"" + remove + "\": there is no such card.");
  29.         }
  30.         action();
  31.     }
  32.     static void imPORT() throws IOException {
  33.         System.out.println("File name:");
  34.         fileName = "C:\\Users\\pleti\\Desktop\\" + input();
  35.         File file = new File(fileName);
  36.         try (Scanner scanner = new Scanner(file)) {
  37.             int ind = 0;
  38.             while (scanner.hasNext()) {
  39.                 String allString = scanner.nextLine();
  40.                 stringTerAndDef.add(allString);
  41.             }
  42.             int i = 0;
  43.             int y = 0;
  44.             while (i < stringTerAndDef.size()) {
  45.                 if (i % 2 == 0) {
  46.                     termin.add(stringTerAndDef.get(i));
  47.                 }
  48.                 if (i % 2 != 0) {
  49.                     definition.add(stringTerAndDef.get(i));
  50.                 }
  51.                 i++;
  52.             }
  53.             while (y < termin.size()){
  54.                 if (!myCards.containsKey(termin.get(y))){
  55.                     myCards.put(termin.get(y), definition.get(y));
  56.                     ind++;
  57.                 } else {
  58.                     myCards.put(termin.get(y), definition.get(y));
  59.                     ind++;
  60.                 }
  61.                 y++;
  62.             }
  63.             System.out.println(ind + " cards have been saved.");
  64.             action();
  65.         } catch (FileNotFoundException e) {
  66.             System.out.println("No file found: " + fileName);
  67.             action();
  68.         }
  69.     }
  70.     static void export() throws IOException {
  71.         System.out.println("File name:");
  72.         String newFile = "C:\\Users\\pleti\\Desktop\\" + input();
  73.         File file = new File(newFile);
  74.         FileWriter writer = new FileWriter(file);
  75.         int ind = 0;
  76.         for (Map.Entry<String, String> product : myCards.entrySet()) {
  77.             writer.write(product.getKey() + "\n");
  78.             writer.write(product.getValue() + "\n");
  79.             ind++;
  80.         }
  81.         System.out.println(ind + " cards have been loaded.");
  82.         writer.close();
  83.         action();
  84.     }
  85.     static void ask() throws IOException {
  86.         Random random = new Random();
  87.         System.out.println("How many times to ask?");
  88.         int randomInt = Integer.parseInt(input());
  89.         List <String> keys = new ArrayList<>(myCards.keySet());
  90.         List <String> value = new ArrayList<>(myCards.values());
  91.         int x = 0;
  92.         while (x < randomInt) {
  93.             String ranKey = keys.get(random.nextInt(keys.size()));
  94.             System.out.println("Print the definition of \"" + ranKey + "\":");
  95.             String answer = input();
  96.             if (answer.equals(myCards.get(ranKey))){
  97.                 System.out.println("Correct answer");
  98.                 x++;
  99.             } else{
  100.                 int indexKey = value.indexOf(answer);
  101.                 int correctAnswerIndex = keys.indexOf(ranKey);
  102.                 if (value.contains(answer)){
  103.  
  104.                     System.out.println("Wrong answer. " +
  105.                             "The correct one is \"" + value.get(correctAnswerIndex) +"\", you've just written the " +
  106.                             "definition of \"" + keys.get(indexKey) + "\".");
  107.                     x++;
  108.                 } else {
  109.                     System.out.println("Wrong answer. The correct one is \"" + value.get(correctAnswerIndex) +"\".");
  110.                     x++;
  111.                 }
  112.             }
  113.         }
  114.         action();
  115.     }
  116.     static void exit(){
  117.         System.out.println("Bye bye!");
  118.     }
  119.     static void action() throws IOException {
  120.         System.out.println("Input the action (add, remove, import, export, ask, exit):");
  121.         String action = input().toLowerCase();
  122.         switch (action){
  123.             case "add":
  124.                 add();
  125.                 break;
  126.             case "remove":
  127.                 remove();
  128.                 break;
  129.             case "import":
  130.                 imPORT();
  131.                 break;
  132.             case "export":
  133.                 export();
  134.                 break;
  135.             case "ask":
  136.                 ask();
  137.                 break;
  138.             case "exit":
  139.                 exit();
  140.                 break;
  141.             default:
  142.                 System.out.println("Incorect input");
  143.                 action();
  144.         }
  145.     }
  146.  
  147.     static void add() throws IOException {
  148.         System.out.println("The card:");
  149.         String ter = input();
  150.         if (myCards.containsKey(ter)){
  151.             System.out.println("The card \""+ ter + "\" already exists.");
  152.         }
  153.         System.out.println("The definition of the card:");
  154.         String def = input();
  155.         if (myCards.containsValue(def)){
  156.             System.out.println("The definition \""+ def +"\" already exists.");
  157.         }
  158.         myCards.put(ter, def);
  159.         System.out.println("The pair (\""+ ter + "\":\""+ def +"\") has been added.");
  160.         action();
  161.     }
  162.     public static void main(String[] args) throws IOException {
  163.         action();
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement