Sergei_Langin

Sergei FlashCards Stage 7 Full for Alexander_P-v

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