Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions src/flashcards/Flashcards.java
Original file line number Diff line number Diff line change
@@ -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<String, String> cardToDefinition = new HashMap<>();
private Map<String, String> 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<String, String> 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<String, String> 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());
}
}
}
56 changes: 54 additions & 2 deletions src/flashcards/Main.java
Original file line number Diff line number Diff line change
@@ -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<String, String>): 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!");
}
}