diff --git a/.idea/misc.xml b/.idea/misc.xml
index a165cb3..6db4ca9 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/src/flashcards/Flashcards.java b/src/flashcards/Flashcards.java
new file mode 100644
index 0000000..c19204c
--- /dev/null
+++ b/src/flashcards/Flashcards.java
@@ -0,0 +1,98 @@
+package flashcards;
+import java.util.*;
+import java.io.*;
+//class for create cards. behavior; add,ask,remove,import,export
+
+class Flashcards {
+ private Scanner scanner = new Scanner(System.in);
+ private Map cardToDefinition = new HashMap<>();
+ private Map definitionToCard = new HashMap<>();
+
+ //add cards and definition
+ void add() {
+ System.out.println("The card:");
+ String userCard = scanner.next();
+ System.out.println("The definition of the card");
+ String userDefinition = scanner.next();
+ if (cardToDefinition.containsKey(userCard) || cardToDefinition.containsValue(userDefinition)) {
+ System.out.println("Don't use a duplicated card or a definition");
+ } else {
+ cardToDefinition.put(userCard, userDefinition);
+ definitionToCard.put(userDefinition, userCard);
+ System.out.println("The pair (" + userCard + ":" +userDefinition + ") is added.");
+ }
+ }
+
+ //asking user and checking it
+ void ask() {
+ System.out.println("How many times to ask?");
+ int numOfAsk = scanner.nextInt();
+ for (Map.Entry card : cardToDefinition.entrySet()) {
+ System.out.println("Print the definition of (" + card.getKey() + "):");
+ String answer = scanner.next();
+ if (answer.equals(card.getValue())) {
+ System.out.print("Correct answer.");
+ } else if (cardToDefinition.containsValue(answer)) {
+ // If the definition is wrong but it is presented in another card
+ System.out.print("Wrong answer (the correct one is " + card.getValue()+ ", you've just written a definition of " + definitionToCard.get(answer) + " card).");
+ } else {
+ System.out.print("Wrong answer (the correct one is " + card.getValue() + ").");
+ }
+ if (--numOfAsk==0) {
+ break;
+ }
+ }
+ }
+
+ //remove card
+ void remove() {
+ System.out.println("The card:");
+ String userCard = scanner.nextLine();
+ if (cardToDefinition.containsKey(userCard)) {
+ definitionToCard.remove(cardToDefinition.get(userCard));
+ cardToDefinition.remove(userCard);
+ System.out.println("(" + userCard + ") removed.");
+ } else {
+ System.out.println("Can't remove (" +userCard + "): there is no such card.");
+ }
+ }
+
+
+ // import cards from txt
+ void importCards() {
+ String pathToFile = "E:/_LearnProgramming/IdeaProjects/flashcards/capitals.txt";
+ File file = new File(pathToFile);
+ int counter=0;
+ try (Scanner reader = new Scanner(file)) {
+ while (reader.hasNext()) {
+ String userCard = reader.next();
+ String userDefinition = reader.next();
+ counter++;
+ if (cardToDefinition.containsKey(userCard) || cardToDefinition.containsValue(userDefinition)) {
+ System.out.println("Don't use a duplicated card or a definition");
+ counter--;
+ } else {
+ cardToDefinition.put(userCard, userDefinition);
+ definitionToCard.put(userDefinition, userCard);
+ }
+ }
+ System.out.println(counter + " cards have been loaded.");
+ } catch (FileNotFoundException e) {
+ System.out.println("No file found: " + pathToFile);
+ }
+ }
+
+ void exportCards() {
+ String pathToFile = "E:/_LearnProgramming/IdeaProjects/flashcards/capitals_export.txt";
+ File file = new File(pathToFile);
+ try (PrintWriter printWriter = new PrintWriter(file)) {
+ for (Map.Entry card : cardToDefinition.entrySet()) {
+ printWriter.println(card.getKey());
+ printWriter.println(card.getValue());
+ }
+ System.out.println(cardToDefinition.size() + " cards have been saved.");
+ } catch (IOException e) {
+ System.out.printf("An exception occurs %s", e.getMessage());
+ }
+ }
+}
diff --git a/src/flashcards/Main.java b/src/flashcards/Main.java
index d1b036c..f020217 100644
--- a/src/flashcards/Main.java
+++ b/src/flashcards/Main.java
@@ -1,7 +1,59 @@
-package flashcards;
+/*
+Stage 4: There is a common situation that the answer is wrong for the given card but it's correct for another card. Let's consider situations like this.
+Remove array-based storage. Use two maps (Map): cardToDefinition and definitionToCard. If the definition is wrong but it is presented
+in definitionToCard, output the original card.
+When the user tries to add a duplicated card or a definition, forbid it. For now you are able to implement this without a try catch construction.
+Use the rule: if you can avoid exception-based logic, avoid it!
+
+Stage 5: Improve the application’s interactivity. Ask the user for an action and do it.
+Support these actions:
+add a card: add,
+remove a card: remove,
+load cards from file ("deserialization"): import,
+save cards to file ("serialization"): export,
+ask for definition of some random cards: ask,
+exit the program: exit.
+You can use the following file format. The file consists of pairs of lines. The first line of each pair is a card, and the second line is a definition of the card.
+ */
+package flashcards;
+import java.util.*;
public class Main {
public static void main(String[] args) {
- System.out.print("Hello world!");
+ usingFlashcards();
+ }
+
+ private static void usingFlashcards() {
+ Scanner scanner = new Scanner(System.in);
+ Flashcards flashcards = new Flashcards();
+ String action;
+ do {
+ System.out.println("Input the action (add, remove, import, export, ask, exit):");
+ action = scanner.next();
+ switch (action) {
+ case "add":
+ flashcards.add();
+ break;
+ case "remove":
+ flashcards.remove();
+ break;
+ case "ask":
+ flashcards.ask();
+ break;
+ case "import":
+ flashcards.importCards();
+ break;
+ case "export":
+ flashcards.exportCards();
+ break;
+ case "exit":
+ break;
+ default:
+ System.out.println("Wrong action!");
+ break;
+ }
+ } while (!action.equals("exit"));
+ scanner.close();
+ System.out.println("Bye bye!");
}
}
\ No newline at end of file