diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index f441b2b..e1801ab 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -10,16 +10,22 @@ package ca.bcit.comp2522.lab03; * @version 1.0.0 */ public abstract class IDevice { + private final int MIN_PURPOSE_CHARACTER = 1; + private final int MAX_PURPOSE_CHARACTER = 255; private final String purpose; /** * IDevice constructor + * * @param purpose of IDevice */ public IDevice(final String purpose) { // TODO: validate String inputs + stringValidator(purpose, + MIN_PURPOSE_CHARACTER, + MAX_PURPOSE_CHARACTER); this.purpose = purpose; } @@ -30,6 +36,7 @@ public abstract class IDevice { /** * getPurpose of IDevice + * * @return purpose of IDevice */ public String getPurpose() { @@ -38,10 +45,28 @@ public abstract class IDevice { /** * toString returns details of IDevice + * * @return purpose of IDevice */ @Override public String toString() { return this.purpose; } + + public void stringValidator(String toBeValidated, int minCharacterCount, int maxCharacterCount) { + if (toBeValidated == null) { + throw new IllegalArgumentException("Value cannot be null"); + } + if (toBeValidated.isEmpty()) { + throw new IllegalArgumentException("Value cannot be empty"); + } + if (toBeValidated.isBlank()) { + throw new IllegalArgumentException("Value cannot be blank"); + } + if (toBeValidated.length() < minCharacterCount || toBeValidated.length() > maxCharacterCount) { + throw new IllegalArgumentException("Value length must be between" + + minCharacterCount + "and " + maxCharacterCount + " characters"); + } + } } + diff --git a/src/code/ca/bcit/comp2522/lab03/Main.java b/src/code/ca/bcit/comp2522/lab03/Main.java index f7d5a55..737e309 100644 --- a/src/code/ca/bcit/comp2522/lab03/Main.java +++ b/src/code/ca/bcit/comp2522/lab03/Main.java @@ -69,6 +69,7 @@ public class Main { final IPhone iphone1; final IPhone iphone2; final IPhone iphone3; + iphone1 = new IPhone(120.0, "Verizon"); // 120 minutes, carrier Verizon iphone2 = new IPhone(180.0, "T-Mobile"); // 180 minutes, carrier T-Mobile iphone3 = new IPhone(120.0, "AT&T"); // 120 minutes, carrier AT&T