Added sting validator to Idevice

This commit is contained in:
Trishaan committed 2025-09-28 18:58:34 -07:00
1 parent 24f19b3533
commit 3a9473fda8
2 files changed
+26

No files matched your search

@@ -10,16 +10,22 @@ package ca.bcit.comp2522.lab03;
* @version 1.0.0 * @version 1.0.0
*/ */
public abstract class IDevice { public abstract class IDevice {
private final int MIN_PURPOSE_CHARACTER = 1;
private final int MAX_PURPOSE_CHARACTER = 255;
private final String purpose; private final String purpose;
/** /**
* IDevice constructor * IDevice constructor
*
* @param purpose of IDevice * @param purpose of IDevice
*/ */
public IDevice(final String purpose) { public IDevice(final String purpose) {
// TODO: validate String inputs // TODO: validate String inputs
stringValidator(purpose,
MIN_PURPOSE_CHARACTER,
MAX_PURPOSE_CHARACTER);
this.purpose = purpose; this.purpose = purpose;
} }
@@ -30,6 +36,7 @@ public abstract class IDevice {
/** /**
* getPurpose of IDevice * getPurpose of IDevice
*
* @return purpose of IDevice * @return purpose of IDevice
*/ */
public String getPurpose() { public String getPurpose() {
@@ -38,10 +45,28 @@ public abstract class IDevice {
/** /**
* toString returns details of IDevice * toString returns details of IDevice
*
* @return purpose of IDevice * @return purpose of IDevice
*/ */
@Override @Override
public String toString() { public String toString() {
return this.purpose; 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");
}
}
} }
@@ -69,6 +69,7 @@ public class Main {
final IPhone iphone1; final IPhone iphone1;
final IPhone iphone2; final IPhone iphone2;
final IPhone iphone3; final IPhone iphone3;
iphone1 = new IPhone(120.0, "Verizon"); // 120 minutes, carrier Verizon iphone1 = new IPhone(120.0, "Verizon"); // 120 minutes, carrier Verizon
iphone2 = new IPhone(180.0, "T-Mobile"); // 180 minutes, carrier T-Mobile iphone2 = new IPhone(180.0, "T-Mobile"); // 180 minutes, carrier T-Mobile
iphone3 = new IPhone(120.0, "AT&T"); // 120 minutes, carrier AT&T iphone3 = new IPhone(120.0, "AT&T"); // 120 minutes, carrier AT&T