From 9d037b5a5bc8013ad3badbaf0abda1fac689eda2 Mon Sep 17 00:00:00 2001 From: Cametendo Date: Sun, 26 Oct 2025 16:23:37 +0100 Subject: [PATCH] Update Main.java --- Cards-Gate/src/Main.java | 233 ++++++++++++++++++++++++--------------- 1 file changed, 143 insertions(+), 90 deletions(-) diff --git a/Cards-Gate/src/Main.java b/Cards-Gate/src/Main.java index 9ec420c..81643e6 100644 --- a/Cards-Gate/src/Main.java +++ b/Cards-Gate/src/Main.java @@ -1,12 +1,10 @@ -import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.List; -import com.google.gson.Gson; -import com.google.gson.internal.bind.util.ISO8601Utils; - -import java.io.FileReader; import java.util.Scanner; +import com.google.gson.Gson; + class CardsClass { public String question = ""; public String answer = ""; @@ -15,101 +13,156 @@ class CardsClass { this.question = question; this.answer = answer; } - } + class Wrapper { List cards; } + public class Main { + + private static final Scanner userAnswers = new Scanner(System.in); + private static final Scanner jsonPath = new Scanner(System.in); + private static final Gson gson = new Gson(); + public static void main(String[] args) { - Gson gson = new Gson(); - Scanner jsonPath = new Scanner(System.in); - Scanner userAnswers = new Scanner(System.in); - System.out.println("Welcome to: Cards Gate"); - System.out.println("-----------------------------------------------------------------------"); - System.out.println("| ##### ##### |"); - System.out.println("| # # ## ##### ##### #### ### # # ## ##### ###### |"); - System.out.println("| # # # # # # # # ### # # # # # |"); - System.out.println("| # # # # # # # #### # #### # # # ##### |"); - System.out.println("| # ###### ##### # # # ### # # ###### # # |"); - System.out.println("| # # # # # # # # # # ### # # # # # # |"); - System.out.println("| ##### # # # # ##### #### # ##### # # # ###### |"); - System.out.println("| # |"); - System.out.println("-----------------------------------------------------------------------"); - String start = ""; - while (true) { - System.out.println("Would you like to start or read the manual? (y/n/m)"); - start = userAnswers.nextLine(); + printWelcome(); + mainMenu(); + } - if (start.equalsIgnoreCase("n")) { - System.exit(0); // Exit program - } else if (start.equalsIgnoreCase("m")) { - System.out.println("The rules:"); - System.out.println("You are allowed to redo a card / deck as many times as you want."); - System.out.println("You can skip a question, look at the answer or go back to the previous question."); - System.out.println("You always have a re-order of your cards / deck"); - System.out.println("That's it. For more details: Read the manual"); - System.out.println(""); - System.out.println("Have fun!!"); - // Loop back to ask again - } else if (start.equalsIgnoreCase("y")) { - System.out.println("Please enter the path of the file you would like to read: "); - String filePath = jsonPath.nextLine(); - Wrapper wrapper = new Wrapper(); - try (Reader fileReader = new FileReader(filePath)) { - wrapper = new Gson().fromJson(fileReader, Wrapper.class); - } catch (IOException e) { - e.printStackTrace(); - } + private static void printWelcome() { + System.out.println("Welcome to: Cards:Gate"); + System.out.println("-----------------------------------------------------------------------"); + System.out.println("| ##### ##### |"); + System.out.println("| # # ## ##### ##### #### ### # # ## ##### ###### |"); + System.out.println("| # # # # # # # # ### # # # # # |"); + System.out.println("| # # # # # # # #### # #### # # # ##### |"); + System.out.println("| # ###### ##### # # # ### # # ###### # # |"); + System.out.println("| # # # # # # # # # # ### # # # # # # |"); + System.out.println("| ##### # # # # ##### #### # ##### # # # ###### |"); + System.out.println("| # |"); + System.out.println("-----------------------------------------------------------------------"); + } - int cardNumber = 0; - while (true) { - String question = wrapper.cards.get(cardNumber).question; - String answer = wrapper.cards.get(cardNumber).answer; + private static void mainMenu() { + while (true) { + System.out.println("Would you like to start or read the manual? (y/n/m)"); + String start = userAnswers.nextLine(); - System.out.println("What's: " + question + "?"); - System.out.println("Available options: back, answer, skip"); - String userInput = userAnswers.nextLine(); - /* - In-session Settings - */ - if (userInput.equalsIgnoreCase("back")) { - cardNumber--; - } else if (userInput.equalsIgnoreCase("answer")) { - System.out.println(answer); - } else if (userInput.equalsIgnoreCase("skip")) { - cardNumber++; - } - - /* - Deck Settings - */ - if (userInput.equalsIgnoreCase(answer)) { - cardNumber++; - if (cardNumber >= wrapper.cards.size()) { - System.out.println("You've answered all cards"); - System.out.println("Redo Deck? (y/n)"); - String finishedDeck = userAnswers.nextLine(); - if (finishedDeck.equalsIgnoreCase("y")) { - System.out.println("Restarting Deck..."); - cardNumber = 0; // Restart deck - } else if(finishedDeck.equalsIgnoreCase("n")) { - System.out.println("Another Deck? (y/n)"); - String anotherDeck = userAnswers.nextLine(); - if (anotherDeck.equalsIgnoreCase("y")) { - break; // Exit card loop - } else if (anotherDeck.equalsIgnoreCase("n")) { - System.exit(0); - } - } - } else { - System.out.println("Wrong, try again..."); - } - } - } - } else { - System.out.println("Invalid choice, please enter y, n or m."); + if (start.equalsIgnoreCase("n")) { + exitGame(); + } else if (start.equalsIgnoreCase("m")) { + printManual(); + } else if (start.equalsIgnoreCase("y")) { + startGame(); + } else { + System.out.println("Invalid choice, please enter y, n or m."); } } } + + private static void printManual() { + System.out.println("The rules:"); + System.out.println("You are allowed to redo a card / deck as many times as you want."); + System.out.println("You can skip a question, look at the answer or go back to the previous question."); + System.out.println("You always have a re-order of your cards / deck"); + System.out.println("That's it. For more details: Read the manual"); + System.out.println(); + System.out.println("Have fun!!"); + } + + private static void startGame() { + System.out.println("Please enter the path of the file you would like to read: "); + String filePath = jsonPath.nextLine(); + + Wrapper wrapper = loadDeck(filePath); + if (wrapper == null || wrapper.cards == null || wrapper.cards.isEmpty()) { + System.out.println("Error: No cards found or invalid file."); + return; + } + + playDeck(wrapper); + } + + private static Wrapper loadDeck(String filePath) { + try (Reader fileReader = new FileReader(filePath)) { + return gson.fromJson(fileReader, Wrapper.class); + } catch (IOException e) { + System.out.println("Could not read file: " + e.getMessage()); + return null; + } + } + + private static void playDeck(Wrapper wrapper) { + int cardNumber = 0; + + while (true) { + if (cardNumber < 0) cardNumber = 0; + if (cardNumber >= wrapper.cards.size()) { + if (!handleDeckFinished(wrapper)) { + break; + } else { + cardNumber = 0; + continue; + } + } + + CardsClass card = wrapper.cards.get(cardNumber); + System.out.println("What's: " + card.question + "?"); + System.out.println("Available options: back, answer, skip"); + + String userInput = userAnswers.nextLine(); + + if (handleSessionInput(userInput, card, wrapper, cardNumber)) { + cardNumber++; + } else if (userInput.equalsIgnoreCase("back")) { + cardNumber--; + } else if (userInput.equalsIgnoreCase("skip")) { + cardNumber++; + } + } + } + + private static boolean handleSessionInput(String userInput, CardsClass card, Wrapper wrapper, int cardNumber) { + if (userInput.equalsIgnoreCase("answer")) { + System.out.println(card.answer); + return false; + } + + if (userInput.equalsIgnoreCase(card.answer)) { + return true; // correct answer + } + + if (!userInput.equalsIgnoreCase("back") && !userInput.equalsIgnoreCase("skip")) { + System.out.println("Wrong, try again..."); + } + + return false; + } + + private static boolean handleDeckFinished(Wrapper wrapper) { + System.out.println("You've answered all cards"); + System.out.println("Redo Deck? (y/n)"); + String finishedDeck = userAnswers.nextLine(); + + if (finishedDeck.equalsIgnoreCase("y")) { + System.out.println("Restarting Deck..."); + return true; + } else if (finishedDeck.equalsIgnoreCase("n")) { + System.out.println("Another Deck? (y/n)"); + String anotherDeck = userAnswers.nextLine(); + + if (anotherDeck.equalsIgnoreCase("y")) { + return false; // break deck loop, go back to main menu + } else if (anotherDeck.equalsIgnoreCase("n")) { + exitGame(); + } + } + return false; + } + + private static void exitGame() { + System.out.println("Exiting Cards:Gate..."); + System.exit(0); + } } \ No newline at end of file