Sergei_Langin

Sergei FlashCards Stage 7 Full

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