Question: In this method, i am trying to return a list of books made by a certain author. At the moment it doesnt work and always
In this method, i am trying to return a list of books made by a certain author. At the moment it doesnt work and always returns 0.
public ArrayList
Here are the rest of the classes on the project.
public class Database { private ArrayList
/** * Create a new database */ public Database() { publicationList = new ArrayList
/** * Add a publication * * @param publication The publication to be added */ public void addPublication(Publication publication) { if (publicationList.contains(publication)) { System.out.println("This publication has already been added to the list: " + publication); } else { publicationList.add(publication); } }
/** * Get the total number of publications * * @return The total number of publications */ public int getTotal() { return publicationList.size(); }
public ArrayList
public int getNumberOfJournals(int month, int year) { int count = 0; for (Publication publication : publicationList) { if (publication instanceof Journal) { Journal journal = (Journal) publication; if (journal.getMonth() == month && journal.getYear() == year) { count++; } } } return count; }
public void printList() { // Sort publications by category, then by title Collections.sort(publicationList, new Comparator
// Print the sorted publications for (var aPublication : publicationList) { System.out.println(aPublication); } System.out.println("Total number of publications: " + getTotal()); } }
public class Journal extends Publication { // The year and month when the journal was published private int month;
/** * Create a journal. * * @param title The title of the journal * @param month The month when the journal was published * @param year The year when the journal was published */ public Journal(String title, int month, int year) { super(title, year); this.month = month; }
/** * Get the month when the journal was published * * @return The month when the journal was published */ public int getMonth() { return month; } /** * Get the details of the journal * * @return The details of the journal, i.e. the title, year and month */ public String toString() { return super.toString() + " (" + getMonthName(month) + ")"; } /** * Check if the journal is the same as the given one. * * @param obj The given object. * * @return true if the journal and the given one have the * same title, year and month; * false otherwise */ public boolean equals(Object obj) { if (this == obj) return true; if ( !(obj instanceof Journal) ) return false; var another = (Journal) obj; return this.getTitle().equals(another.getTitle()) && this.month == another.month && this.getYear() == another.getYear(); } /** * To get the name of a given month * * @param month A given month * @return The month's name */ private String getMonthName(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; default: return "Unknown"; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
