/* * LibraryMain.java * * Created on July 10, 2003, 11:47 AM */ package mid1Library; import java.util.ArrayList; import java.util.Iterator; /** Common interface for any kind of item owned by the library. */ interface ILibraryItem { /** Tells the title of the item. */ String getTitle(); /** Determines whether the item is overdue. * @return true if the item is checked out and is overdue; false otherwise. */ boolean isOverdue(); /** Tells the name of the borrower of the item. * @return the name of the borrower (patron) if the item is checked out; * returns null if the item is not checked out. * The name should exactly match the patron name as recorded in some IPatron object. */ String getBorrowerName(); } /** Iterface describing library patrons, who may borrow items from the library. */ interface IPatron { /** Tells the name of the patron. */ String getName(); /** Tells the address of the patron. * @return a string with the address in a multi-line format, suitable for * printing on a mailing label. */ String getAddress(); } /** Encapsulate the information needed to prepare and send an overdue notice * to the patron who has the item checked out. There is one notice per overdue * item, even if one patron has many such items. */ abstract class AOverdueNotice { /** Name of the patron who should receive the notice. */ protected String pName; /** Address of the patron who should receive the notice, in a multi-line form * suitable for printing on a mailing label. */ protected String pAddress; /** Title of the item which is overdue. */ protected String iName; /** Record the name of the patron who has the item checked out. The name * will be exactly in the same format as in a Library Item object. */ public abstract void setPatronName(String name); /** Record the address of the patron who should receive the notice, in a multi-line form suitable for printing on a mailing label. */ public abstract void setPatronAddress(String address); /** Record the title of the item which is overdue. */ public abstract void setItemName(String name); } /** * * @author dickey */ public class LibraryMain { /** Creates a new instance of LibraryMain */ public LibraryMain() { } /** Find the overdue items and prepare an overdue notice for each one. * @param allItems A list of all Library Items owned by the library. * @param allPatrons A list of all patrons who have library cards. * @return a list of OverdueNotice objects, one for each item which is * checked out and overdue. */ public ArrayList listOverdue(ArrayList allItems, IPatron[] allPatrons) { ArrayList notices = new ArrayList(); Iterator itemIter = allItems.iterator(); while (itemIter.hasNext()) { ILibraryItem anItem = (ILibraryItem) itemIter.next(); if (anItem.isOverdue()) { AOverdueNotice notice = new OverdueNotice(); notice.setItemName(anItem.getTitle()); String pName = anItem.getBorrowerName(); notice.setPatronName(pName); IPatron borrower = findPatron(allPatrons, pName); String pAddress; if (borrower == null) { pAddress = "unknown address"; } else { pAddress = borrower.getAddress(); } notice.setPatronAddress(pAddress); notices.add(notice); } } return notices; } /** Find and return the patron whose name is given. * An exact match is required. * @return the matching patron, or null if there is no match */ private IPatron findPatron(IPatron[] patrons, String patronName) { for (int p = 0; p < patrons.length; p++) { if (patrons[p].getName().equals(patronName)) { return patrons[p]; } } return null; } } class OverdueNotice extends AOverdueNotice { public void setPatronName(String name) { this.pName = name;} public void setPatronAddress(String address) { this.pAddress = address;} public void setItemName(String name) { this.iName = name;} public String toString() { return this.pName + "\n" + this.pAddress + "\n" + this.iName; } }