This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

  1. /*
  2. * SD2x Homework #8
  3. * This class represents a single book.
  4. * Please do not change this code! Your solution will be evaluated using this version of the class.
  5. */
  6. public class Book implements Comparable<Book> {
  7. @Override
  8. public String toString() {
  9. return "Book [title=" + title + ", author=" + author
  10. + ", publicationYear=" + publicationYear + "]";
  11. }
  12. protected String title;
  13. protected String author;
  14. protected int publicationYear;
  15. public Book(String title, String author, int year) {
  16. this.title = title;
  17. this.author = author;
  18. this.publicationYear = year;
  19. }
  20. public String getTitle() {
  21. return title;
  22. }
  23. public String getAuthor() {
  24. return author;
  25. }
  26. public int getPublicationYear() {
  27. return publicationYear;
  28. }
  29. @Override
  30. public boolean equals(Object obj) {
  31. if (this == obj)
  32. return true;
  33. if (obj == null)
  34. return false;
  35. if (getClass() != obj.getClass())
  36. return false;
  37. Book other = (Book) obj;
  38. if (author == null) {
  39. if (other.author != null)
  40. return false;
  41. } else if (!author.equals(other.author))
  42. return false;
  43. if (publicationYear != other.publicationYear)
  44. return false;
  45. if (title == null) {
  46. if (other.title != null)
  47. return false;
  48. } else if (!title.equals(other.title))
  49. return false;
  50. return true;
  51. }
  52. @Override
  53. public int compareTo(Book other) {
  54. return this.publicationYear - other.publicationYear;
  55. }
  56. @Override
  57. public int hashCode() {
  58. return this.author.hashCode();
  59. }
  60. }