From 894425176d6009378dbbd42ea7b322a88624fe0a Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Sun, 3 Feb 2019 12:56:24 +1000 Subject: [PATCH 1/8] Update Main.java --- src/flashcards/Main.java | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index d1b036c..2e8a45e 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -1,7 +1,33 @@ package flashcards; +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.print("Hello world!"); + Scanner sc = new Scanner(System.in); + + System.out.println("Input the number of cards: "); + int n = sc.nextInt(); + int i; + String possible = sc.nextLine(); + String[] questions = new String[n]; + String[] answers = new String[n]; + + for (i = 0; i < n; i++) { + System.out.println("The card #" + (i+1) + ":"); + questions[i] = sc.nextLine(); + System.out.println("The definition of the card #" + (i+1) + ":"); + answers[i] = sc.nextLine(); + } + + for (i = 0; i < n; i++) { + System.out.println("Print the definition of \"" + questions[i] + "\":"); + possible = sc.nextLine(); + if (possible.equalsIgnoreCase(answers[i])) { + System.out.print("Correct answer. "); + } else { + System.out.println("Wrong answer (the correct one is \"" + answers[i] +"\")."); + } + } + } -} \ No newline at end of file +} From a74277d4215a029eee5e7bce6decc0ad2439e09d Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Tue, 5 Feb 2019 17:27:22 +1000 Subject: [PATCH 2/8] Update Main.java --- src/flashcards/Main.java | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index 2e8a45e..8b13789 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -1,33 +1 @@ -package flashcards; -import java.util.Scanner; -public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.println("Input the number of cards: "); - int n = sc.nextInt(); - int i; - String possible = sc.nextLine(); - String[] questions = new String[n]; - String[] answers = new String[n]; - - for (i = 0; i < n; i++) { - System.out.println("The card #" + (i+1) + ":"); - questions[i] = sc.nextLine(); - System.out.println("The definition of the card #" + (i+1) + ":"); - answers[i] = sc.nextLine(); - } - - for (i = 0; i < n; i++) { - System.out.println("Print the definition of \"" + questions[i] + "\":"); - possible = sc.nextLine(); - if (possible.equalsIgnoreCase(answers[i])) { - System.out.print("Correct answer. "); - } else { - System.out.println("Wrong answer (the correct one is \"" + answers[i] +"\")."); - } - } - - } -} From eb90a162625f1f73ceb4c21b8345567d9dab773d Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Tue, 5 Feb 2019 17:47:26 +1000 Subject: [PATCH 3/8] Update Main.java --- src/flashcards/Main.java | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index 8b13789..1f2d7b8 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -1 +1,48 @@ +import java.util.*; +public class flashcards { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n; + String card; + String definition; + String answer; + Map cardToDefinition = new LinkedHashMap<>(); + Map definitionToCard = new LinkedHashMap<>(); + System.out.println("Input the number of cards:"); + + while (true) { + try { + n = Integer.parseInt(sc.nextLine()); + break; + } catch (NumberFormatException e) { + System.out.println("There is an error (input is not an integer number). Please try again."); + } + } + + for (int i = 1; i <= n; i++) { + System.out.println("The card #" + i + ":"); + card = sc.nextLine(); + System.out.println("The definition of the card #" + i + ":"); + definition = sc.nextLine(); + cardToDefinition.put(card, definition); + definitionToCard.put(definition, card); + } + + for (Map.Entry couple: cardToDefinition.entrySet()) { + System.out.println("Print the definition of \"" + couple.getKey() + "\":"); + answer = sc.nextLine(); + if (couple.getValue().equals(answer)) { + System.out.print("Correct answer. "); + } else if (definitionToCard.containsKey(answer)) { + System.out.print("Wrong answer (the correct one is \"" + couple.getValue() + + "\", you've just written a definition of \"" + definitionToCard.get(answer) + "\" card)."); + } else { + System.out.print("Wrong answer (the correct one is \"" + couple.getValue() + "\")."); + } + } + + + + } +} From 1d24dd19f95b46c6c69b08a8eb6a3f2f31396146 Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Tue, 5 Feb 2019 17:48:28 +1000 Subject: [PATCH 4/8] Update Main.java --- src/flashcards/Main.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index 1f2d7b8..1dea386 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -1,5 +1,7 @@ +package flashcards; + import java.util.*; -public class flashcards { +public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; From 5b8b3ed12ac27cd9f4a3a91c2948f27a7b686152 Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Tue, 5 Feb 2019 23:18:22 +1000 Subject: [PATCH 5/8] Update Main.java --- src/flashcards/Main.java | 151 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 143 insertions(+), 8 deletions(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index 1dea386..572817c 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -1,18 +1,155 @@ package flashcards; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.*; + public class Main { + + private static Map cardToDefinition = new LinkedHashMap<>(); + private static Map definitionToCard = new LinkedHashMap<>(); + private static boolean isEnabled = true; + private final static Scanner sc = new Scanner(System.in); + private static Random random = new Random(); + + + private static void chooseAction() { + System.out.println("Input the action (add, remove, import, export, ask, exit):"); + String action = sc.nextLine().trim(); + if (action.equals("add")) { + add(); + } else if (action.equals("remove")) { + remove(); + } else if (action.equals("import")) { + importCards(); + } else if (action.equals("export")) { + exportCards(); + } else if (action.equals("ask")) { + ask(); + } else if (action.equals("exit")) { + System.out.println("Bye bye!"); + isEnabled = false; + } else { + System.out.println("Sorry, I cannot understand."); + } + System.out.println(); + } + + + private static void add() { + System.out.println("The card:"); + String card = sc.nextLine(); + System.out.println("The definition:"); + String definition = sc.nextLine(); + cardToDefinition.put(card, definition); + definitionToCard.put(definition, card); + System.out.println("The pair (\"" + card + "\":\"" + definition + "\") is added."); + } + + + private static void remove() { + System.out.println("The card:"); + String card = sc.nextLine(); + + if (cardToDefinition.containsKey(card)) { + definitionToCard.remove(cardToDefinition.get(card)); + cardToDefinition.remove(card); + System.out.println("The card \"" + card +"\" was removed."); + } else { + System.out.println("Can't remove \"" + card + "\": there is no such card."); + } + + } + + + private static void importCards() { + System.out.println("File name:"); + String fileName = sc.nextLine(); + File file = new File(fileName); + try (Scanner reader = new Scanner(file)) { + int amount = 0; + while (reader.hasNextLine()) { + String card = reader.nextLine(); + String definition = reader.nextLine(); + cardToDefinition.put(card, definition); + definitionToCard.put(definition, card); + amount++; + } + System.out.println(amount + " cards have been loaded."); + + } catch (IOException e) { + System.out.println("There is no file with this name"); + } + } + + + private static void exportCards() { + System.out.println("File name:"); + String fileName = sc.nextLine(); + try (FileWriter writer = new FileWriter(fileName)) { + for (Map.Entry couple: cardToDefinition.entrySet()) { + writer.write(couple.getKey() + "\n"); + writer.write(couple.getValue() + "\n"); + } + System.out.println(cardToDefinition.size() + " cards have been saved."); + + } catch (IOException e) { + System.out.println("There is an error with file name."); + } + } + + + private static void ask() { + System.out.println("How many times to ask?"); + int amount = Integer.parseInt(sc.nextLine()); + String answer, key, value; + + for (int i = 0; i < amount; i++) { + key = randomKey(); + value = cardToDefinition.get(key); + System.out.println("Print the definition of \"" + key + "\":"); + answer = sc.nextLine(); + if (value.equals(answer)) { + System.out.print("Correct answer. "); + } else if (definitionToCard.containsKey(answer)) { + System.out.print("Wrong answer (the correct one is \"" + value + + "\", you've just written a definition of \"" + definitionToCard.get(answer) + "\" card)."); + } else { + System.out.print("Wrong answer (the correct one is \"" + value + "\")."); + } + } + + } + + + private static String randomKey() { + int number = random.nextInt(cardToDefinition.size()); + int i = 0; + + for (String key: cardToDefinition.keySet()) { + if (i == number) { + return key; + } + i++; + } + return null; + } + + public static void main(String[] args) { - Scanner sc = new Scanner(System.in); + + while (isEnabled) { + chooseAction(); + } + + + /* int n; String card; String definition; String answer; - Map cardToDefinition = new LinkedHashMap<>(); - Map definitionToCard = new LinkedHashMap<>(); - System.out.println("Input the number of cards:"); - while (true) { try { n = Integer.parseInt(sc.nextLine()); @@ -43,8 +180,6 @@ public static void main(String[] args) { System.out.print("Wrong answer (the correct one is \"" + couple.getValue() + "\")."); } } - - - + */ } } From 98f7d4801a3ae35b2fd25a9131288e35c85150b3 Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Wed, 6 Feb 2019 15:44:13 +1000 Subject: [PATCH 6/8] Update Main.java --- src/flashcards/Main.java | 250 +++++++++++++++++++++++++-------------- 1 file changed, 161 insertions(+), 89 deletions(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index 572817c..df7ccf6 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -5,124 +5,234 @@ import java.io.IOException; import java.util.*; -public class Main { + +public class flashcards { + + + private static String userNextLine() { + String str = scanner.nextLine(); + logList.add(str); + return str; + } + + + private static void userPrintln(String string) { + if (string != null) { + System.out.println(string); + logList.add(string); + } else { + System.out.println(); + } + } + + + private static void userPrint(String string) { + System.out.print(string); + logList.add(string); + } + + private static Map cardToDefinition = new LinkedHashMap<>(); private static Map definitionToCard = new LinkedHashMap<>(); + private static Map cardToMistakes = new LinkedHashMap<>(); + private static ArrayList logList = new ArrayList<>(); private static boolean isEnabled = true; - private final static Scanner sc = new Scanner(System.in); + private static final Scanner scanner = new Scanner(System.in); private static Random random = new Random(); private static void chooseAction() { - System.out.println("Input the action (add, remove, import, export, ask, exit):"); - String action = sc.nextLine().trim(); - if (action.equals("add")) { - add(); - } else if (action.equals("remove")) { - remove(); - } else if (action.equals("import")) { - importCards(); - } else if (action.equals("export")) { - exportCards(); - } else if (action.equals("ask")) { - ask(); - } else if (action.equals("exit")) { - System.out.println("Bye bye!"); - isEnabled = false; - } else { - System.out.println("Sorry, I cannot understand."); + userPrintln("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):"); + String action = userNextLine().trim(); + switch (action) { + case "add": + add(); + break; + case "remove": + remove(); + break; + case "import": + importCards(); + break; + case "export": + exportCards(); + break; + case "ask": + ask(); + break; + case "exit": + userPrintln("Bye bye!"); + isEnabled = false; + break; + case "log": + log(); + break; + case "hardest card": + hardestCard(); + break; + case "reset stats": + resetStats(); + break; + default: + userPrintln("Sorry, I cannot understand. Please try again."); + break; } - System.out.println(); + + userPrintln(null); + } private static void add() { - System.out.println("The card:"); - String card = sc.nextLine(); - System.out.println("The definition:"); - String definition = sc.nextLine(); + userPrintln("The card:"); + String card = userNextLine(); + userPrintln("The definition:"); + String definition = userNextLine(); + cardToDefinition.put(card, definition); definitionToCard.put(definition, card); - System.out.println("The pair (\"" + card + "\":\"" + definition + "\") is added."); + cardToMistakes.put(card, 0); + userPrintln("The pair (\"" + card + "\":\"" + definition + "\") is added."); } private static void remove() { - System.out.println("The card:"); - String card = sc.nextLine(); + userPrintln("The card:"); + String card = userNextLine(); if (cardToDefinition.containsKey(card)) { definitionToCard.remove(cardToDefinition.get(card)); cardToDefinition.remove(card); - System.out.println("The card \"" + card +"\" was removed."); + cardToMistakes.remove(card); + userPrintln("The card \"" + card +"\" was removed."); } else { - System.out.println("Can't remove \"" + card + "\": there is no such card."); + userPrintln("Can't remove \"" + card + "\": there is no such card."); } } private static void importCards() { - System.out.println("File name:"); - String fileName = sc.nextLine(); + userPrintln("File name:"); + String fileName = userNextLine(); + File file = new File(fileName); try (Scanner reader = new Scanner(file)) { int amount = 0; while (reader.hasNextLine()) { String card = reader.nextLine(); String definition = reader.nextLine(); + String mistakes = reader.nextLine(); cardToDefinition.put(card, definition); definitionToCard.put(definition, card); + cardToMistakes.put(card, Integer.parseInt(mistakes)); amount++; } - System.out.println(amount + " cards have been loaded."); + userPrintln(amount + " cards have been loaded."); } catch (IOException e) { - System.out.println("There is no file with this name"); + userPrintln("There is no file with this name"); } } private static void exportCards() { - System.out.println("File name:"); - String fileName = sc.nextLine(); + userPrintln("File name:"); + String fileName = userNextLine(); + try (FileWriter writer = new FileWriter(fileName)) { for (Map.Entry couple: cardToDefinition.entrySet()) { writer.write(couple.getKey() + "\n"); writer.write(couple.getValue() + "\n"); + writer.write(cardToMistakes.get(couple.getKey()) + "\n"); } - System.out.println(cardToDefinition.size() + " cards have been saved."); + userPrintln(cardToDefinition.size() + " cards have been saved."); } catch (IOException e) { - System.out.println("There is an error with file name."); + userPrintln("There is an error with file name."); } } private static void ask() { - System.out.println("How many times to ask?"); - int amount = Integer.parseInt(sc.nextLine()); + userPrintln("How many times to ask?"); + int amount = Integer.parseInt(userNextLine()); + String answer, key, value; - for (int i = 0; i < amount; i++) { - key = randomKey(); - value = cardToDefinition.get(key); - System.out.println("Print the definition of \"" + key + "\":"); - answer = sc.nextLine(); - if (value.equals(answer)) { - System.out.print("Correct answer. "); - } else if (definitionToCard.containsKey(answer)) { - System.out.print("Wrong answer (the correct one is \"" + value + - "\", you've just written a definition of \"" + definitionToCard.get(answer) + "\" card)."); - } else { - System.out.print("Wrong answer (the correct one is \"" + value + "\")."); + if (amount <= cardToDefinition.size()) { + for (int i = 0; i < amount; i++) { + key = randomKey(); + value = cardToDefinition.get(key); + userPrintln("Print the definition of \"" + key + "\":"); + answer = userNextLine(); + + if (value.equals(answer)) { + userPrint("Correct answer. "); + } else if (definitionToCard.containsKey(answer)) { + cardToMistakes.put(key, cardToMistakes.get(key) + 1); + userPrint("Wrong answer (the correct one is \"" + value + + "\", you've just written a definition of \"" + definitionToCard.get(answer) + "\" card). "); + } else { + cardToMistakes.put(key, cardToMistakes.get(key) + 1); + userPrint("Wrong answer (the correct one is \"" + value + "\"). "); + } + } + } else { + userPrintln("Sorry, I have only " + amount + " card(s)."); + } + + } + + + private static void log() { + userPrintln("File name:"); + String fileName = userNextLine(); + + try (FileWriter writer = new FileWriter(fileName)) { + for (String line: logList) { + writer.write(line + "\n"); } + userPrintln("The log has been saved."); + + } catch (IOException e) { + userPrintln("There is an error with file name."); } } + private static void hardestCard() { + + if (cardToMistakes.size() > 0) { + int max = -1; + String cardWithMaxMistakes = null; + + for (Map.Entry couple: cardToMistakes.entrySet()) { + if (couple.getValue() > max) { + cardWithMaxMistakes = couple.getKey(); + max = couple.getValue(); + } + } + userPrintln("The hardest card is \"" + cardWithMaxMistakes + "\". " + + "You have " + max + " errors answering it."); + + } else { + userPrintln("No cards are here."); + } + + } + + + private static void resetStats() { + for (String key: cardToMistakes.keySet()) { + cardToMistakes.put(key, 0); + } + userPrintln("Stats reset"); + } + + private static String randomKey() { int number = random.nextInt(cardToDefinition.size()); int i = 0; @@ -143,43 +253,5 @@ public static void main(String[] args) { chooseAction(); } - - /* - int n; - String card; - String definition; - String answer; - System.out.println("Input the number of cards:"); - while (true) { - try { - n = Integer.parseInt(sc.nextLine()); - break; - } catch (NumberFormatException e) { - System.out.println("There is an error (input is not an integer number). Please try again."); - } - } - - for (int i = 1; i <= n; i++) { - System.out.println("The card #" + i + ":"); - card = sc.nextLine(); - System.out.println("The definition of the card #" + i + ":"); - definition = sc.nextLine(); - cardToDefinition.put(card, definition); - definitionToCard.put(definition, card); - } - - for (Map.Entry couple: cardToDefinition.entrySet()) { - System.out.println("Print the definition of \"" + couple.getKey() + "\":"); - answer = sc.nextLine(); - if (couple.getValue().equals(answer)) { - System.out.print("Correct answer. "); - } else if (definitionToCard.containsKey(answer)) { - System.out.print("Wrong answer (the correct one is \"" + couple.getValue() + - "\", you've just written a definition of \"" + definitionToCard.get(answer) + "\" card)."); - } else { - System.out.print("Wrong answer (the correct one is \"" + couple.getValue() + "\")."); - } - } - */ } } From 9fef7f0981cdb031e8d9b2e9eb507ade7bc91c7e Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Wed, 6 Feb 2019 15:45:08 +1000 Subject: [PATCH 7/8] Update Main.java From 3b5672dd0b7fc0cf0b558e1a55ab8d9fdfef8fbd Mon Sep 17 00:00:00 2001 From: getPL <47267794+getPL@users.noreply.github.com> Date: Wed, 6 Feb 2019 16:01:25 +1000 Subject: [PATCH 8/8] Update Main.java --- src/flashcards/Main.java | 45 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java index df7ccf6..9c0fe05 100644 --- a/src/flashcards/Main.java +++ b/src/flashcards/Main.java @@ -6,7 +6,7 @@ import java.util.*; -public class flashcards { +public class Main { private static String userNextLine() { @@ -38,6 +38,8 @@ private static void userPrint(String string) { private static Map cardToMistakes = new LinkedHashMap<>(); private static ArrayList logList = new ArrayList<>(); private static boolean isEnabled = true; + private static boolean needExport = false; + private static String exportFileName; private static final Scanner scanner = new Scanner(System.in); private static Random random = new Random(); @@ -64,6 +66,9 @@ private static void chooseAction() { case "exit": userPrintln("Bye bye!"); isEnabled = false; + if (needExport) { + doExport(exportFileName); + } break; case "log": log(); @@ -247,8 +252,46 @@ private static String randomKey() { } + private static void doImport(String fileName) { + File file = new File(fileName); + try (Scanner reader = new Scanner(file)) { + while (reader.hasNextLine()) { + String card = reader.nextLine(); + String definition = reader.nextLine(); + String mistakes = reader.nextLine(); + cardToDefinition.put(card, definition); + definitionToCard.put(definition, card); + cardToMistakes.put(card, Integer.parseInt(mistakes)); + } + } catch (IOException e) {} + } + + + private static void doExport(String fileName) { + + try (FileWriter writer = new FileWriter(fileName)) { + for (Map.Entry couple: cardToDefinition.entrySet()) { + writer.write(couple.getKey() + "\n"); + writer.write(couple.getValue() + "\n"); + writer.write(cardToMistakes.get(couple.getKey()) + "\n"); + } + } catch (IOException e) {} + } + + public static void main(String[] args) { + if (args.length % 2 == 0) { + for (int i = 0; i < args.length; i+=2) { + if (args[i].equals("-import")) { + doImport(args[i+1]); + } else if (args[i].equals("-export")) { + needExport = true; + exportFileName = args[i+1]; + } + } + } + while (isEnabled) { chooseAction(); }