diff --git a/src/code/ca/bcit/comp2522/lab04/Author.java b/src/code/ca/bcit/comp2522/lab04/Author.java index 87d673d..4474250 100644 --- a/src/code/ca/bcit/comp2522/lab04/Author.java +++ b/src/code/ca/bcit/comp2522/lab04/Author.java @@ -10,7 +10,7 @@ package ca.bcit.comp2522.lab04; * @author Calvin Arifianto * @version 1.0.0 */ -public class Author extends Person { +public class Author extends Person implements Printable{ private static final int MAX_GENRE_LENGTH = 30; private final String genre; @@ -34,4 +34,23 @@ public class Author extends Person { this.genre = genre; } + + @Override + public void display() { + + StringBuilder builder; + builder = new StringBuilder(); + + builder.append("Name: "); + builder.append(this.name.getFullName()); + builder.append("\nDate of Birth: "); + builder.append(this.dateOfBirth); + builder.append("\nDate of Death: "); + builder.append(this.dateOfDeath); + builder.append("\nGenre: "); + builder.append(this.genre); + + + System.out.println(builder.toString()); + } } diff --git a/src/code/ca/bcit/comp2522/lab04/Autobiography.java b/src/code/ca/bcit/comp2522/lab04/Autobiography.java index 7f83566..061da89 100644 --- a/src/code/ca/bcit/comp2522/lab04/Autobiography.java +++ b/src/code/ca/bcit/comp2522/lab04/Autobiography.java @@ -10,7 +10,7 @@ package ca.bcit.comp2522.lab04; * @author Calvin Arifianto * @version 1.0.0 */ -public class Autobiography extends Biography +public class Autobiography extends Biography implements Printable { /** * Autobiography constructor @@ -25,4 +25,23 @@ public class Autobiography extends Biography ) { super(title, year, author, author); } + + @Override + public void display() { + + StringBuilder builder; + builder = new StringBuilder(); + + builder.append("Title: "); + builder.append(this.title); + builder.append(" - Year: "); + builder.append(this.year); + builder.append(" - Author: "); + builder.append(this.author.getFullName()); + builder.append(" - Subject: "); + builder.append(this.subject.getFullName()); + + + System.out.println(builder.toString()); + } } diff --git a/src/code/ca/bcit/comp2522/lab04/Biography.java b/src/code/ca/bcit/comp2522/lab04/Biography.java index 1d8eab2..1b4315b 100644 --- a/src/code/ca/bcit/comp2522/lab04/Biography.java +++ b/src/code/ca/bcit/comp2522/lab04/Biography.java @@ -9,9 +9,9 @@ package ca.bcit.comp2522.lab04; * @author Calvin Arifianto * @version 1.0.0 */ -public class Biography extends Book +public class Biography extends Book implements Printable { - private final Person subject; + final Person subject; /** * Biography constructor @@ -32,4 +32,52 @@ public class Biography extends Book this.subject = subject; } + + @Override + public boolean equals(Object o) { + + if (o == null || getClass() != o.getClass()) + { + return false; + } + + Biography other; + other = (Biography)o; + + if (this == o) + { + return true; + } + + return this.subject.equals(other.subject); + } + + @Override + public int hashCode() { + if(subject == null) + { + return 0; + } + return subject.hashCode(); + } + + + @Override + public void display() { + + StringBuilder builder; + builder = new StringBuilder(); + + builder.append("Title: "); + builder.append(this.title); + builder.append("\nYear: "); + builder.append(this.year); + builder.append("\nAuthor: "); + builder.append(this.author.getFullName()); + builder.append("\nSubject: "); + builder.append(this.subject.getFullName()); + + + System.out.println(builder.toString()); + } } diff --git a/src/code/ca/bcit/comp2522/lab04/Book.java b/src/code/ca/bcit/comp2522/lab04/Book.java index 203e399..fd50405 100644 --- a/src/code/ca/bcit/comp2522/lab04/Book.java +++ b/src/code/ca/bcit/comp2522/lab04/Book.java @@ -12,14 +12,17 @@ package ca.bcit.comp2522.lab04; * @version 1.0.0 */ public class Book + implements Comparable, + Printable, + Reversible { 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; + protected final String title; + protected final int year; + protected final Author author; /** * Book constructor @@ -40,4 +43,45 @@ public class Book this.year = year; this.author = author; } + + @Override + public void display() { + + StringBuilder builder; + builder = new StringBuilder(); + + builder.append("Title: "); + builder.append(this.title); + builder.append("\nYear: "); + builder.append(this.year); + builder.append("\nAuthor: "); + builder.append(this.author.getFullName()); + + + System.out.println(builder.toString()); + } + + @Override + public void backward() + { + final StringBuilder builder; + builder = new StringBuilder(); + + for (int i = this.title.length() - 1; i >= 0; i--) { + builder.append(title.charAt(i)); + } + + System.out.println(builder.toString()); + } + + @Override + public int compareTo(Book other) { + if (this.year < other.year) { + return -1; + } else if (this.year > other.year) { + return 1; + } + + return 0; + } } diff --git a/src/code/ca/bcit/comp2522/lab04/Date.java b/src/code/ca/bcit/comp2522/lab04/Date.java index 8cbfd91..0b9c9a7 100644 --- a/src/code/ca/bcit/comp2522/lab04/Date.java +++ b/src/code/ca/bcit/comp2522/lab04/Date.java @@ -11,11 +11,11 @@ package ca.bcit.comp2522.lab04; * @author Calvin Arifianto * @version 1.0.0 */ -public final class Date implements Printable +public final class Date implements Printable, Comparable { // Domain of dates - private static final int MIN_YEAR = 1800; + private static final int MIN_YEAR = 1500; private static final int MAX_YEAR = 2025; private static final int MIN_DAY = 1; @@ -395,4 +395,30 @@ public final class Date implements Printable public void display() { System.out.println(getYyyyMmDd()); } + + /** + * Compares two dates, the older is the "largest"/"bigger" + */ + @Override + public int compareTo(Date other) { + if (this.year < other.year) { + return -1; + } else if (this.year > other.year) { + return 1; + } + + if (this.month < other.month) { + return -1; + } else if (this.month > other.month) { + return 1; + } + + if (this.day < other.day) { + return -1; + } else if (this.day > other.day) { + return 1; + } + + return 0; // years, months, and days are all equal + } } diff --git a/src/code/ca/bcit/comp2522/lab04/Main.java b/src/code/ca/bcit/comp2522/lab04/Main.java new file mode 100644 index 0000000..ee6bc61 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab04/Main.java @@ -0,0 +1,115 @@ +package ca.bcit.comp2522.lab04; + +public class Main { + public static void main(final String[] args) { + + Author author1; + Name nameAuthor1; + Date birthAuthor1; + Date deathAuthor1; + + nameAuthor1 = new Name("George", "Orwell"); + birthAuthor1 = new Date(1903,6,25); + deathAuthor1 = new Date(1950,1,21); + + author1 = new Author(nameAuthor1,birthAuthor1,deathAuthor1, "Dystopia"); + + + Author author2; + Name nameAuthor2; + Date birthAuthor2; + Date deathAuthor2; + + nameAuthor2 = new Name("Harper", "Lee"); + birthAuthor2 = new Date(1926,4,28); + deathAuthor2 = new Date(2016,2,19); + + author2 = new Author(nameAuthor2,birthAuthor2,deathAuthor2, "Literature/fiction"); + + Author author3; + Name nameAuthor3; + Date birthAuthor3; + Date deathAuthor3; + + nameAuthor3 = new Name("Jane", "Austen"); + birthAuthor3 = new Date(1775,12,16); + deathAuthor3 = new Date(1817,7,18); + + author3 = new Author(nameAuthor3,birthAuthor3,deathAuthor3, "Literary realism"); + + + Author author4; + Name nameAuthor4; + Date birthAuthor4; + Date deathAuthor4; + + nameAuthor4 = new Name("F. Scott", "Fitzgerald"); + birthAuthor4 = new Date(1896,9,24); + deathAuthor4 = new Date(1940,12,21); + + author4 = new Author(nameAuthor4,birthAuthor4,deathAuthor4, "Modernist fiction"); + + Author author5; + Name nameAuthor5; + Date birthAuthor5; + Date deathAuthor5; + + nameAuthor5 = new Name("Herman", "Melville"); + birthAuthor5 = new Date(1819,8,1); + deathAuthor5 = new Date(1891,9,28); + + author5 = new Author(nameAuthor5,birthAuthor5,deathAuthor5, "Travelogue"); + + Book book1; + Book book2; + Book book3; + Book book4; + Book book5; + + book1 = new Book("1984", 1949, author1); + book2 = new Book("To Kill a Mockingbird", 1960, author2); + book3 = new Book("Pride and Prejudice", 1813, author3); + book4 = new Book("The Great Gatsby", 1925, author4); + book5 = new Book("Moby-Dick", 1851, author5); + + Author biographyAuthor1; + + Name nameBiographyAuthor1; + Date birthBiographyAuthor1; + Date deathBiographyAuthor1; + + nameBiographyAuthor1 = new Name("Robert","Caro"); + birthBiographyAuthor1 = new Date(1935,10,30); + deathBiographyAuthor1 = new Date(1935,10,30); + + Person biographySubject1; + + Name nameBiographySubject1; + Date birthBiographySubject1; + Date deathBiographySubject1; + + nameBiographySubject1 = new Name("Robert","Moses"); + birthBiographySubject1 = new Date(1888,12,18); + deathBiographySubject1 = new Date(1981,7, 29); + + biographyAuthor1 = new Author(nameBiographyAuthor1,birthBiographyAuthor1, deathBiographyAuthor1, "Biography"); + biographySubject1 = new Person(birthBiographySubject1, deathBiographySubject1, nameBiographySubject1); + + Biography biography1; + Biography biography2; + Biography biography3; + Biography biography4; + Biography biography5; + + biography1 = new Biography("The Power Broker", 1974, biographyAuthor1, biographySubject1); + + Autobiography AutoBio1; + Autobiography AutoBio2; + Autobiography AutoBio3; + Autobiography AutoBio4; + Autobiography AutoBio5; + + + + } +} diff --git a/src/code/ca/bcit/comp2522/lab04/Name.java b/src/code/ca/bcit/comp2522/lab04/Name.java index cef4d71..808a73c 100644 --- a/src/code/ca/bcit/comp2522/lab04/Name.java +++ b/src/code/ca/bcit/comp2522/lab04/Name.java @@ -31,9 +31,13 @@ public class Name implements Printable this.last = last; } + protected String getFullName(){ + return this.first + " " + this.last; + } + @Override public void display() { - System.out.println("First: " + first + " Last: " + last); + System.out.println(first + " " + last); } } diff --git a/src/code/ca/bcit/comp2522/lab04/Person.java b/src/code/ca/bcit/comp2522/lab04/Person.java index 1595b14..e3aa771 100644 --- a/src/code/ca/bcit/comp2522/lab04/Person.java +++ b/src/code/ca/bcit/comp2522/lab04/Person.java @@ -12,10 +12,13 @@ package ca.bcit.comp2522.lab04; * @version 1.0.0 */ public class Person + implements Comparable, + Printable, + Reversible { - private final Date dateOfBirth; - private final Date dateOfDeath; - private final Name name; + protected final Date dateOfBirth; + protected final Date dateOfDeath; + protected final Name name; /** * Person construct @@ -35,4 +38,46 @@ public class Person this.dateOfDeath = dateOfDeath; this.name = name; } + + public String getFullName(){ + return this.name.getFullName(); + } + + @Override + public void display() { + + StringBuilder builder; + builder = new StringBuilder(); + + builder.append("Date of Birth: "); + builder.append(this.dateOfBirth); + builder.append("\nDate of Death: "); + builder.append(this.dateOfDeath); + builder.append("\nName: "); + builder.append(this.name.getFullName()); + + System.out.println(builder.toString()); + } + + @Override + public void backward() + { + final StringBuilder builder; + final String fullName; + + builder = new StringBuilder(); + fullName = this.name.getFullName(); + + for (int i = fullName.length() - 1; i >= 0; i--) { + builder.append(fullName.charAt(i)); + } + + System.out.println(builder.toString()); + } + + + @Override + public int compareTo(Person other) { + return this.dateOfBirth.compareTo(other.dateOfBirth); + } }