Advertisement
Sergei_Langin

Sergei FlashCards Stage 7 for dying_coder

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