diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index f441b2b..f0f5c90 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 static int MIN_PURPOSE_CHARACTER = 1; + private final static int MAX_PURPOSE_CHARACTER = 255; private final String purpose; /** * IDevice constructor + * * @param purpose of IDevice */ - public IDevice(final String purpose) { + 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,42 @@ public abstract class IDevice { /** * toString returns details of IDevice + * * @return purpose of IDevice */ @Override public String toString() { return this.purpose; } + + /** + * stringValidator checks if the string is within character + * bounds and if it is blank, null or empty. + * @param toBeValidated passes the string that needs to be validated. + * @param maxCharacterCount passes the maximum number of characters that can be accepted + * as an integer. + * @param minCharacterCount passes the minimum number of characters that can be accepted + * as an integer. + * */ + public void stringValidator(final String toBeValidated, + final int minCharacterCount, + final int maxCharacterCount) + throws IllegalArgumentException + { + + 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/IPad.java b/src/code/ca/bcit/comp2522/lab03/IPad.java index e8ea924..d91bcdf 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPad.java +++ b/src/code/ca/bcit/comp2522/lab03/IPad.java @@ -11,6 +11,9 @@ package ca.bcit.comp2522.lab03; */ public class IPad extends IDevice { + private final static int MAX_OSVERSION_CHARACTERS = 10; + private final static int MIN_OSVERSION_CHARACTERS = 1; + private final boolean hasCase; private final String OSVersion; @@ -24,9 +27,10 @@ public class IPad extends IDevice { final String OSVersion ) { super("learning"); - - // TODO: validate String inputs this.hasCase = hasCase; + stringValidator(OSVersion, + MIN_OSVERSION_CHARACTERS, + MAX_OSVERSION_CHARACTERS); this.OSVersion = OSVersion; } diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone.java b/src/code/ca/bcit/comp2522/lab03/IPhone.java index 3ea41a0..8121dc2 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone.java @@ -10,7 +10,9 @@ package ca.bcit.comp2522.lab03; * @version 1.0.0 */ public class IPhone extends IDevice { - + private static final int MIN_CAREER_CHARACTERS = 3; + private static final int MAX_CAREER_CHARACTERS = 15; + private static final double NO_MINUTES_REMAINING = 0.0; private static final int IPHONE_HASH = 11; private static final int IPHONE_HASH_CODE = 101; @@ -24,12 +26,16 @@ public class IPhone extends IDevice { */ public IPhone( final double remainingPhonePlanMinutes, - final String carrier - ) { + final String carrier) + { super("talking"); - // TODO: validate String inputs, possibly other inputs too + minutesValidator(remainingPhonePlanMinutes); this.remainingPhonePlanMinutes = remainingPhonePlanMinutes; + + stringValidator(carrier, + MIN_CAREER_CHARACTERS, + MAX_CAREER_CHARACTERS); this.carrier = carrier; } @@ -67,8 +73,17 @@ public class IPhone extends IDevice { @Override public String toString() { - // TODO: convert to StringBuilder, refer to IPad - return super.toString() + " " + this.remainingPhonePlanMinutes + " " + this.carrier; + final StringBuilder iphoneDetails; + + iphoneDetails = new StringBuilder(); + + iphoneDetails.append(super.toString()); + iphoneDetails.append(" "); + iphoneDetails.append(this.remainingPhonePlanMinutes); + iphoneDetails.append(" "); + iphoneDetails.append(this.carrier); + + return iphoneDetails.toString(); } /** @@ -112,4 +127,14 @@ public class IPhone extends IDevice { return hash; } + + public void minutesValidator(final double mins) + throws IllegalArgumentException + { + if(mins < NO_MINUTES_REMAINING) + { + throw new IllegalArgumentException("Minutes cannot be negative"); + } + + } } diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone16.java b/src/code/ca/bcit/comp2522/lab03/IPhone16.java index 8099d8e..aa42480 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone16.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone16.java @@ -10,8 +10,9 @@ package ca.bcit.comp2522.lab03; * @version 1.0.0 */ public class IPhone16 extends IPhone { - + private static final int BASE_VARIANT_MEMORY = 128; private static final int IPHONE_HASH_WITH_HIGH_RES = 103; + private static final int IS_A_MULTIPLE_OF_BASE_VARIANT = 0; final boolean highResolutionCamera; final int memoryGB; @@ -24,16 +25,16 @@ public class IPhone16 extends IPhone { * @param memoryGB of this IPhone */ public IPhone16( - final double remainingPhonePlanMinutes, - final String carrier, + final double remainingPhonePlanMinutes, + final String carrier, final boolean highResolutionCamera, - final int memoryGB + final int memoryGB ) { super(remainingPhonePlanMinutes, carrier); - // TODO: validate memoryGB input? - this.highResolutionCamera = highResolutionCamera; + + memoryGBValidator(memoryGB); this.memoryGB = memoryGB; } @@ -117,4 +118,18 @@ public class IPhone16 extends IPhone { return hash; } + + /** + * memoryGBValidator makes sure the right memory storage is assigned + * since it needs to be a multiple of 128. + * @param memoryGB passes the value of the memory to the method. + * */ + public void memoryGBValidator(final int memoryGB) + throws IllegalArgumentException + { + if (memoryGB % BASE_VARIANT_MEMORY != IS_A_MULTIPLE_OF_BASE_VARIANT) + { + throw new IllegalArgumentException("Invalid Memory value (GB)"); + } + } } diff --git a/src/code/ca/bcit/comp2522/lab03/IPod.java b/src/code/ca/bcit/comp2522/lab03/IPod.java index dc2ac84..79513ce 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPod.java +++ b/src/code/ca/bcit/comp2522/lab03/IPod.java @@ -11,6 +11,7 @@ package ca.bcit.comp2522.lab03; */ public class IPod extends IDevice { + private static final int NO_SONGS = 0; private static final int IPOD_HASH = 7; private static final int IPOD_HASH_CODE = 97; @@ -28,9 +29,9 @@ public class IPod extends IDevice { ) { super("music"); - // TODO: validate numberOfSongs input - + numberOfSongsValidator(numberOfSongs); this.numberOfSongs = numberOfSongs; + this.maxVolumeDecibels = maxVolumeDecibels; } @@ -68,8 +69,17 @@ public class IPod extends IDevice { @Override public String toString() { - // TODO: change this to StringBuilder, refer to IPad - return super.toString() + " " + this.numberOfSongs + " " + this.maxVolumeDecibels; + final StringBuilder ipodDetails; + + ipodDetails = new StringBuilder(); + + ipodDetails.append(super.toString()); + ipodDetails.append(" "); + ipodDetails.append(this.numberOfSongs); + ipodDetails.append(" "); + ipodDetails.append(this.maxVolumeDecibels); + + return ipodDetails.toString(); } /** @@ -113,4 +123,18 @@ public class IPod extends IDevice { return hash; } + + /** + * numberOfSongsValidator makes sure the number of songs being + * passed are not negative. + * @param songNumber passes the value of the number of songs. + * */ + public void numberOfSongsValidator(final int songNumber) + throws IllegalArgumentException + { + if(songNumber < NO_SONGS) + { + throw new IllegalArgumentException("Number of songs cannot be less than 0"); + } + } } 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