From f5885b8f673ebf9faa48f165ca298eb1c5cae619 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 10:56:18 +0200 Subject: [PATCH 1/7] Created new exception types --- .../exceptions/ChaosGameDescriptionException.java | 15 +++++++++++++++ .../ChaosGameDescriptionFactoryException.java | 15 +++++++++++++++ .../exceptions/ChaosGameFileHandlerException.java | 15 +++++++++++++++ .../exceptions/WrongFileFormatException.java | 6 +++++- 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionException.java create mode 100644 src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionFactoryException.java create mode 100644 src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameFileHandlerException.java diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionException.java new file mode 100644 index 0000000..4e8da78 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionException.java @@ -0,0 +1,15 @@ +package edu.ntnu.idatt2003.exceptions; + +public class ChaosGameDescriptionException extends Exception { + public ChaosGameDescriptionException() { + super("An error occurred in the ChaosGameDescription class."); + } + + public ChaosGameDescriptionException(String message) { + super(message); + } + + public ChaosGameDescriptionException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionFactoryException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionFactoryException.java new file mode 100644 index 0000000..24fc0f2 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameDescriptionFactoryException.java @@ -0,0 +1,15 @@ +package edu.ntnu.idatt2003.exceptions; + +public class ChaosGameDescriptionFactoryException extends Exception { + public ChaosGameDescriptionFactoryException() { + super("An error occurred while creating a ChaosGameDescription object."); + } + + public ChaosGameDescriptionFactoryException(String message, Throwable cause) { + super(message, cause); + } + + public ChaosGameDescriptionFactoryException(String message) { + super(message); + } +} diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameFileHandlerException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameFileHandlerException.java new file mode 100644 index 0000000..57c6b00 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameFileHandlerException.java @@ -0,0 +1,15 @@ +package edu.ntnu.idatt2003.exceptions; + +public class ChaosGameFileHandlerException extends Exception { + public ChaosGameFileHandlerException() { + super("An error occurred while handling the file"); + } + + public ChaosGameFileHandlerException(String message, Throwable cause) { + super(message, cause); + } + + public ChaosGameFileHandlerException(String message) { + super(message); + } +} diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/WrongFileFormatException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/WrongFileFormatException.java index aebd905..66eca37 100644 --- a/src/main/java/edu/ntnu/idatt2003/exceptions/WrongFileFormatException.java +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/WrongFileFormatException.java @@ -1,10 +1,14 @@ package edu.ntnu.idatt2003.exceptions; -public class WrongFileFormatException extends Exception { +public class WrongFileFormatException extends ChaosGameFileHandlerException { public WrongFileFormatException() { super("The file has the wrong format"); } + public WrongFileFormatException(String message, Throwable cause) { + super(message, cause); + } + public WrongFileFormatException(String message) { super(message); } -- GitLab From 2d94b3d4032c946ac3354cf286e74b69fb458690 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 10:57:04 +0200 Subject: [PATCH 2/7] Added exceptions to ChaosGameDescription --- .../model/game/ChaosGameDescription.java | 26 +++++++----- .../model/io/ChaosGameFileHandler.java | 40 +++++++++++-------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescription.java index d55143b..1203185 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescription.java +++ b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescription.java @@ -1,5 +1,6 @@ package edu.ntnu.idatt2003.model.game; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; import edu.ntnu.idatt2003.model.math.transformation.Transform2D; import edu.ntnu.idatt2003.util.InputValidation; @@ -20,19 +21,23 @@ public class ChaosGameDescription { * @param minCoords the minimum coordinates. * @param maxCoords the maximum coordinates. * - * @throws IllegalArgumentException if the list of transforms is null, + * @throws ChaosGameDescriptionException if the list of transforms is null, * if the list of transforms is empty, * if minCoords is null, * if maxCoords is null, * if minCoords is greater than or equal to maxCoords */ public ChaosGameDescription(List<Transform2D> transforms, Vector2D minCoords, Vector2D maxCoords) - throws IllegalArgumentException { - InputValidation.validateNotNull(transforms, "transforms"); - InputValidation.validateListNotEmpty(transforms, "transforms"); - InputValidation.validateNotNull(minCoords, "minCoords"); - InputValidation.validateNotNull(maxCoords, "maxCoords"); - validateCoordinates(minCoords, maxCoords); + throws ChaosGameDescriptionException { + try { + InputValidation.validateNotNull(transforms, "transforms"); + InputValidation.validateListNotEmpty(transforms, "transforms"); + InputValidation.validateNotNull(minCoords, "minCoords"); + InputValidation.validateNotNull(maxCoords, "maxCoords"); + validateCoordinates(minCoords, maxCoords); + } catch (IllegalArgumentException e) { + throw new ChaosGameDescriptionException("Invalid ChaosGameDescription", e); + } this.transforms = transforms; this.minCoords = minCoords; this.maxCoords = maxCoords; @@ -71,11 +76,12 @@ public class ChaosGameDescription { * @param minCoords the minimum coordinates. * @param maxCoords the maximum coordinates. * - * @throws IllegalArgumentException if minCoords is greater than or equal to maxCoords + * @throws ChaosGameDescriptionException if minCoords is greater than or equal to maxCoords */ - private void validateCoordinates(Vector2D minCoords, Vector2D maxCoords) { + private void validateCoordinates(Vector2D minCoords, Vector2D maxCoords) + throws ChaosGameDescriptionException { if (minCoords.getX0() >= maxCoords.getX0() || minCoords.getX1() >= maxCoords.getX1()) { - throw new IllegalArgumentException("minCoords must be less than maxCoords"); + throw new ChaosGameDescriptionException("minCoords must be less than maxCoords"); } } } diff --git a/src/main/java/edu/ntnu/idatt2003/model/io/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/idatt2003/model/io/ChaosGameFileHandler.java index b789063..7549e22 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/io/ChaosGameFileHandler.java +++ b/src/main/java/edu/ntnu/idatt2003/model/io/ChaosGameFileHandler.java @@ -1,5 +1,7 @@ package edu.ntnu.idatt2003.model.io; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; +import edu.ntnu.idatt2003.exceptions.ChaosGameFileHandlerException; import edu.ntnu.idatt2003.exceptions.WrongFileFormatException; import edu.ntnu.idatt2003.model.game.ChaosGameDescription; import edu.ntnu.idatt2003.model.math.mathModel.Complex; @@ -34,7 +36,7 @@ public class ChaosGameFileHandler { * @return the ChaosGameDescription read from the file. */ public ChaosGameDescription readFromFile(File file) - throws FileNotFoundException, WrongFileFormatException { + throws ChaosGameFileHandlerException, ChaosGameDescriptionException { List<List<String>> lines = divideFileToLines(file); if (lines.size() <= 3) { throw new WrongFileFormatException(); @@ -43,7 +45,7 @@ public class ChaosGameFileHandler { try { InputValidation.validateNotNull(type, "type"); } catch (IllegalArgumentException e) { - throw new WrongFileFormatException("The file should have one transformation type"); + throw new WrongFileFormatException("The file should have one transformation type", e); } if (type.size() != 1) { throw new WrongFileFormatException("The file should have one transformation type"); @@ -58,7 +60,7 @@ public class ChaosGameFileHandler { InputValidation.validateNotNull(minCoords, "minCoords"); InputValidation.validateNotNull(maxCoords, "maxCoords"); } catch (IllegalArgumentException e) { - throw new WrongFileFormatException("The file should have two min/max coordinates each"); + throw new WrongFileFormatException("The file should have two min/max coordinates each", e); } if (minCoords.size() != 2 || maxCoords.size() != 2) { throw new WrongFileFormatException("The file should have two min/max coordinates each"); @@ -74,9 +76,9 @@ public class ChaosGameFileHandler { maxX = Double.parseDouble(maxCoords.get(0)); maxY = Double.parseDouble(maxCoords.get(1)); } catch (NumberFormatException e) { - throw new WrongFileFormatException("The min/max coordinates in the file are not numbers"); + throw new WrongFileFormatException("The min/max coordinates in the file are not numbers", e); } catch (IndexOutOfBoundsException e) { - throw new WrongFileFormatException("The file is missing min/max coordinates"); + throw new WrongFileFormatException("The file is missing min/max coordinates", e); } if (minX >= maxX || minY >= maxY) { @@ -87,11 +89,16 @@ public class ChaosGameFileHandler { List<Transform2D> transforms = (type.getFirst().equals("Affine2D")) ? packageToAffineList(lines) : packageToJuliaList(lines); - - return new ChaosGameDescription(transforms, new Vector2D(minX, minY), new Vector2D(maxX, maxY)); + try { + return new ChaosGameDescription(transforms, new Vector2D(minX, minY), + new Vector2D(maxX, maxY)); + } catch (ChaosGameDescriptionException e) { + throw new ChaosGameDescriptionException("Could not create ChaosGameDescription", e); + } } - private List<List<String>> divideFileToLines(File file) throws FileNotFoundException { + private List<List<String>> divideFileToLines(File file) + throws ChaosGameFileHandlerException { List<List<String>> lines = new ArrayList<>(); try (Scanner scanner = new Scanner(file)) { scanner.useDelimiter("#.*|\n"); @@ -110,7 +117,7 @@ public class ChaosGameFileHandler { i++; } } catch (FileNotFoundException e) { - throw new FileNotFoundException("File not found"); + throw new ChaosGameFileHandlerException("File not found", e); } return lines; } @@ -127,7 +134,7 @@ public class ChaosGameFileHandler { Double.parseDouble(value); } catch (NumberFormatException e) { throw new WrongFileFormatException( - "The transformations in the file should only contain numbers"); + "The transformations in the file should only contain numbers", e); } } } @@ -160,7 +167,7 @@ public class ChaosGameFileHandler { try { InputValidation.validateNotNull(julia, "Julia transformation"); } catch (IllegalArgumentException e) { - throw new WrongFileFormatException("The file should have one Julia transformation"); + throw new WrongFileFormatException("The file should have one Julia transformation", e); } if (julia.size() != 2) { @@ -173,7 +180,7 @@ public class ChaosGameFileHandler { real = Double.parseDouble(julia.get(0)); imaginary = Double.parseDouble(julia.get(1)); } catch (NumberFormatException e) { - throw new WrongFileFormatException("The Julia transformation should only contain numbers"); + throw new WrongFileFormatException("The Julia transformation should only contain numbers", e); } JuliaTransform juliaTransformPositive = new JuliaTransform(new Complex(real, imaginary), 1); @@ -190,7 +197,8 @@ public class ChaosGameFileHandler { * @param chaosGameDescription the ChaosGameDescription to write. * @param path the path to the file. */ - public void writeToFile(ChaosGameDescription chaosGameDescription, File path) throws IOException { + public void writeToFile(ChaosGameDescription chaosGameDescription, File path) + throws ChaosGameFileHandlerException { try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) { String type = chaosGameDescription.getTransforms().getFirst() instanceof AffineTransform2D ? "Affine2D" : "Julia"; @@ -214,7 +222,7 @@ public class ChaosGameFileHandler { writeJuliaToFile(writer, juliaTransform); } } catch (IOException e) { - throw new IOException("Could not write to file"); + throw new ChaosGameFileHandlerException("Could not write to file", e); } } @@ -247,7 +255,7 @@ public class ChaosGameFileHandler { * * @return a list of all files in the resources directory. */ - public List<String> listFiles() { + public List<String> listFiles() throws ChaosGameFileHandlerException { List<String> fileList = new ArrayList<>(); try { Path resourceDirectory = Paths.get("src/main/resources"); @@ -257,7 +265,7 @@ public class ChaosGameFileHandler { .forEach(path -> fileList.add(path.toString())); } } catch (IOException e) { - e.printStackTrace(); + throw new ChaosGameFileHandlerException("Could not list files", e); } return fileList; } -- GitLab From 0d7143c681bf65f623cc095f4065605a49a2fa30 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 10:57:33 +0200 Subject: [PATCH 3/7] Added exceptions to ChaosGameDescriptionFactory --- .../game/ChaosGameDescriptionFactory.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionFactory.java b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionFactory.java index f333d89..c9849e7 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionFactory.java +++ b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionFactory.java @@ -1,5 +1,7 @@ package edu.ntnu.idatt2003.model.game; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionFactoryException; import edu.ntnu.idatt2003.model.math.mathModel.Complex; import edu.ntnu.idatt2003.model.math.mathModel.Matrix2x2; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; @@ -20,12 +22,12 @@ public class ChaosGameDescriptionFactory { * @param type the type of chaos game to create. * @return a ChaosGameDescription object. */ - public static ChaosGameDescription get(String type) { + public static ChaosGameDescription get(String type) throws ChaosGameDescriptionFactoryException { return switch (type) { case "Sierpinski" -> createSierpinskiTriangle(); case "Barnsley" -> createBarnsleyFern(); case "Julia Set" -> createJuliaSet(); - default -> throw new IllegalArgumentException("Invalid chaos game type"); + default -> throw new ChaosGameDescriptionFactoryException("Invalid chaos game type"); }; } @@ -34,14 +36,19 @@ public class ChaosGameDescriptionFactory { * * @return a list of the available chaos game types. */ - private static ChaosGameDescription createJuliaSet() { + private static ChaosGameDescription createJuliaSet() throws ChaosGameDescriptionFactoryException { List<Transform2D> transforms = List.of( new JuliaTransform(new Complex(0, 0.8), 1), new JuliaTransform(new Complex(0, 0.8), -1) ); Vector2D minCoords = new Vector2D(-1.4, -1.4); Vector2D maxCoords = new Vector2D(1.4, 1.4); - return new ChaosGameDescription(transforms, minCoords, maxCoords); + try { + return new ChaosGameDescription(transforms, minCoords, maxCoords); + } catch (ChaosGameDescriptionException e) { + throw new ChaosGameDescriptionFactoryException( + "An error occurred while creating a ChaosGameDescription object in factory.", e); + } } /** @@ -49,7 +56,8 @@ public class ChaosGameDescriptionFactory { * * @return a list of the available chaos game types. */ - private static ChaosGameDescription createBarnsleyFern() { + private static ChaosGameDescription createBarnsleyFern() + throws ChaosGameDescriptionFactoryException { List<Transform2D> transforms = List.of( new AffineTransform2D(new Matrix2x2(0, 0, 0, 0.16), new Vector2D(0, 0)), new AffineTransform2D(new Matrix2x2(0.85, 0.04, -0.04, 0.85), new Vector2D(0, 1.6)), @@ -58,7 +66,12 @@ public class ChaosGameDescriptionFactory { ); Vector2D minCoords = new Vector2D(-2.65, 0); Vector2D maxCoords = new Vector2D(2.65, 10); - return new ChaosGameDescription(transforms, minCoords, maxCoords); + try { + return new ChaosGameDescription(transforms, minCoords, maxCoords); + } catch (ChaosGameDescriptionException e) { + throw new ChaosGameDescriptionFactoryException( + "An error occurred while creating a ChaosGameDescription object in factory.", e); + } } /** @@ -66,7 +79,8 @@ public class ChaosGameDescriptionFactory { * * @return a list of the available chaos game types. */ - private static ChaosGameDescription createSierpinskiTriangle() { + private static ChaosGameDescription createSierpinskiTriangle() + throws ChaosGameDescriptionFactoryException { List<Transform2D> transforms = List.of( new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(0, 0)), new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(0.5, 0)), @@ -74,6 +88,11 @@ public class ChaosGameDescriptionFactory { ); Vector2D minCoords = new Vector2D(0, 0); Vector2D maxCoords = new Vector2D(1, 1); - return new ChaosGameDescription(transforms, minCoords, maxCoords); + try { + return new ChaosGameDescription(transforms, minCoords, maxCoords); + } catch (ChaosGameDescriptionException e) { + throw new ChaosGameDescriptionFactoryException( + "An error occurred while creating a ChaosGameDescription object in factory.", e); + } } } -- GitLab From 2cb4ea8e9bd82649024e54fa316451876f0ced55 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 10:57:58 +0200 Subject: [PATCH 4/7] Fixes for new exceptions --- .../controller/ChaosGameController.java | 7 ++++-- .../model/math/mathModel/Matrix2x2.java | 8 +++---- .../idatt2003/view/CommandLineInterface.java | 14 +++++++---- .../idatt2003/view/components/TopBar.java | 19 ++++++++++++--- .../model/game/ChaosGameDescriptionTest.java | 23 ++++++++++--------- 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java b/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java index a5d8949..c771a17 100644 --- a/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java +++ b/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java @@ -1,5 +1,6 @@ package edu.ntnu.idatt2003.controller; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionFactoryException; import edu.ntnu.idatt2003.model.game.ChaosGame; import edu.ntnu.idatt2003.model.game.ChaosGameDescription; import edu.ntnu.idatt2003.model.game.ChaosGameDescriptionFactory; @@ -24,13 +25,15 @@ public class ChaosGameController implements Observer { * @param width the width of the canvas. * @param height the height of the canvas. */ - public ChaosGameController(ViewCanvas viewCanvas, int width, int height) { + public ChaosGameController(ViewCanvas viewCanvas, int width, int height) + throws ChaosGameDescriptionFactoryException { this.viewCanvas = viewCanvas; chaosGame = new ChaosGame(ChaosGameDescriptionFactory.get("Julia Set"), width, height); chaosGame.attach(this); } - public void resetChaosGameWithDescription(String description) { + public void resetChaosGameWithDescription(String description) + throws ChaosGameDescriptionFactoryException { ChaosGameDescription newDescription = ChaosGameDescriptionFactory.get(description); chaosGame.resetGameWithDescription(newDescription); } diff --git a/src/main/java/edu/ntnu/idatt2003/model/math/mathModel/Matrix2x2.java b/src/main/java/edu/ntnu/idatt2003/model/math/mathModel/Matrix2x2.java index 9709856..405511c 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/math/mathModel/Matrix2x2.java +++ b/src/main/java/edu/ntnu/idatt2003/model/math/mathModel/Matrix2x2.java @@ -4,10 +4,10 @@ package edu.ntnu.idatt2003.model.math.mathModel; * A class representing a 2x2 matrix. */ public class Matrix2x2 { - private double a00; - private double a01; - private double a10; - private double a11; + private final double a00; + private final double a01; + private final double a10; + private final double a11; /** * Constructor for the Matrix2x2 class. diff --git a/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java b/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java index 2b1b3aa..be5a3cb 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java +++ b/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java @@ -1,5 +1,7 @@ package edu.ntnu.idatt2003.view; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; +import edu.ntnu.idatt2003.exceptions.ChaosGameFileHandlerException; import edu.ntnu.idatt2003.exceptions.WrongFileFormatException; import edu.ntnu.idatt2003.model.game.ChaosGame; import edu.ntnu.idatt2003.model.game.ChaosGameDescription; @@ -30,7 +32,7 @@ public class CommandLineInterface { /** * Starts the command line interface. */ - public void start() { + public void start() throws ChaosGameFileHandlerException { boolean exit = false; while (!exit) { System.out.println("Welcome to the Chaos Game!"); @@ -55,7 +57,7 @@ public class CommandLineInterface { /** * Chooses a file to construct a ChaosGameDescription from. */ - private void chooseFile() { + private void chooseFile() throws ChaosGameFileHandlerException { System.out.println("Choose a file:"); int i = 1; for (String path : chaosGameFileHandler.listFiles()) { @@ -74,10 +76,12 @@ public class CommandLineInterface { File file = new File(path); description = chaosGameFileHandler.readFromFile(file); System.out.println("File read successfully!"); - } catch (FileNotFoundException e) { - System.out.println("File not found. Please try again."); } catch (WrongFileFormatException e) { System.out.println("Wrong file format. Please try again."); + } catch (ChaosGameFileHandlerException e) { + System.out.println("File not found. Please try again."); + } catch (ChaosGameDescriptionException e) { + System.out.println("Failed to create ChaosGameDescription. Please try again."); } break; } @@ -101,7 +105,7 @@ public class CommandLineInterface { File file = new File(path); chaosGameFileHandler.writeToFile(description, file); System.out.println("File written successfully!"); - } catch (IOException e) { + } catch (ChaosGameFileHandlerException e) { System.out.println("An error occurred. Please try again."); } } diff --git a/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java b/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java index 3cb4513..e398383 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java +++ b/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java @@ -1,6 +1,7 @@ package edu.ntnu.idatt2003.view.components; import edu.ntnu.idatt2003.controller.ChaosGameController; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionFactoryException; import javafx.application.Platform; import javafx.scene.control.Button; import javafx.scene.control.TextField; @@ -18,19 +19,31 @@ public class TopBar extends StackPane { Button juliaButton = new SecondaryButton("Julia Set"); juliaButton.setOnAction(e -> { this.controller.resetViewCanvas(); - this.controller.resetChaosGameWithDescription("Julia Set"); + try { + this.controller.resetChaosGameWithDescription("Julia Set"); + } catch (ChaosGameDescriptionFactoryException ex) { + ex.printStackTrace(); + } }); Button sierpinskiButton = new SecondaryButton("Sierpinski"); sierpinskiButton.setOnAction(e -> { this.controller.resetViewCanvas(); - this.controller.resetChaosGameWithDescription("Sierpinski"); + try { + this.controller.resetChaosGameWithDescription("Sierpinski"); + } catch (ChaosGameDescriptionFactoryException ex) { + ex.printStackTrace(); + } }); Button barnsleyButton = new SecondaryButton("Barnsley"); barnsleyButton.setOnAction(e -> { this.controller.resetViewCanvas(); - this.controller.resetChaosGameWithDescription("Barnsley"); + try { + this.controller.resetChaosGameWithDescription("Barnsley"); + } catch (ChaosGameDescriptionFactoryException ex) { + ex.printStackTrace(); + } }); Button readFileButton = new SecondaryButton("Read File"); diff --git a/src/test/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionTest.java b/src/test/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionTest.java index c283e82..b764dd3 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/game/ChaosGameDescriptionTest.java @@ -1,5 +1,6 @@ package edu.ntnu.idatt2003.model.game; +import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; import edu.ntnu.idatt2003.model.math.mathModel.Complex; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; import edu.ntnu.idatt2003.model.math.transformation.JuliaTransform; @@ -26,7 +27,7 @@ class ChaosGameDescriptionTest { Vector2D maxCoords; @BeforeEach - public void setUp() { + public void setUp() throws ChaosGameDescriptionException { transforms = List.of( new JuliaTransform(new Complex(-0.74543, 0.11301), 1), new JuliaTransform(new Complex(-0.74543, 0.11301), -1) @@ -59,37 +60,37 @@ class ChaosGameDescriptionTest { @DisplayName("Negative tests") class MethodsThrowsExceptions { @Test - @DisplayName("Test constructor throws IllegalArgumentException if transforms is null") + @DisplayName("Test constructor throws ChaosGameDescriptionException if transforms is null") public void testConstructorTransformsIsNull() { - assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription( + assertThrows(ChaosGameDescriptionException.class, () -> new ChaosGameDescription( null, new Vector2D(0, 0), new Vector2D(1, 1))); } @Test - @DisplayName("Test constructor throws IllegalArgumentException if transforms is empty") + @DisplayName("Test constructor throws ChaosGameDescriptionException if transforms is empty") public void testConstructorTransformsIsEmpty() { - assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription( + assertThrows(ChaosGameDescriptionException.class, () -> new ChaosGameDescription( List.of(), new Vector2D(0, 0), new Vector2D(1, 1))); } @Test - @DisplayName("Test constructor throws IllegalArgumentException if minCoords is null") + @DisplayName("Test constructor throws ChaosGameDescriptionException if minCoords is null") public void testConstructorIfMinCoordsIsNull() { - assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription( + assertThrows(ChaosGameDescriptionException.class, () -> new ChaosGameDescription( List.of(new JuliaTransform(new Complex(0, 0), 1)), null, new Vector2D(1, 1))); } @Test - @DisplayName("Test constructor throws IllegalArgumentException if maxCoords is null") + @DisplayName("Test constructor throws ChaosGameDescriptionException if maxCoords is null") public void testConstructorMaxCoordsIsNull() { - assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription( + assertThrows(ChaosGameDescriptionException.class, () -> new ChaosGameDescription( List.of(new JuliaTransform(new Complex(0, 0), 1)), new Vector2D(0, 0), null)); } @Test - @DisplayName("Test constructor throws IllegalArgumentException if minCoords is greater than maxCoords") + @DisplayName("Test constructor throws ChaosGameDescriptionException if minCoords is greater than maxCoords") public void testConstructorMinCoordsIsGreaterThanMaxCoords() { - assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription( + assertThrows(ChaosGameDescriptionException.class, () -> new ChaosGameDescription( List.of(new JuliaTransform(new Complex(0, 0), 1)), new Vector2D(1, 1), new Vector2D(0, 0))); } } -- GitLab From 2de2a3397dc63851159db85066b851563e39bf91 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 11:23:42 +0200 Subject: [PATCH 5/7] Created new exceptions --- .../exceptions/ChaosCanvasException.java | 15 +++++++++++++++ .../idatt2003/exceptions/ChaosGameException.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/main/java/edu/ntnu/idatt2003/exceptions/ChaosCanvasException.java create mode 100644 src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameException.java diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosCanvasException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosCanvasException.java new file mode 100644 index 0000000..7d10c75 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosCanvasException.java @@ -0,0 +1,15 @@ +package edu.ntnu.idatt2003.exceptions; + +public class ChaosCanvasException extends Exception { + public ChaosCanvasException() { + super("Chaos canvas exception occurred."); + } + + public ChaosCanvasException(String message, Throwable cause) { + super(message, cause); + } + + public ChaosCanvasException(String message) { + super(message); + } +} diff --git a/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameException.java b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameException.java new file mode 100644 index 0000000..be77333 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/exceptions/ChaosGameException.java @@ -0,0 +1,15 @@ +package edu.ntnu.idatt2003.exceptions; + +public class ChaosGameException extends Exception { + public ChaosGameException() { + super("Chaos game exception occurred."); + } + + public ChaosGameException(String message, Throwable cause) { + super(message, cause); + } + + public ChaosGameException(String message) { + super(message); + } +} -- GitLab From 908fbba7a773f8388fbf8b47b4bde1d53efdf8e5 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 11:24:29 +0200 Subject: [PATCH 6/7] Implemented new exceptions to ChaosCanvas --- .../idatt2003/model/game/ChaosCanvas.java | 27 +++++++++++++------ .../idatt2003/model/game/ChaosCanvasTest.java | 13 ++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosCanvas.java b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosCanvas.java index bab820a..65a63f9 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosCanvas.java @@ -1,5 +1,6 @@ package edu.ntnu.idatt2003.model.game; +import edu.ntnu.idatt2003.exceptions.ChaosCanvasException; import edu.ntnu.idatt2003.model.math.mathModel.Matrix2x2; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; import edu.ntnu.idatt2003.model.math.transformation.AffineTransform2D; @@ -31,10 +32,15 @@ public class ChaosCanvas { * if minCoords is null, * if maxCoords is null */ - public ChaosCanvas(int width, int height, Vector2D minCoords, Vector2D maxCoords) { - InputValidation.validatePositiveInt(width, "width"); - InputValidation.validatePositiveInt(height, "height"); - setMinMaxCoords(minCoords, maxCoords); + public ChaosCanvas(int width, int height, Vector2D minCoords, Vector2D maxCoords) + throws ChaosCanvasException { + try { + InputValidation.validatePositiveInt(width, "width"); + InputValidation.validatePositiveInt(height, "height"); + setMinMaxCoords(minCoords, maxCoords); + } catch (IllegalArgumentException e) { + throw new ChaosCanvasException("An error occurred while creating the ChaosCanvas class", e); + } this.width = width; this.height = height; this.canvas = new int[width][height]; @@ -87,10 +93,15 @@ public class ChaosCanvas { * @throws IllegalArgumentException if minCoords is null, * if maxCoords is null */ - public void setMinMaxCoords(Vector2D minCoords, Vector2D maxCoords) { - setMinCoords(minCoords); - setMaxCoords(maxCoords); - setTransformCoordsToIndices(); + public void setMinMaxCoords(Vector2D minCoords, Vector2D maxCoords) throws ChaosCanvasException { + try { + setMinCoords(minCoords); + setMaxCoords(maxCoords); + setTransformCoordsToIndices(); + } catch (IllegalArgumentException e) { + throw new ChaosCanvasException( + "An error occurred while setting the min and max coordinates", e); + } } /** diff --git a/src/test/java/edu/ntnu/idatt2003/model/game/ChaosCanvasTest.java b/src/test/java/edu/ntnu/idatt2003/model/game/ChaosCanvasTest.java index 0cf30c6..89e6760 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/game/ChaosCanvasTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/game/ChaosCanvasTest.java @@ -2,6 +2,7 @@ package edu.ntnu.idatt2003.model.game; import static org.junit.jupiter.api.Assertions.*; +import edu.ntnu.idatt2003.exceptions.ChaosCanvasException; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -19,7 +20,7 @@ class ChaosCanvasTest { ChaosCanvas canvas; @BeforeEach - public void setUp() { + public void setUp() throws ChaosCanvasException { canvas = new ChaosCanvas(100, 100, new Vector2D(0, 0), new Vector2D(200, 200)); } @@ -103,10 +104,10 @@ class ChaosCanvasTest { @Test @DisplayName("Negative tests for setMinMaxCoords") - public void setMinMaxCoordsThrowsExceptionOnNull() { + public void setMinMaxCoordsThrowsExceptionOnNull() throws ChaosCanvasException { ChaosCanvas canvas = new ChaosCanvas(100, 100, new Vector2D(0, 0), new Vector2D(200, 200)); - assertThrows(IllegalArgumentException.class, () -> canvas.setMinMaxCoords(null, new Vector2D(200, 200))); - assertThrows(IllegalArgumentException.class, () -> canvas.setMinMaxCoords(new Vector2D(0, 0), null)); + assertThrows(ChaosCanvasException.class, () -> canvas.setMinMaxCoords(null, new Vector2D(200, 200))); + assertThrows(ChaosCanvasException.class, () -> canvas.setMinMaxCoords(new Vector2D(0, 0), null)); } @Nested @@ -116,13 +117,13 @@ class ChaosCanvasTest { @Test @DisplayName("Constructor throws exception on negative width") public void constructorThrowsExceptionOnNegativeWidth() { - assertThrows(IllegalArgumentException.class, () -> new ChaosCanvas(-100, 100, new Vector2D(0, 0), new Vector2D(200, 200))); + assertThrows(ChaosCanvasException.class, () -> new ChaosCanvas(-100, 100, new Vector2D(0, 0), new Vector2D(200, 200))); } @Test @DisplayName("Constructor throws exception on negative height") public void constructorThrowsExceptionOnNegativeHeight() { - assertThrows(IllegalArgumentException.class, () -> new ChaosCanvas(100, -100, new Vector2D(0, 0), new Vector2D(200, 200))); + assertThrows(ChaosCanvasException.class, () -> new ChaosCanvas(100, -100, new Vector2D(0, 0), new Vector2D(200, 200))); } } } -- GitLab From 3fd24c26a56ec466b689efc877ba3846f2edf393 Mon Sep 17 00:00:00 2001 From: Vetle <vetletb@stud.ntnu.no> Date: Tue, 14 May 2024 11:24:39 +0200 Subject: [PATCH 7/7] Implemented new exceptions to ChaosGame --- .../controller/ChaosGameController.java | 6 ++-- .../ntnu/idatt2003/model/game/ChaosGame.java | 30 +++++++++++++++---- .../idatt2003/view/CommandLineInterface.java | 7 +++-- .../idatt2003/view/components/TopBar.java | 7 +++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java b/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java index c771a17..7c04f0c 100644 --- a/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java +++ b/src/main/java/edu/ntnu/idatt2003/controller/ChaosGameController.java @@ -1,6 +1,8 @@ package edu.ntnu.idatt2003.controller; +import edu.ntnu.idatt2003.exceptions.ChaosCanvasException; import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionFactoryException; +import edu.ntnu.idatt2003.exceptions.ChaosGameException; import edu.ntnu.idatt2003.model.game.ChaosGame; import edu.ntnu.idatt2003.model.game.ChaosGameDescription; import edu.ntnu.idatt2003.model.game.ChaosGameDescriptionFactory; @@ -26,14 +28,14 @@ public class ChaosGameController implements Observer { * @param height the height of the canvas. */ public ChaosGameController(ViewCanvas viewCanvas, int width, int height) - throws ChaosGameDescriptionFactoryException { + throws ChaosGameDescriptionFactoryException, ChaosGameException, ChaosCanvasException { this.viewCanvas = viewCanvas; chaosGame = new ChaosGame(ChaosGameDescriptionFactory.get("Julia Set"), width, height); chaosGame.attach(this); } public void resetChaosGameWithDescription(String description) - throws ChaosGameDescriptionFactoryException { + throws ChaosGameDescriptionFactoryException, ChaosGameException { ChaosGameDescription newDescription = ChaosGameDescriptionFactory.get(description); chaosGame.resetGameWithDescription(newDescription); } diff --git a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGame.java b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGame.java index d13b804..5b469e6 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGame.java +++ b/src/main/java/edu/ntnu/idatt2003/model/game/ChaosGame.java @@ -1,7 +1,10 @@ package edu.ntnu.idatt2003.model.game; +import edu.ntnu.idatt2003.exceptions.ChaosCanvasException; +import edu.ntnu.idatt2003.exceptions.ChaosGameException; import edu.ntnu.idatt2003.model.math.mathModel.Vector2D; import edu.ntnu.idatt2003.model.math.transformation.Transform2D; +import edu.ntnu.idatt2003.util.InputValidation; import java.util.List; import java.util.Random; @@ -21,9 +24,18 @@ public class ChaosGame extends Subject { * @param width the width of the canvas. * @param height the height of the canvas. */ - public ChaosGame(ChaosGameDescription description, int width, int height) { + public ChaosGame(ChaosGameDescription description, int width, int height) + throws ChaosGameException, ChaosCanvasException { + try { + InputValidation.validateNotNull(description, "description"); + this.canvas = new ChaosCanvas( + width, height, description.getMinCoords(), description.getMaxCoords()); + } catch (IllegalArgumentException e) { + throw new ChaosGameException("The description of the chaos game cannot be null.", e); + } catch (ChaosCanvasException e) { + throw new ChaosGameException("An error occurred while creating the ChaosCanvas class.", e); + } this.description = description; - this.canvas = new ChaosCanvas(width, height, description.getMinCoords(), description.getMaxCoords()); this.currentPoint = new Vector2D(0, 0); this.random = new Random(); } @@ -40,9 +52,17 @@ public class ChaosGame extends Subject { /** * Sets the description of the chaos game. */ - private void setDescription(ChaosGameDescription description) { + private void setDescription(ChaosGameDescription description) throws ChaosGameException { + try { + canvas.setMinMaxCoords(description.getMinCoords(), description.getMaxCoords()); + InputValidation.validateNotNull(description, "description"); + } catch (IllegalArgumentException e) { + throw new ChaosGameException("The description of the chaos game cannot be null.", e); + } catch (ChaosCanvasException e) { + throw new ChaosGameException( + "An error occurred while setting the description of the chaos game.", e); + } this.description = description; - canvas.setMinMaxCoords(description.getMinCoords(), description.getMaxCoords()); } /** @@ -58,7 +78,7 @@ public class ChaosGame extends Subject { * * @param description the description to reset the chaos game with. */ - public void resetGameWithDescription(ChaosGameDescription description) { + public void resetGameWithDescription(ChaosGameDescription description) throws ChaosGameException { setDescription(description); resetGame(); } diff --git a/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java b/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java index be5a3cb..614b565 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java +++ b/src/main/java/edu/ntnu/idatt2003/view/CommandLineInterface.java @@ -1,6 +1,8 @@ package edu.ntnu.idatt2003.view; +import edu.ntnu.idatt2003.exceptions.ChaosCanvasException; import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionException; +import edu.ntnu.idatt2003.exceptions.ChaosGameException; import edu.ntnu.idatt2003.exceptions.ChaosGameFileHandlerException; import edu.ntnu.idatt2003.exceptions.WrongFileFormatException; import edu.ntnu.idatt2003.model.game.ChaosGame; @@ -32,7 +34,8 @@ public class CommandLineInterface { /** * Starts the command line interface. */ - public void start() throws ChaosGameFileHandlerException { + public void start() throws ChaosGameFileHandlerException, ChaosGameException, + ChaosCanvasException { boolean exit = false; while (!exit) { System.out.println("Welcome to the Chaos Game!"); @@ -113,7 +116,7 @@ public class CommandLineInterface { /** * Runs the chaos game and prints the result. */ - private void runChaosGame() { + private void runChaosGame() throws ChaosGameException, ChaosCanvasException { ChaosGame chaosGame = new ChaosGame(description, width, height); System.out.println("Choose number of steps:"); int steps = scanner.nextInt(); diff --git a/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java b/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java index e398383..a322a02 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java +++ b/src/main/java/edu/ntnu/idatt2003/view/components/TopBar.java @@ -2,6 +2,7 @@ package edu.ntnu.idatt2003.view.components; import edu.ntnu.idatt2003.controller.ChaosGameController; import edu.ntnu.idatt2003.exceptions.ChaosGameDescriptionFactoryException; +import edu.ntnu.idatt2003.exceptions.ChaosGameException; import javafx.application.Platform; import javafx.scene.control.Button; import javafx.scene.control.TextField; @@ -23,6 +24,8 @@ public class TopBar extends StackPane { this.controller.resetChaosGameWithDescription("Julia Set"); } catch (ChaosGameDescriptionFactoryException ex) { ex.printStackTrace(); + } catch (ChaosGameException ex) { + ex.printStackTrace(); } }); @@ -33,6 +36,8 @@ public class TopBar extends StackPane { this.controller.resetChaosGameWithDescription("Sierpinski"); } catch (ChaosGameDescriptionFactoryException ex) { ex.printStackTrace(); + } catch (ChaosGameException ex) { + ex.printStackTrace(); } }); @@ -43,6 +48,8 @@ public class TopBar extends StackPane { this.controller.resetChaosGameWithDescription("Barnsley"); } catch (ChaosGameDescriptionFactoryException ex) { ex.printStackTrace(); + } catch (ChaosGameException ex) { + ex.printStackTrace(); } }); -- GitLab