create remaining lab04 classes and interfaces
This commit is contained in:
9 files changed
+222
-62
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package ca.bcit.comp2522.lab04;
|
||||
|
||||
/**
|
||||
* Reversible classes must print their name/title
|
||||
* backwards.
|
||||
*/
|
||||
public interface Reversible {
|
||||
void backward();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user