From 45d26fdf5e1a019b5e9bbdc9fedaade7ff758a55 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Fri, 3 Oct 2025 20:35:36 -0700 Subject: [PATCH] create remaining lab04 classes and interfaces --- .idea/workspace.xml | 4 +- src/code/ca/bcit/comp2522/lab04/Author.java | 37 +++++++++++ .../ca/bcit/comp2522/lab04/Autobiography.java | 28 +++++++++ .../ca/bcit/comp2522/lab04/Biography.java | 35 +++++++++++ src/code/ca/bcit/comp2522/lab04/Book.java | 43 +++++++++++++ src/code/ca/bcit/comp2522/lab04/Name.java | 46 +------------- src/code/ca/bcit/comp2522/lab04/Person.java | 20 ++---- .../ca/bcit/comp2522/lab04/Reversible.java | 9 +++ .../ca/bcit/comp2522/lab04/Validator.java | 62 +++++++++++++++++++ 9 files changed, 222 insertions(+), 62 deletions(-) create mode 100644 src/code/ca/bcit/comp2522/lab04/Author.java create mode 100644 src/code/ca/bcit/comp2522/lab04/Autobiography.java create mode 100644 src/code/ca/bcit/comp2522/lab04/Biography.java create mode 100644 src/code/ca/bcit/comp2522/lab04/Book.java create mode 100644 src/code/ca/bcit/comp2522/lab04/Reversible.java create mode 100644 src/code/ca/bcit/comp2522/lab04/Validator.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5e0b70e..f27ca33 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,11 +5,8 @@ - - - diff --git a/src/code/ca/bcit/comp2522/lab04/Author.java b/src/code/ca/bcit/comp2522/lab04/Author.java new file mode 100644 index 0000000..87d673d --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Author.java @@ -0,0 +1,37 @@ +package ca.bcit.comp2522.lab04; + +/** + * Authors are just a Person but also + * have a genre in which they write. + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Author extends Person { + private static final int MAX_GENRE_LENGTH = 30; + + private final String genre; + + /** + * Author constructor + * @param name of the author + * @param dateOfBirth of the author + * @param dateOfDeath of author if they did die (can be null) + * @param genre that the author writes in + */ + public Author( + final Name name, + final Date dateOfBirth, + final Date dateOfDeath, + final String genre + ) { + super(dateOfBirth, dateOfDeath, name); + + Validator.validateString(genre, MAX_GENRE_LENGTH); + + this.genre = genre; + } +} diff --git a/src/code/ca/bcit/comp2522/lab04/Autobiography.java b/src/code/ca/bcit/comp2522/lab04/Autobiography.java new file mode 100644 index 0000000..7f83566 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Autobiography.java @@ -0,0 +1,28 @@ +package ca.bcit.comp2522.lab04; + +/** + * Autobiography is just a Biography about the + * author and not another Person. + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Autobiography extends Biography +{ + /** + * Autobiography constructor + * @param title of autobiography + * @param year published + * @param author and subject of autobiography + */ + public Autobiography( + final String title, + final int year, + final Author author + ) { + super(title, year, author, author); + } +} diff --git a/src/code/ca/bcit/comp2522/lab04/Biography.java b/src/code/ca/bcit/comp2522/lab04/Biography.java new file mode 100644 index 0000000..1d8eab2 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Biography.java @@ -0,0 +1,35 @@ +package ca.bcit.comp2522.lab04; + +/** + * Biography is a Book about a Person + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Biography extends Book +{ + private final Person subject; + + /** + * Biography constructor + * @param title of Biography + * @param year published + * @param author of Biography + * @param subject that the Biography is about + */ + public Biography( + final String title, + final int year, + final Author author, + final Person subject + ) { + super(title, year, author); + + Validator.validateObject(subject, "subject"); + + this.subject = subject; + } +} diff --git a/src/code/ca/bcit/comp2522/lab04/Book.java b/src/code/ca/bcit/comp2522/lab04/Book.java new file mode 100644 index 0000000..203e399 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Book.java @@ -0,0 +1,43 @@ +package ca.bcit.comp2522.lab04; + +// TODO: Implement Comparable, Printable, Reversible + +/** + * Book has an Author, year, and title + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Book +{ + private static final int MAX_TITLE_LENGTH = 100; + private static final int MIN_YEAR = 1; + private static final int MAX_YEAR = 2025; + + private final String title; + private final int year; + private final Author author; + + /** + * Book constructor + * @param title of the book + * @param year the book was published + * @param author of the book + */ + public Book( + final String title, + final int year, + final Author author + ) { + Validator.validateString(title, MAX_TITLE_LENGTH); + Validator.validateObject(author, "author"); + Validator.validateInteger(year, MIN_YEAR, MAX_YEAR); + + this.title = title; + this.year = year; + this.author = author; + } +} diff --git a/src/code/ca/bcit/comp2522/lab04/Name.java b/src/code/ca/bcit/comp2522/lab04/Name.java index 80cf20d..cef4d71 100644 --- a/src/code/ca/bcit/comp2522/lab04/Name.java +++ b/src/code/ca/bcit/comp2522/lab04/Name.java @@ -15,47 +15,7 @@ public class Name implements Printable private final String first; private final String last; - private static final int MAX_CHARACTERS = 50; - - /** - * stringValidator checks the string being passed into it for - * being null, blank, empty or over the limit of characters. - * - * @param stringToCheck passes the string to be checked. - * @param maxLength passes the integer value of the number of character count - * the String can be. - * */ - private static void stringValidator(final String stringToCheck, final int maxLength) - { - - if (stringToCheck == null) - { - throw new IllegalArgumentException("String cannot be null"); - } - - if (stringToCheck.isEmpty() || - stringToCheck.isBlank()) - { - throw new IllegalArgumentException("String cannot be empty or blank"); - } - - if (stringToCheck.length() > maxLength) - { - throw new IllegalArgumentException( - "String longer than " + maxLength + - " characters" - ); - } - } - - /** - * stringValidator with default MAX_CHARACTERS restriction - * @param stringToCheck passes the string to be checked - */ - private static void stringValidator(final String stringToCheck) - { - stringValidator(stringToCheck, MAX_CHARACTERS); - } + private static final int MAX_NAME_CHARACTERS = 50; /** * Name constructor @@ -64,8 +24,8 @@ public class Name implements Printable */ public Name(final String first, final String last) { - stringValidator(first); - stringValidator(last); + Validator.validateString(first, MAX_NAME_CHARACTERS); + Validator.validateString(last, MAX_NAME_CHARACTERS); this.first = first; this.last = last; diff --git a/src/code/ca/bcit/comp2522/lab04/Person.java b/src/code/ca/bcit/comp2522/lab04/Person.java index 2c92b7e..1595b14 100644 --- a/src/code/ca/bcit/comp2522/lab04/Person.java +++ b/src/code/ca/bcit/comp2522/lab04/Person.java @@ -1,5 +1,7 @@ package ca.bcit.comp2522.lab04; +// TODO: implement Comparable, Printable, Reversible + /** * Person contains date of birth, death, and a name. * @@ -15,20 +17,6 @@ public class Person private final Date dateOfDeath; private final Name name; - /** - * validateObject ensures a given object is not null - * @param object to ensure is not null - * @param objectName variable name of object for log - */ - private static void validateObject( - final Object object, - final String objectName - ) { - if (object == null) { - throw new IllegalArgumentException(objectName + " cannot be null"); - } - } - /** * Person construct * @param dateOfBirth of person, cannot be null @@ -40,8 +28,8 @@ public class Person final Date dateOfDeath, final Name name ) { - validateObject(dateOfBirth, "date of birth"); - validateObject(name, "name"); + Validator.validateObject(dateOfBirth, "date of birth"); + Validator.validateObject(name, "name"); this.dateOfBirth = dateOfBirth; this.dateOfDeath = dateOfDeath; diff --git a/src/code/ca/bcit/comp2522/lab04/Reversible.java b/src/code/ca/bcit/comp2522/lab04/Reversible.java new file mode 100644 index 0000000..85749f2 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Reversible.java @@ -0,0 +1,9 @@ +package ca.bcit.comp2522.lab04; + +/** + * Reversible classes must print their name/title + * backwards. + */ +public interface Reversible { + void backward(); +} diff --git a/src/code/ca/bcit/comp2522/lab04/Validator.java b/src/code/ca/bcit/comp2522/lab04/Validator.java new file mode 100644 index 0000000..b4f762c --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Validator.java @@ -0,0 +1,62 @@ +package ca.bcit.comp2522.lab04; + +/** + * Validator provides useful validation methods + * that many classes can use. + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public final class Validator +{ + + /** + * validateString ensures a string is not empty, blank, + * null or greater than maxLength + * @param string to validate + * @param maxLength that string can be + */ + public static void validateString(final String string, final int maxLength) { + if (string == null || string.isEmpty()) { + throw new IllegalArgumentException("string cannot be null or empty"); + } + + if (string.length() > maxLength) { + throw new IllegalArgumentException("string cannot be longer than " + maxLength); + } + } + + /** + * validateObject ensures a given object is not null + * @param object to ensure is not null + * @param objectName variable name of object for log + */ + public static void validateObject( + final Object object, + final String objectName + ) { + if (object == null) { + throw new IllegalArgumentException(objectName + " cannot be null"); + } + } + + /** + * validateInteger ensures a given number is within + * the correct provided range + * @param number to validate + * @param min of range inclusive + * @param max of range inclusive + */ + public static void validateInteger( + final int number, + final int min, + final int max + ) { + if (number < min || number > max) { + throw new IllegalArgumentException(number + " is out of range" + min + " to " + max); + } + } +}