30% done with the Main class

This commit is contained in:
nicoagostini committed 2025-10-04 00:15:09 -07:00
1 parent a86f0cc26d
commit 429e95f371
8 files changed
+333 -13

No files matched your search

+20 -1
View File
@@ -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());
}
}
@@ -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());
}
}
+50 -2
View File
@@ -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());
}
}
+47 -3
View File
@@ -12,14 +12,17 @@ package ca.bcit.comp2522.lab04;
* @version 1.0.0
*/
public class Book
implements Comparable<Book>,
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;
}
}
+28 -2
View File
@@ -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<Date>
{
// 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
}
}
+115
View File
@@ -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;
}
}
+5 -1
View File
@@ -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);
}
}
+48 -3
View File
@@ -12,10 +12,13 @@ package ca.bcit.comp2522.lab04;
* @version 1.0.0
*/
public class Person
implements Comparable<Person>,
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);
}
}