Sergei_Langin

Sergei FlashCards Stage 7 Full

Mar 15th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.22 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 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\\capitals.txt");
  72.         File file3 = new File("testLog.txt");
  73.  
  74.         if (args[0].equals("-import")) {
  75.             if (args[1].equals("capitals.txt")) {
  76.                 int N = 0;
  77.                 Scanner skaner = null;
  78.                 try {
  79.                     skaner = new Scanner(file);
  80.                     while (skaner.hasNext()) {
  81.                         String question = skaner.nextLine();
  82.                         String answer = skaner.nextLine();
  83.                         String error = skaner.nextLine();
  84.                         hashMap.put(question, answer);
  85.                         hashMap2.put(question, Integer.valueOf(error));
  86.                         N++;
  87.                     }
  88.                 } finally {
  89.                     if (skaner != null) {
  90.                         skaner.close();
  91.                     }
  92.                 }
  93.                 System.out.println(N + " cards have been loaded.");
  94.                 list.add(N + " cards have been loaded.");
  95.             } else if (args[1].equals("capitalsNew.txt")) {
  96.                 Scanner skaner = null;
  97.                 int N = 0;
  98.                 try {
  99.                    
  100.                     skaner = new Scanner(file2);
  101.                     while (skaner.hasNext()) {
  102.                         String question = skaner.nextLine();
  103.                         String answer = skaner.nextLine();
  104.                         hashMap.put(question, answer);
  105.                         N++;
  106.                     }
  107.                 } finally {
  108.                     if (skaner != null) {
  109.                         skaner.close();
  110.                     }
  111.                 }
  112.                 System.out.println(N + " cards have been loaded.");
  113.                 list.add(N + " cards have been loaded.");
  114.                 System.out.println();
  115.             }
  116.            
  117.         } else if (args[0].equals("-export")) {
  118.                 if (args[1].equals("capitalsNew.txt")) {
  119.                     listaMap(hashMap, hashMap2, file2);
  120.                 } else if (args[1].equals("capitals.txt")) {
  121.                     listaMap(hashMap, hashMap2, file);
  122.                 }
  123.                 if (args[2].equals("exit")) {
  124.                     System.out.println("Bye bye!");
  125.                     System.out.println(hashMap.size() + " cards have been saved.");
  126.                     System.exit(0);
  127.                     list.add(hashMap.size() + " cards have been saved.");
  128.                     System.out.println();
  129.                 }
  130.         }
  131.         for (; ; ) {
  132.             System.out.println("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
  133.             list.add("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
  134.             Scanner scan = new Scanner(System.in);
  135.             String action = scan.nextLine();
  136.             list.add(action);
  137.  
  138.             switch (action) {
  139.                 case "add":
  140.                     System.out.println("The card:");
  141.                     list.add("The card:");
  142.                     String frontCard = scan.nextLine();
  143.                     list.add(frontCard);
  144.                     if (hashMap.containsKey(frontCard)) {
  145.                         System.out.println("The card \"" + frontCard + "\"" + " already exists.");
  146.                         list.add("The card \"" + frontCard + "\"" + " already exists.");
  147.                         System.out.println();
  148.                         break;
  149.                     }
  150.                     System.out.println("The definition of the card:");
  151.                     list.add("The definition of the card:");
  152.                     String backCard = scan.nextLine();
  153.                     list.add(backCard);
  154.                     if (hashMap.containsValue(backCard)) {
  155.                         System.out.println("The definition \"" + backCard + "\"" + " already exists.");
  156.                         list.add("The definition \"" + backCard + "\"" + " already exists.");
  157.                         System.out.println();
  158.                         break;
  159.                     }
  160.                     hashMap.put(frontCard, backCard);
  161.                     System.out.println("The pair (\"" + frontCard + "\"" + ":\"" + backCard + "\"" + ") has been added.");
  162.                     list.add("The pair (\"" + frontCard + "\"" + ":\"" + backCard + "\"" + ") has been added.");
  163.                     System.out.println();
  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.                     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.                     break;
  235.  
  236.                 case "export":
  237.                     System.out.println("File name:");
  238.                     list.add("File name:");
  239.                     String capitalsNew = scan.next();
  240.                     list.add(capitalsNew);
  241.                     if (capitalsNew.equals("capitalsNew.txt")) {
  242.                         listaMap(hashMap, hashMap2, file2);
  243.                     } else if (capitalsNew.equals("capitals.txt")) {
  244.                         listaMap(hashMap, hashMap2, file);
  245.                     }
  246.                     System.out.println(hashMap.size() + " cards have been saved.");
  247.                     list.add(hashMap.size() + " cards have been saved.");
  248.                     System.out.println();
  249.                     break;
  250.  
  251.                 case "ask":
  252.                     System.out.println("How many times to ask?");
  253.                     list.add("How many times to ask?");
  254.                     Scanner scan1 = new Scanner(System.in);
  255.                     String enter = scan1.nextLine();
  256.                     list.add(enter);
  257.                     int y = Integer.parseInt(enter);
  258.                     Collection<String> values = hashMap.values();
  259.  
  260.                     for (int j = 0; j < y;) {
  261.                         for (String randomValue : values) {
  262.                             int count = 0;
  263.                             System.out.println("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  264.                             list.add("Print the definition of \"" + getKeyFromValue(hashMap, randomValue) + "\"" + ":");
  265.                             String answer = scan.next();
  266.                             list.add(answer);
  267.                             if (answer.equals(randomValue)) {
  268.                                 System.out.println("Correct answer.");
  269.                                 list.add("Correct answer.");
  270.                             } else if (hashMap.containsValue(answer)) {
  271.                                 count++;
  272.                                 hashMap2.put((String) getKeyFromValue(hashMap, randomValue),
  273.                                         hashMap2.getOrDefault((String) getKeyFromValue(hashMap, randomValue), (int) 0) + count);
  274.                                 System.out.println("Wrong answer. (The correct one is \"" +
  275.                                         randomValue + "\"" + ", " +
  276.                                         "you've just written the definition of \"" +
  277.                                         getKeyFromValue(hashMap, answer) + "\"" + ".)");
  278.                                 list.add("Wrong answer. (The correct one is \"" +
  279.                                         randomValue + "\"" + ", " +
  280.                                         "you've just written the definition of \"" +
  281.                                         getKeyFromValue(hashMap, answer) + "\"" + ".)");
  282.                             } else {
  283.                                 count++;
  284.                                 hashMap2.put((String) getKeyFromValue(hashMap, randomValue),
  285.                                         hashMap2.getOrDefault((String) getKeyFromValue(hashMap, randomValue), (int) 0) + count);
  286.                                 System.out.println("Wrong answer. The correct one is \"" + randomValue + "\"" + ".");
  287.                                 list.add("Wrong answer. The correct one is \"" + randomValue + "\"" + ".");
  288.                             }
  289.                             j++;
  290.                             if (j == y) {
  291.                                 break;
  292.                             }
  293.                         }
  294.                     }
  295.                     break;
  296.  
  297.                 case "exit":
  298.                     System.out.println("Bye bye!");
  299.                     list.add("Bye bye!");
  300.                     System.exit(0);
  301.                     break;
  302.  
  303.                 case "log":
  304.                     System.out.println("File name:");
  305.                     list.add("File name:");
  306.                     String log = scan.next();
  307.                     list.add(log);
  308.                     if (log.equals("testLog.txt")) {
  309.                         listaList(list, file3);
  310.                     }
  311.                     System.out.println("The log has been saved.");
  312.                     System.out.println();
  313.                     break;
  314.  
  315.                 case "hardest card":
  316.                     Collection<Integer> values2 = hashMap2.values();
  317.                     int count = 0;
  318.                     for (Integer Value2 : values2) {
  319.                         if (Value2 > 0) {
  320.                             count++;
  321.                             list2.add(Value2);
  322.                         }
  323.                     }
  324.                     if (count == 1) {
  325.                         System.out.println("The hardest card is \"" +
  326.                                 getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0)  + "\"" +
  327.                                 ". You have " + list2.get(0) + " errors answering it.");
  328.                         list2.clear();
  329.                     } else if (count > 1) {
  330.                         System.out.println("The hardest cards are \"" +
  331.                                 getKeyFromValue2(hashMap2, (Integer) list2.get(0)).get(0) + "\"" +
  332.                                 ", \"" + getKeyFromValue2(hashMap2, (Integer) list2.get(1)).get(1)  + "\"" + "." +
  333.                                 "You have " + list2.get(0) + " errors answering them.");
  334.                         list2.clear();
  335.                     } else {
  336.                         System.out.println("There are no cards with errors.");
  337.                     }
  338.                     break;
  339.  
  340.                 case "reset stats":
  341.                     for (String key : hashMap2.keySet()) {
  342.                         hashMap2.remove(key);
  343.                     }
  344.                     list2.clear();
  345.                     System.out.println("Card statistics has been reset.");
  346.                     break;
  347.             }
  348.         }
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment