From 22c6d84d5903a7d1d6a60cb5c1aa992cb3865493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Daleng?= <142524365+MrMarHVD@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:26:29 +0200 Subject: [PATCH] Improved CLI-app to add super basic user input handling. --- .../java/edu/ntnu/stud/chaosgame/Main.java | 37 +++++++++++++++++-- src/main/resources/descriptions/output.txt | 6 +++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/descriptions/output.txt diff --git a/src/main/java/edu/ntnu/stud/chaosgame/Main.java b/src/main/java/edu/ntnu/stud/chaosgame/Main.java index 2d0d607..73020f6 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/Main.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/Main.java @@ -8,17 +8,21 @@ import edu.ntnu.stud.chaosgame.model.handling.ChaosCanvas; import edu.ntnu.stud.chaosgame.model.handling.ChaosGame; import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.Scanner; /** * Main class for the Chaos Game application. */ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); System.out.println("Hi"); - String path = "src/main/resources/descriptions/Sierpinski.txt"; + String path = "src/main/resources/descriptions/sierpinski.txt"; ChaosGameDescription description = null; // Declare description @@ -34,7 +38,34 @@ public class Main { } catch (Exception e) { System.out.println(e.getMessage()); // TODO: Alter error handling } - game.runSteps(100000); + + boolean exit = false; + + // While loop for simple cli-app. + while (!exit) { + + System.out.println("What would you like to do? Type 1 for displaying fractal, " + + "or anything else for ending the program"); + if (Objects.equals(sc.next(), "1")) { + game.runSteps(100000); + System.out.println( + "What would you like to do? Type 1 for saving the fractal, anything else for ending the program"); + + if (Objects.equals(sc.next(), "1")) { + ChaosGameFileHandler handler = new ChaosGameFileHandler(); + handler.writeToFile(description, "src/main/resources/descriptions/output.txt"); + System.out.println("Wrote to file output.txt."); + } + else { + exit = true; + } + } + else { + exit = true; + } + } + System.out.println("Terminated program."); + diff --git a/src/main/resources/descriptions/output.txt b/src/main/resources/descriptions/output.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/output.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 -- GitLab