From 3a9473fda8f79132e6ba69011ffd3dfc74fa3d28 Mon Sep 17 00:00:00 2001 From: Trishaan Date: Sun, 28 Sep 2025 18:58:34 -0700 Subject: [PATCH 1/9] Added sting validator to Idevice --- src/code/ca/bcit/comp2522/lab03/IDevice.java | 25 ++++++++++++++++++++ src/code/ca/bcit/comp2522/lab03/Main.java | 1 + 2 files changed, 26 insertions(+) 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 From c0050a6a3132080b5c144004b72ff5848a1523bf Mon Sep 17 00:00:00 2001 From: Trishaan Date: Sun, 28 Sep 2025 19:34:59 -0700 Subject: [PATCH 2/9] Added sting validator to Idevice --- src/code/ca/bcit/comp2522/lab03/IDevice.java | 9 +++++++++ src/code/ca/bcit/comp2522/lab03/IPad.java | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index e1801ab..473de7f 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -53,6 +53,15 @@ public abstract class IDevice { 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(String toBeValidated, int minCharacterCount, int maxCharacterCount) { if (toBeValidated == null) { throw new IllegalArgumentException("Value cannot be null"); diff --git a/src/code/ca/bcit/comp2522/lab03/IPad.java b/src/code/ca/bcit/comp2522/lab03/IPad.java index e8ea924..fad009d 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 int MAX_OSVERSION_CHARACTERS = 10; + private final int MIN_OSVERSION_CHARACTERS = 1; + private final boolean hasCase; private final String OSVersion; @@ -27,6 +30,9 @@ public class IPad extends IDevice { // TODO: validate String inputs this.hasCase = hasCase; + stringValidator(OSVersion, + MIN_OSVERSION_CHARACTERS, + MAX_OSVERSION_CHARACTERS); this.OSVersion = OSVersion; } From 804a7619fe378fa1b2869724093297cd705e22aa Mon Sep 17 00:00:00 2001 From: Trishaan Date: Sun, 28 Sep 2025 23:08:32 -0700 Subject: [PATCH 3/9] Removed TODO comments --- src/code/ca/bcit/comp2522/lab03/IDevice.java | 1 - src/code/ca/bcit/comp2522/lab03/IPad.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index 473de7f..8e99241 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -22,7 +22,6 @@ public abstract class IDevice { */ public IDevice(final String purpose) { - // TODO: validate String inputs stringValidator(purpose, MIN_PURPOSE_CHARACTER, MAX_PURPOSE_CHARACTER); diff --git a/src/code/ca/bcit/comp2522/lab03/IPad.java b/src/code/ca/bcit/comp2522/lab03/IPad.java index fad009d..03a144a 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPad.java +++ b/src/code/ca/bcit/comp2522/lab03/IPad.java @@ -27,8 +27,6 @@ public class IPad extends IDevice { final String OSVersion ) { super("learning"); - - // TODO: validate String inputs this.hasCase = hasCase; stringValidator(OSVersion, MIN_OSVERSION_CHARACTERS, From ad4296d6a416c8d56b0d944e20a8cd2cfdcb4d8c Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 01:32:34 -0700 Subject: [PATCH 4/9] Updated parameters to be final --- src/code/ca/bcit/comp2522/lab03/IDevice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index 8e99241..5361bad 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -61,7 +61,11 @@ public abstract class IDevice { * @param minCharacterCount passes the minimum number of characters that can be accepted * as an integer. * */ - public void stringValidator(String toBeValidated, int minCharacterCount, int maxCharacterCount) { + public void stringValidator(final String toBeValidated, + final int minCharacterCount, + final int maxCharacterCount) + { + if (toBeValidated == null) { throw new IllegalArgumentException("Value cannot be null"); } From a35e706c886521d3389a93071ecda0ac5f325651 Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 01:33:27 -0700 Subject: [PATCH 5/9] Added remainingPhonePlanMinutes validator --- src/code/ca/bcit/comp2522/lab03/IPhone.java | 36 +++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone.java b/src/code/ca/bcit/comp2522/lab03/IPhone.java index 3ea41a0..15ecf12 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,13 @@ public class IPhone extends IDevice { return hash; } + + public void minutesValidator(final double mins) + { + if(mins < NO_MINUTES_REMAINING) + { + throw new IllegalArgumentException("Minutes cannot be negative"); + } + + } } From e24adee9cc99c58ea137b076167dc412480facfc Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 01:45:21 -0700 Subject: [PATCH 6/9] Created memoryGB validator --- src/code/ca/bcit/comp2522/lab03/IPhone16.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone16.java b/src/code/ca/bcit/comp2522/lab03/IPhone16.java index 8099d8e..10d1c1e 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone16.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone16.java @@ -10,7 +10,7 @@ package ca.bcit.comp2522.lab03; * @version 1.0.0 */ public class IPhone16 extends IPhone { - + private static final int BASE_VARIANT_MEMORY = 16; private static final int IPHONE_HASH_WITH_HIGH_RES = 103; final boolean highResolutionCamera; @@ -31,9 +31,9 @@ public class IPhone16 extends IPhone { ) { super(remainingPhonePlanMinutes, carrier); - // TODO: validate memoryGB input? - this.highResolutionCamera = highResolutionCamera; + + memoryGBValidator(memoryGB); this.memoryGB = memoryGB; } @@ -117,4 +117,12 @@ public class IPhone16 extends IPhone { return hash; } + + public void memoryGBValidator(int memoryGB) + { + if (memoryGB % BASE_VARIANT_MEMORY != 0) + { + throw new IllegalArgumentException("Invalid Memory value (GB)"); + } + } } From 92d3c13c150a6f5d63abe20684859980216b48e7 Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 01:52:59 -0700 Subject: [PATCH 7/9] Added comments to memoryGBValidator. --- src/code/ca/bcit/comp2522/lab03/IPhone16.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone16.java b/src/code/ca/bcit/comp2522/lab03/IPhone16.java index 10d1c1e..6cad3f8 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone16.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone16.java @@ -10,7 +10,7 @@ package ca.bcit.comp2522.lab03; * @version 1.0.0 */ public class IPhone16 extends IPhone { - private static final int BASE_VARIANT_MEMORY = 16; + private static final int BASE_VARIANT_MEMORY = 128; private static final int IPHONE_HASH_WITH_HIGH_RES = 103; final boolean highResolutionCamera; @@ -118,6 +118,11 @@ public class IPhone16 extends IPhone { } + /** + * 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(int memoryGB) { if (memoryGB % BASE_VARIANT_MEMORY != 0) From 0385837c0d27d25c967248733db3154b1a545cc1 Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 02:08:14 -0700 Subject: [PATCH 8/9] Created numberOfSongsValidator and toString method for ipod. --- src/code/ca/bcit/comp2522/lab03/IPod.java | 31 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IPod.java b/src/code/ca/bcit/comp2522/lab03/IPod.java index dc2ac84..0109284 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,17 @@ 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(int songNumber) + { + if(songNumber < NO_SONGS) + { + throw new IllegalArgumentException("Number of songs cannot be less than 0"); + } + } } From 02bc37b44f36e1750a668bda094e53b302ae2d15 Mon Sep 17 00:00:00 2001 From: Trishaan Date: Mon, 29 Sep 2025 13:51:22 -0700 Subject: [PATCH 9/9] Added throws IllegalArgumentException to all required methods and made necessary variables final and static --- src/code/ca/bcit/comp2522/lab03/IDevice.java | 8 +++++--- src/code/ca/bcit/comp2522/lab03/IPad.java | 4 ++-- src/code/ca/bcit/comp2522/lab03/IPhone.java | 1 + src/code/ca/bcit/comp2522/lab03/IPhone16.java | 12 +++++++----- src/code/ca/bcit/comp2522/lab03/IPod.java | 3 ++- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index 5361bad..f0f5c90 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -10,8 +10,8 @@ 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 static int MIN_PURPOSE_CHARACTER = 1; + private final static int MAX_PURPOSE_CHARACTER = 255; private final String purpose; @@ -20,7 +20,8 @@ public abstract class IDevice { * * @param purpose of IDevice */ - public IDevice(final String purpose) { + public IDevice(final String purpose) + { stringValidator(purpose, MIN_PURPOSE_CHARACTER, @@ -64,6 +65,7 @@ public abstract class IDevice { public void stringValidator(final String toBeValidated, final int minCharacterCount, final int maxCharacterCount) + throws IllegalArgumentException { if (toBeValidated == null) { diff --git a/src/code/ca/bcit/comp2522/lab03/IPad.java b/src/code/ca/bcit/comp2522/lab03/IPad.java index 03a144a..d91bcdf 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPad.java +++ b/src/code/ca/bcit/comp2522/lab03/IPad.java @@ -11,8 +11,8 @@ package ca.bcit.comp2522.lab03; */ public class IPad extends IDevice { - private final int MAX_OSVERSION_CHARACTERS = 10; - private final int MIN_OSVERSION_CHARACTERS = 1; + private final static int MAX_OSVERSION_CHARACTERS = 10; + private final static int MIN_OSVERSION_CHARACTERS = 1; private final boolean hasCase; private final String OSVersion; diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone.java b/src/code/ca/bcit/comp2522/lab03/IPhone.java index 15ecf12..8121dc2 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone.java @@ -129,6 +129,7 @@ public class IPhone extends IDevice { } public void minutesValidator(final double mins) + throws IllegalArgumentException { if(mins < NO_MINUTES_REMAINING) { diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone16.java b/src/code/ca/bcit/comp2522/lab03/IPhone16.java index 6cad3f8..aa42480 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone16.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone16.java @@ -12,6 +12,7 @@ package ca.bcit.comp2522.lab03; 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,10 +25,10 @@ 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); @@ -123,9 +124,10 @@ public class IPhone16 extends IPhone { * since it needs to be a multiple of 128. * @param memoryGB passes the value of the memory to the method. * */ - public void memoryGBValidator(int memoryGB) + public void memoryGBValidator(final int memoryGB) + throws IllegalArgumentException { - if (memoryGB % BASE_VARIANT_MEMORY != 0) + 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 0109284..79513ce 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPod.java +++ b/src/code/ca/bcit/comp2522/lab03/IPod.java @@ -129,7 +129,8 @@ public class IPod extends IDevice { * passed are not negative. * @param songNumber passes the value of the number of songs. * */ - public void numberOfSongsValidator(int songNumber) + public void numberOfSongsValidator(final int songNumber) + throws IllegalArgumentException { if(songNumber < NO_SONGS) {