Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main {
- static Scanner scanner;
- static Map<String, String> cardToDefinition;
- static Map<String, String> definitionToCard;
- public static void main(String[] args) {
- scanner = new Scanner(System.in);
- cardToDefinition = new HashMap<>();
- definitionToCard = new HashMap<>();
- while (true) {
- System.out.println("Input the action (add, remove, import, export, ask, exit):");
- String input = scanner.nextLine();
- switch(input) {
- case "add":
- add();
- break;
- case "remove":
- remove();
- break;
- case "import":
- importCards();
- break;
- case "export":
- exportCards();
- break;
- case "ask":
- ask();
- break;
- case "exit":
- System.out.println("Bye bye!");
- System.exit(0);
- break;
- }
- }
- }
- public static void add() {
- System.out.println("The card:");
- String userCard = scanner.nextLine();
- if (cardToDefinition.containsKey(userCard)) {
- System.out.println("The card "+ "\"" + userCard + "\"" +" already exists.");
- return;
- }
- System.out.println("The definition of the card:");
- String userDefinition = scanner.nextLine();
- if (cardToDefinition.containsValue(userDefinition)) {
- System.out.println("The definition " + "\"" + userDefinition + "\"" + " already exists.");
- } else {
- cardToDefinition.put(userCard, userDefinition);
- definitionToCard.put(userDefinition, userCard);
- System.out.println("The pair (" + "\"" + userCard + "\"" + ":" + "\"" + userDefinition + "\"" + ") has been added.");
- }
- }
- public static 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("The card has been removed.");
- } else {
- System.out.println("Can't remove " + "\"" +userCard + "\"" + ": there is no such card.");
- }
- }
- public static void ask() {
- System.out.println("How many times to ask?");
- int numOfAsk = Integer.parseInt(scanner.nextLine());
- int numb = 0;
- for (Map.Entry<String, String> card : cardToDefinition.entrySet()) {
- if(numb == numOfAsk) {
- break;
- }
- System.out.println("Print the definition of " + "\"" + card.getKey() + "\"" + ":");
- String answer = scanner.nextLine();
- if (answer.equals(card.getValue())) {
- System.out.println("Correct answer.");
- } else if (cardToDefinition.containsValue(answer)) {
- System.out.println("Wrong answer . The correct one is " + "\"" + card.getValue() + "\"" + ", you've just written a definition of " + "\"" + definitionToCard.get(answer) + "\"" + " card.");
- } else {
- System.out.println("Wrong answer . The correct one is " + "\"" + card.getValue() + "\"" + ".");
- }
- numb++;
- }
- }
- public static void importCards() {
- System.out.println("File name:");
- String pathToFile = scanner.nextLine();
- 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("File not found.");
- }
- }
- public static void exportCards() {
- System.out.println("File name:");
- String pathToFile = scanner.nextLine();
- 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());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement