Skip to content
Snippets Groups Projects
Commit 88e28ba2 authored by Nicklas Persia Tufteland's avatar Nicklas Persia Tufteland
Browse files

Merge branch '12-implement-test-cli-application' into 'dev'

Feat: Implement CLI-application. This includes methods for user to read,...

Closes #12

See merge request !16
parents a92d1383 245a24bb
No related branches found
No related tags found
3 merge requests!41final delivery,!28MVP,!16Feat: Implement CLI-application. This includes methods for user to read,...
Pipeline #274277 passed
package edu.ntnu.idatt2003.Controller;
public class UserController {
public void readDescriptionFromFile() {
}
}
...@@ -6,6 +6,7 @@ import edu.ntnu.idatt2003.model.ChaosGameDescription; ...@@ -6,6 +6,7 @@ import edu.ntnu.idatt2003.model.ChaosGameDescription;
import edu.ntnu.idatt2003.model.Matrix2x2; import edu.ntnu.idatt2003.model.Matrix2x2;
import edu.ntnu.idatt2003.model.Transform2D; import edu.ntnu.idatt2003.model.Transform2D;
import edu.ntnu.idatt2003.model.Vector2d; import edu.ntnu.idatt2003.model.Vector2d;
import edu.ntnu.idatt2003.view.UI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -27,6 +28,9 @@ public class Main { ...@@ -27,6 +28,9 @@ public class Main {
ChaosGame game = new ChaosGame(description, 100, 100); ChaosGame game = new ChaosGame(description, 100, 100);
game.runSteps(10000); game.runSteps(10000);
game.getCanvas().showCanvas(); game.getCanvas().showCanvas();
UI ui = new UI();
ui.start();
} }
} }
package edu.ntnu.idatt2003.view;
/**
* Represents a text renderer.
* Contains constants for menu options and methods for showing the menu, entering a path and entering steps.
* Goal: act as a view for a text renderer.
*
*/
public class TextRenderer {
public static final String READ_FILE = "1";
public static final String WRITE_FILE = "2";
public static final String RUN_ITERATIONS = "3";
public static final String SHOW_CANVAS = "4";
public static final String EXIT = "5";
public void showMenu() {
System.out.println("Menu:");
System.out.println("1. Read description from file");
System.out.println("2. Write description to file");
System.out.println("3. Run ChaosGame a given number of steps");
System.out.println("4. Show Canvas");
System.out.println("5. Exit");
}
public void enterPath() {
System.out.println("Enter path to file:");
}
public void enterSteps() {
System.out.println("Enter number of steps:");
}
}
package edu.ntnu.idatt2003.view;
import static edu.ntnu.idatt2003.view.TextRenderer.READ_FILE;
import static edu.ntnu.idatt2003.view.TextRenderer.EXIT;
import static edu.ntnu.idatt2003.view.TextRenderer.RUN_ITERATIONS;
import static edu.ntnu.idatt2003.view.TextRenderer.SHOW_CANVAS;
import static edu.ntnu.idatt2003.view.TextRenderer.WRITE_FILE;
import edu.ntnu.idatt2003.model.ChaosGame;
import edu.ntnu.idatt2003.model.ChaosGameDescription;
import edu.ntnu.idatt2003.model.ChaosGameFileHandler;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Represents a user interface.
* Contains methods for starting the UI, reading a description from a file, writing a description to a file,
* running iterations and showing the canvas.
* Includes a scanner for user input, and instances of TextRenderer, ChaosGameFileHandler, ChaosGame and ChaosGameDescription.
* Goal: act as a view for a user interface.
*/
public class UI {
TextRenderer textRenderer = new TextRenderer();
ChaosGameFileHandler chaosGameFileHandler = new ChaosGameFileHandler();
ChaosGameDescription description;
ChaosGame chaosGame;
private final Scanner scanner = new Scanner(System.in);
/**
* Starts the user interface. Asks the user for input and performs the corresponding action.
* Breaks the loop when the user chooses to exit, which is when the user inputs "5".
* Invalid input will result in a message.
*/
public void start() {
String choice;
do {
textRenderer.showMenu();
choice = getUserChoice();
switch (choice) {
case READ_FILE:
readDescriptionFromFile();
break;
case WRITE_FILE:
writeDescriptionToFile();
break;
case RUN_ITERATIONS:
runIterations();
break;
case SHOW_CANVAS:
ShowCanvas();
break;
default:
System.out.println("Invalid choice");
break;
}
} while (!choice.equals(EXIT));
}
/**
* Gets the user's choice.
*
* @return The user's choice.
*/
public static String getUserChoice() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
/**
* Gets the user's input, and checks if it is empty.
*
* @return The user's input.
*/
public String textInput() {
String text;
do {
text = scanner.nextLine();
if (text.isEmpty()) {
System.out.println("Input cannot be empty. Please try again.");
}
} while (text.isEmpty());
return text;
}
/**
* Gets the user's input, and checks if it is a number and whether it is empty.
*
* @return The user's input.
*/
public int numberInput() {
int number;
while (true) {
String input = scanner.nextLine();
if (!input.isEmpty()) {
try {
number = Integer.parseInt(input);
break;
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
} else {
System.out.println("Input cannot be empty. Please try again.");
}
}
return number;
}
/**
* Reads a description from a file.
* Asks the user for the path to the file, and reads the already existing description from the file.
* Sets the description and chaosGame to the new description.
* If the file is not found, a message will be shown.
*/
public void readDescriptionFromFile(){
textRenderer.enterPath();
String pathToFile = textInput();
ChaosGameDescription newDescription;
try {
newDescription = chaosGameFileHandler.readFromFile(pathToFile);
System.out.println("file read successfully");
} catch (FileNotFoundException e) {
throw new RuntimeException("File '" + pathToFile + "' not found." + e.getMessage());
}
this.description = newDescription;
chaosGame = new ChaosGame(description, 30, 30);
System.out.println("description set successfully");
}
/**
* Writes a description to a file.
* Asks the user for the path to the file, and writes the description to the file.
* If the file is not found, a message will be shown.
*/
public void writeDescriptionToFile() {
textRenderer.enterPath();
String pathToFile = textInput();
chaosGameFileHandler.writeToFile(description, pathToFile);
}
/**
* Runs iterations.
* Asks the user for the number of steps to run the simulation.
* Runs the simulation for the specified number of steps.
*/
public void runIterations() {
if(chaosGame == null || description == null) {
System.out.println("description or chaosGame is null");
return;
}
textRenderer.enterSteps();
int steps = numberInput();
chaosGame.runSteps(steps);
}
/**
* Shows the canvas, and ensures that chaosGame is not null.
*/
public void ShowCanvas() {
if(chaosGame == null) {
System.out.println("chaosGame is null");
return;
}
chaosGame.getCanvas().showCanvas();
}
}
...@@ -3,4 +3,4 @@ Affine2D # Type of transform ...@@ -3,4 +3,4 @@ Affine2D # Type of transform
1, 1 # Upper right 1, 1 # Upper right
.5, 0, 0, .5, 0, 0 # 1st transform (a00, a01, a10, a11, b0, b1) .5, 0, 0, .5, 0, 0 # 1st transform (a00, a01, a10, a11, b0, b1)
.5, 0, 0, .5, .25, .5 # 2nd transform .5, 0, 0, .5, .25, .5 # 2nd transform
.5, 0, 0, .5, .5, 0 .5, 0, 0, .5, .5, 0 # 3rd transform
\ No newline at end of file \ No newline at end of file
Affine2D
0.0, 0.0
1.0, 1.0
0.500000, 0.000000, 0.000000, 0.500000, 0.0, 0.0
0.500000, 0.000000, 0.000000, 0.500000, 0.25, 0.5
0.500000, 0.000000, 0.000000, 0.500000, 0.5, 0.0
Julia # Type of transform Julia
-1.6, -1 # Lower left -1.6, -1.0
1.6, 1 # Upper right 1.6, 1.0
-.74543, .11301 # Real and imaginary parts of the constant c -0.74543, 0.11301
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment