Sergei_Langin

Sergei FlashCards Stage 7 for hyperskill

Mar 16th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.64 KB | None | 0 0
  1. package flashcards;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     private static Object getKeyFromValue(Map hm, Object value) {
  10.         for (Object o : hm.keySet()) {
  11.             if (hm.get(o).equals(value)) {
  12.                 return o;
  13.             }
  14.         }
  15.         return null;
  16.     }
  17.     private static List<Object> getKeyFromValue2(Map hm, Object value) {
  18.         List<Object> rtn = new ArrayList<>();
  19.         for (Object o : hm.keySet()) {
  20.             if (hm.get(o).equals(value)) {
  21.                 rtn.add(o);
  22.             }
  23.         }
  24.         return rtn;
  25.     }
  26.     private static void listaMap(Map<String, String> map, Map<String, Integer> map2, File filename) {
  27.         if (map != null) {
  28.             try (FileWriter writer = new FileWriter(filename)) {
  29.                 for (String key : map.keySet()) {
  30.                     String value = map.get(key);
  31.                     Integer error = map2.get(key);
  32.                     if (value == null) {
  33.                         System.out.println("Warning: " +
  34.                                 key + " in listaMap.");
  35.                         value = "Not sure";
  36.                     }
  37.                     writer.write(key.trim());
  38.                     writer.write('\n');
  39.                     writer.write(value.trim());
  40.                     writer.write('\n');
  41.                     writer.write(error + "\n");
  42.                 }
  43.             } catch (IOException e) {
  44.                 System.out.println("Issue: " + filename +
  45.                         " in listaMap");
  46.             }
  47.         }
  48.     }
  49.  
  50.     private static void listaList(LinkedList<String> list2, File filename) {
  51.         if (list2 != null) {
  52.             try {
  53.                 FileWriter writer = new FileWriter(filename);
  54.                 for (String s : list2) {
  55.                     writer.write(s + "\n");
  56.                 }
  57.                 writer.close();
  58.             } catch (IOException e) {
  59.                 System.out.println("Issue: " + filename +
  60.                         " in listaMap");
  61.             }
  62.         }
  63.     }
  64.     public static void main(String[] args) throws IOException {
  65.  
  66.         LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();
  67.         LinkedList<String> list = new LinkedList<>();
  68.         LinkedList<Integer> list2 = new LinkedList<>();
  69.         LinkedHashMap<String, Integer> hashMap2 = new LinkedHashMap<>();
  70.         File file = new File("C:\\Users\\Plaza\\IdeaProjects\\HelloWorld\\src\\capitals.txt");
  71.         File file2 = new File("C:\\Users\\Plaza\\IdeaProjects\\HelloWorld\\src\\capitalsNew.txt");
  72.         File file3 = new File("testLog.txt");
  73.  
  74.         if (args.length == 4) {
  75.             if ((args[0].equals("-import") & args[2].equals("-export")) |
  76.                     (args[0].equals("-export") & args[2].equals("-import"))) {
  77.                 int N = 0;
  78.                 Scanner skaner = null;
  79.                 try {
  80.                     skaner = new Scanner(file);
  81.                     while (skaner.hasNext()) {
  82.                         String question = skaner.nextLine();
  83.                         String answer = skaner.nextLine();
  84.                         String error = skaner.nextLine();
  85.                         hashMap.put(question, answer);
  86.                         hashMap2.put(question, Integer.valueOf(error));
  87.                         N++;
  88.                     }
  89.                 } finally {
  90.                     if (skaner != null) {
  91.                         skaner.close();
  92.                     }
  93.                 }
  94.                 System.out.println(N + " cards have been loaded.");
  95.                 listaMap(hashMap, hashMap2, file2);
  96.             }
  97.         } else if (args.length == 2) {
  98.             if (args[0].equals("-import")) {
  99. //            if ((args[0].equals("-import") & args[2].equals("-export")) |
  100. //                    (args[0].equals("-export") & args[2].equals("-import"))) {
  101. //                if ((args[1].equals("capitals.txt") & args[3].equals("capitalsNew.txt")) |
  102. //                        (args[1].equals("capitalsNew.txt") & args[3].equals("capitals.txt"))) {
  103.                 int N = 0;
  104.                 Scanner skaner = null;
  105.                 try {
  106.                     skaner = new Scanner(file);
  107.                     while (skaner.hasNext()) {
  108.                         String question = skaner.nextLine();
  109.                         String answer = skaner.nextLine();
  110.                         String error = skaner.nextLine();
  111.                         hashMap.put(question, answer);
  112.                         hashMap2.put(question, Integer.valueOf(error));
  113.                         N++;
  114.                     }
  115.                 } finally {
  116.                     if (skaner != null) {
  117.                         skaner.close();
  118.                     }
  119.                 }
  120.                 System.out.println(N + " cards have been loaded.");
  121.             } else if (args[0].equals("-export")) {
  122.                 //if (args[1].equals("capitalsNew.txt")) {
  123.                 listaMap(hashMap, hashMap2, file2);
  124.                 //}
  125.             }
  126.         }
  127.  
  128.         for (; ; ) {
  129.             System.out.println("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
  130.             list.add("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
  131.             Scanner scan = new Scanner(System.in);
  132.             //while (scan.hasNextLine()) {
  133.             String action = scan.nextLine();
  134.  
  135.             list.add(action);
  136.  
  137.             switch (action) {
  138.                 case "add":
  139.                     System.out.println("The card:");
  140.                     list.add("The card:");
  141.                     String frontCard = scan.nextLine();
  142.                     list.add(frontCard);
  143.                     if (hashMap.containsKey(frontCard)) {
  144.                         System.out.println("The card \"" + frontCard + "\"" + " already exists.");
  145.                         list.add("The card \"" + frontCard + "\"" + " already exists.");
  146.                         System.out.println();
  147.                         break;
  148.                     }
  149.                     System.out.println("The definition of the card:");
  150.                     list.add("The definition of the card:");
  151.                     String backCard = scan.nextLine();
  152.                     list.add(backCard);
  153.                     if (hashMap.containsValue(backCard)) {
  154.                         System.out.println("The definition \"" + backCard + "\"" + " already exists.");
  155.                         list.add("The definition \"" + backCard + "\"" + " already exists.");
  156.                         System.out.println();
  157.                         break;
  158.                     }
  159.                     hashMap.put(frontCard, backCard);
  160.                     System.out.println("The pair (\"" + frontCard + "\"" + ":\"" + backCard + "\"" + ") has been added.");
  161.                     list.add("The pair (\"" + frontCard + "\"" + ":\"" + backCard + "\"" + ") has been added.");
  162.                     System.out.println();
  163.                    // scan.close();
  164.                     break;
  165.  
  166.  
  167.                 case "remove":
  168.                     System.out.println("The card:");
  169.                     list.add("The card:");
  170.                     String frontCard2 = scan.nextLine();
  171.                     list.add(frontCard2);
  172.                     if (hashMap.containsKey(frontCard2)) {
  173.                         hashMap.remove(frontCard2);
  174.                         hashMap2.remove(frontCard2);
  175.                         System.out.println("The card has been removed.");
  176.                         list.add("The card has been removed.");
  177.                         System.out.println();
  178.                     } else {
  179.                         System.out.println("Can't remove \"" + frontCard2 + "\"" + ": there is no such card.");
  180.                         list.add("Can't remove \"" + frontCard2 + "\"" + ": there is no such card.");
  181.                         System.out.println();
  182.                     }
  183.                     //scan.close();
  184.                     break;
  185.  
  186.                 case "import":
  187.                     int N = 0;
  188.                     System.out.println("File name:");
  189.                     list.add("File name:");
  190.                     String capitals = scan.nextLine();
  191.                     list.add(capitals);
  192.                     if (capitals.equals("capitals.txt")) {
  193.                         Scanner skaner = null;
  194.                         try {
  195.                             skaner = new Scanner(file);
  196.                             while (skaner.hasNext()) {
  197.                                 String question = skaner.nextLine();
  198.                                 String answer = skaner.nextLine();
  199.                                 String error = skaner.nextLine();
  200.                                 hashMap.put(question, answer);
  201.                                 hashMap2.put(question, Integer.valueOf(error));
  202.                                 N++;
  203.                             }
  204.                         } finally {
  205.                             if (skaner != null) {
  206.                                 skaner.close();
  207.                             }
  208.                         }
  209.                         System.out.println(N + " cards have been loaded.");
  210.                         list.add(N + " cards have been loaded.");
  211.                         System.out.println();
  212.                     } else if (capitals.equals("capitalsNew.txt")) {
  213.                         Scanner skaner = null;
  214.                         try {
  215.                             skaner = new Scanner(file2);
  216.                             while (skaner.hasNext()) {
  217.                                 String question = skaner.nextLine();
  218.                                 String answer = skaner.nextLine();
  219.                                 hashMap.put(question, answer);
  220.                                 N++;
  221.                             }
  222.                         } finally {
  223.                             if (skaner != null) {
  224.                                 skaner.close();
  225.                             }
  226.                         }
  227.                         System.out.println(N + " cards have been loaded.");
  228.                         list.add(N + " cards have been loaded.");
  229.                         System.out.println();
  230.                     } else {
  231.                         System.out.println("File not found.");
  232.                         list.add("File not found.");
  233.                         System.out.println();
  234.                     }
  235.                     //scan.close();
  236.                     break;
  237.  
  238.                 case "export":
  239.                     System.out.println("File name:");
  240.                     list.add("File name:");
  241.                     String capitalsNew = scan.next();
  242.                     list.add(capitalsNew);
  243.                     if (capitalsNew.equals("capitalsNew.txt")) {
  244.                         listaMap(hashMap, hashMap2, file2);
  245.                     } else if (capitalsNew.equals("capitals.txt")) {
  246.                         listaMap(hashMap, hashMap2, file);
  247.                     }
  248.                     System.out.println(hashMap.size() + " cards have been saved.");
  249.                     list.add(hashMap.size() + " cards have been saved.");
  250.                     System.out.println();
  251.                     //scan.close();
  252.                     break;
  253.  
  254.                 case "ask":
  255.                     System.out.println("How many times to ask?");
  256.                     list.add("How many times to ask?");
  257.                     Scanner scan1 = new Scanner(System.in);
  258.                     String enter = scan1.nextLine();
  259.                     list.add(enter);
  260.                     int y = Integer.parseInt(enter);
  261.                     Collection<String> values = hashMap.values();
  262.  
  263.                     for (int j = 0; j < y; ) {
  264.                         for (String randomValue : values) {
  265.                             int count = 0;
  266.                             System.out.println("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  267.                             list.add("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  268.                             String answer = scan.next();
  269.                             list.add(answer);
  270.                             if (answer.equals(randomValue)) {
  271.                                 System.out.println("Correct answer.");
  272.                                 list.add("Correct answer.");
  273.                             } else if (hashMap.containsValue(answer)) {
  274.                                 count++;
  275.                                 hashMap2.put((String) getKeyFromValue(hashMap, randomValue),
  276.                                         hashMap2.getOrDefault((String) getKeyFromValue(hashMap, randomValue), (int) 0) + count);
  277.                                 System.out.println("Wrong answer. (The correct one is \"" +
  278.                                         randomValue + "\"" + ", " +
  279.                                         "you've just written the definition of \"" +
  280.                                         getKeyFromValue(hashMap, answer) + "\"" + ".)");
  281.                                 list.add("Wrong answer. (The correct one is \"" +
  282.                                         randomValue + "\"" + ", " +
  283.                                         "you've just written the definition of \"" +
  284.                                         getKeyFromValue(hashMap, answer) + "\"" + ".)");
  285.                             } else {
  286.                                 count++;
  287.                                 hashMap2.put((String) getKeyFromValue(hashMap, randomValue),
  288.                                         hashMap2.getOrDefault((String) getKeyFromValue(hashMap, randomValue), (int) 0) + count);
  289.                                 System.out.println("Wrong answer. The correct one is \"" + randomValue + "\"" + ".");
  290.                                 list.add("Wrong answer. The correct one is \"" + randomValue + "\"" + ".");
  291.                             }
  292.                             j++;
  293.                             if (j == y) {
  294.                                 break;
  295.                             }
  296.                         }
  297.                     }
  298.                     //scan.close();
  299.                     break;
  300.  
  301.                 case "exit":
  302.                     System.out.println("Bye bye!");
  303.                     list.add("Bye bye!");
  304.                     if (args.length > 0) {
  305.                         System.out.println(hashMap.size() + " cards have been saved.");
  306.                     }
  307.                     System.exit(0);
  308.                     break;
  309.  
  310.                 case "log":
  311.                     System.out.println("File name:");
  312.                     list.add("File name:");
  313.                     String log = scan.next();
  314.                     list.add(log);
  315.                     if (log.equals("testLog.txt")) {
  316.                         listaList(list, file3);
  317.                     }
  318.                     System.out.println("The log has been saved.");
  319.                     System.out.println();
  320.                     //scan.close();
  321.                     break;
  322.  
  323.                 case "hardest card":
  324.                     Collection<Integer> values2 = hashMap2.values();
  325.                     int count = 0;
  326.                     for (Integer Value2 : values2) {
  327.                         if (Value2 > 0) {
  328.                             count++;
  329.                             list2.add(Value2);
  330.                         }
  331.                     }
  332.                     if (count == 1) {
  333.                         System.out.println("The hardest card is \"" +
  334.                                 getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0) + "\"" +
  335.                                 ". You have " + list2.get(0) + " errors answering it.");
  336.                         list2.clear();
  337.                     } else if (count > 1) {
  338.                         System.out.println("The hardest cards are \"" +
  339.                                 getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0) + "\"" +
  340.                                 ", \"" + getKeyFromValue2(hashMap2, (Integer) list2.get(1)).get(1) + "\"" + "." +
  341.                                 "You have " + list2.get(0) + " errors answering them.");
  342.                         list2.clear();
  343.                     } else {
  344.                         System.out.println("There are no cards with errors.");
  345.                     }
  346.                     break;
  347.  
  348.                 case "reset stats":
  349.                     for (String key : hashMap2.keySet()) {
  350.                         hashMap2.remove(key);
  351.                     }
  352.                     list2.clear();
  353.                     System.out.println("Card statistics has been reset.");
  354.                     break;
  355.             }
  356.         }
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment