From 3d2e56e093b7f71d868cb5076a11b3e0a98af797 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Mon, 26 Feb 2018 17:27:02 +0100 Subject: [PATCH] Added worm constructors and name checking --- docs/samenvatting.md | 2 +- src/IllegalNameException.java | 57 +++++++++++++++++++++++++++++++++++ src/Worm.java | 27 ++++++++++++----- 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/IllegalNameException.java diff --git a/docs/samenvatting.md b/docs/samenvatting.md index 532a7eb..f9fdd35 100644 --- a/docs/samenvatting.md +++ b/docs/samenvatting.md @@ -41,7 +41,7 @@ total -### naam +### naam - DONE min 2 letters 1e letter uppercase alleen letters quotes en spaties diff --git a/src/IllegalNameException.java b/src/IllegalNameException.java new file mode 100644 index 0000000..b64dafd --- /dev/null +++ b/src/IllegalNameException.java @@ -0,0 +1,57 @@ +import be.kuleuven.cs.som.annotate.*; + +/** + * A class for signalling illegal names for worms + * + * Each illegal name exception involves the name and the index of the character where the error occurred. + * + * @version 1.0 + * @author Arthur Bols / Leen Dereu + */ +public class IllegalNameException extends RuntimeException { + + /** + * Variable registering the index of this illegal name exception + */ + private final int index; + + /** + * Variable referencing the naem of this illegal name exception + */ + private final String name; + + /** + * Initalize this new illegal name exception with given operands + * + * @param index + * The index of the character where the exception occurred. + * @param name + * The String where the error occurred. + * + * @post The index of this new illegal name exception is set to the given index + * | new.getIndex() == index + * + * @post The name of this new illegal name exception is set to the given name + * | new.getName() == name + */ + public IllegalNameException(int index, String name) { + this.index = index; + this.name = name; + } + + /** + * Return the index of this illegal name exception. + */ + @Basic @Immutable + public int getIndex() { + return this.index; + } + + /** + * Return the name of this illegal name exception. + */ + @Basic @Immutable + public String getName() { + return this.name; + } +} diff --git a/src/Worm.java b/src/Worm.java index 795c8d9..228d04f 100644 --- a/src/Worm.java +++ b/src/Worm.java @@ -27,32 +27,43 @@ public class Worm { this.orientation = orientation; this.radius = radius; this.minRadius = 0.25; + + int validValue = isValidName(name); + if (validValue != -1) { + throw new IllegalNameException(validValue, name); + } this.name = name; } public Worm(double coordX, double coordY, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { + this.coordX = coordX; this.coordY = coordY; this.orientation = orientation; this.radius = radius; this.minRadius = minRadius; + + int validValue = isValidName(name); + if (validValue != -1) { + throw new IllegalNameException(validValue, name); + } this.name = name; } - private boolean isValidName (String name) { + private int isValidName (String name) { if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) { - return false; + return 0; } - //char allowedCharacters[] = {'\'', '"', ' '}; - Set allowedCharacters[] = new + String allowedCharacters = "'\" "; + for (int i = 0; i < name.length(); i++) { if (!Character.isLetter(name.charAt(i))) { - if (!allowedCharacters.contains() name.charAt(i)) + if (allowedCharacters.indexOf(name.charAt(i)) == -1) { + return i; + } } } - - - return false; + return -1; } }