Sergei_Langin

Sergei FlashCards Stage 7 Full

Mar 17th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.63 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.*;
  5. import java.util.List;
  6.  
  7. public class flashcards {
  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.             try (Scanner scan = new Scanner(System.in)) {
  132.                 //while (scan.hasNextLine()) {
  133.                 //if (scan.hasNextLine()) {
  134.                 String action = scan.nextLine();
  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.                     case "remove":
  167.                         System.out.println("The card:");
  168.                         list.add("The card:");
  169.                         String frontCard2 = scan.nextLine();
  170.                         list.add(frontCard2);
  171.                         if (hashMap.containsKey(frontCard2)) {
  172.                             hashMap.remove(frontCard2);
  173.                             hashMap2.remove(frontCard2);
  174.                             System.out.println("The card has been removed.");
  175.                             list.add("The card has been removed.");
  176.                             System.out.println();
  177.                         } else {
  178.                             System.out.println("Can't remove \"" + frontCard2 + "\"" + ": there is no such card.");
  179.                             list.add("Can't remove \"" + frontCard2 + "\"" + ": there is no such card.");
  180.                             System.out.println();
  181.                         }
  182.                         //scan.close();
  183.                         break;
  184.  
  185.                     case "import":
  186.                         int N = 0;
  187.                         System.out.println("File name:");
  188.                         list.add("File name:");
  189.                         String capitals = scan.nextLine();
  190.                         list.add(capitals);
  191.                         if (capitals.equals("capitals.txt")) {
  192.                             Scanner skaner = null;
  193.                             try {
  194.                                 skaner = new Scanner(file);
  195.                                 while (skaner.hasNext()) {
  196.                                     String question = skaner.nextLine();
  197.                                     String answer = skaner.nextLine();
  198.                                     String error = skaner.nextLine();
  199.                                     hashMap.put(question, answer);
  200.                                     hashMap2.put(question, Integer.valueOf(error));
  201.                                     N++;
  202.                                 }
  203.                             } finally {
  204.                                 if (skaner != null) {
  205.                                     skaner.close();
  206.                                 }
  207.                             }
  208.                             System.out.println(N + " cards have been loaded.");
  209.                             list.add(N + " cards have been loaded.");
  210.                             System.out.println();
  211.                         } else if (capitals.equals("capitalsNew.txt")) {
  212.                             Scanner skaner = null;
  213.                             try {
  214.                                 skaner = new Scanner(file2);
  215.                                 while (skaner.hasNext()) {
  216.                                     String question = skaner.nextLine();
  217.                                     String answer = skaner.nextLine();
  218.                                     hashMap.put(question, answer);
  219.                                     N++;
  220.                                 }
  221.                             } finally {
  222.                                 if (skaner != null) {
  223.                                     skaner.close();
  224.                                 }
  225.                             }
  226.                             System.out.println(N + " cards have been loaded.");
  227.                             list.add(N + " cards have been loaded.");
  228.                             System.out.println();
  229.                         } else {
  230.                             System.out.println("File not found.");
  231.                             list.add("File not found.");
  232.                             System.out.println();
  233.                         }
  234.                         //scan.close();
  235.                         break;
  236.  
  237.                     case "export":
  238.                         System.out.println("File name:");
  239.                         list.add("File name:");
  240.                         String capitalsNew = scan.next();
  241.                         list.add(capitalsNew);
  242.                         if (capitalsNew.equals("capitalsNew.txt")) {
  243.                             listaMap(hashMap, hashMap2, file2);
  244.                         } else if (capitalsNew.equals("capitals.txt")) {
  245.                             listaMap(hashMap, hashMap2, file);
  246.                         }
  247.                         System.out.println(hashMap.size() + " cards have been saved.");
  248.                         list.add(hashMap.size() + " cards have been saved.");
  249.                         System.out.println();
  250.                         //scan.close();
  251.                         break;
  252.  
  253.                     case "ask":
  254.                         System.out.println("How many times to ask?");
  255.                         list.add("How many times to ask?");
  256.                         Scanner scan1 = new Scanner(System.in);
  257.                         String enter = scan1.nextLine();
  258.                         list.add(enter);
  259.                         int y = Integer.parseInt(enter);
  260.                         Collection<String> values = hashMap.values();
  261.  
  262.                         for (int j = 0; j < y; ) {
  263.                             for (String randomValue : values) {
  264.                                 int count = 0;
  265.                                 System.out.println("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  266.                                 list.add("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  267.                                 String answer = scan.next();
  268.                                 list.add(answer);
  269.                                 if (answer.equals(randomValue)) {
  270.                                     System.out.println("Correct answer.");
  271.                                     list.add("Correct answer.");
  272.                                 } else if (hashMap.containsValue(answer)) {
  273.                                     count++;
  274.                                     hashMap2.put((String) getKeyFromValue(hashMap, randomValue),
  275.                                             hashMap2.getOrDefault((String) getKeyFromValue(hashMap, randomValue), (int) 0) + count);
  276.                                     System.out.println("Wrong answer. (The correct one is \"" +
  277.                                             randomValue + "\"" + ", " +
  278.                                             "you've just written the definition of \"" +
  279.                                             getKeyFromValue(hashMap, answer) + "\"" + ".)");
  280.                                     list.add("Wrong answer. (The correct one is \"" +
  281.                                             randomValue + "\"" + ", " +
  282.                                             "you've just written the definition of \"" +
  283.                                             getKeyFromValue(hashMap, answer) + "\"" + ".)");
  284.                                 } else {
  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 \"" + randomValue + "\"" + ".");
  289.                                     list.add("Wrong answer. The correct one is \"" + randomValue + "\"" + ".");
  290.                                 }
  291.                                 j++;
  292.                                 if (j == y) {
  293.                                     break;
  294.                                 }
  295.                             }
  296.                         }
  297.                         //scan.close();
  298.                         break;
  299.  
  300.                     case "exit":
  301.                         System.out.println("Bye bye!");
  302.                         list.add("Bye bye!");
  303.                         for (String arg : args) {
  304.                             if (arg.equals("-export")) {
  305.                                 System.out.println(hashMap.size() + " cards have been saved.");
  306.                             }
  307.                         }
  308.                         System.exit(0);
  309.                         break;
  310.  
  311.                     case "log":
  312.                         System.out.println("File name:");
  313.                         list.add("File name:");
  314.                         String log = scan.next();
  315.                         list.add(log);
  316.                         if (log.equals("testLog.txt")) {
  317.                             listaList(list, file3);
  318.                         }
  319.                         System.out.println("The log has been saved.");
  320.                         System.out.println();
  321.                         //scan.close();
  322.                         break;
  323.  
  324.                     case "hardest card":
  325.                         Collection<Integer> values2 = hashMap2.values();
  326.                         int count = 0;
  327.                         for (Integer Value2 : values2) {
  328.                             if (Value2 > 0) {
  329.                                 count++;
  330.                                 list2.add(Value2);
  331.                             }
  332.                         }
  333.                         if (count == 1) {
  334.                             System.out.println("The hardest card is \"" +
  335.                                     getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0) + "\"" +
  336.                                     ". You have " + list2.get(0) + " errors answering it.");
  337.                             list2.clear();
  338.                         } else if (count > 1) {
  339.                             System.out.println("The hardest cards are \"" +
  340.                                     getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0) + "\"" +
  341.                                     ", \"" + getKeyFromValue2(hashMap2, (Integer) list2.get(1)).get(1) + "\"" + "." +
  342.                                     "You have " + list2.get(0) + " errors answering them.");
  343.                             list2.clear();
  344.                         } else {
  345.                             System.out.println("There are no cards with errors.");
  346.                         }
  347.                         break;
  348.  
  349.                     case "reset stats":
  350.                         for (String key : hashMap2.keySet()) {
  351.                             hashMap2.remove(key);
  352.                         }
  353.                         list2.clear();
  354.                         System.out.println("Card statistics has been reset.");
  355.                         break;
  356.                 }
  357.             }
  358.         }
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment