/** *
* PostOffice.java * * Revisions: 1.0 Oct. 11, 2002 * Created the PostOffice class * 1.1 Oct. 11, 2002 * Compiled, Finished, Commented * 2.0 Oct. 16, 2002 * Added Phase6 methods * ** * Collaboration Statement: * I worked on the homework assignment alone, using only * course materials. * * Created with JCreatorLE, some indents are off when viewed through notepad * or EMACS * * @author Jose Manuel Caban * @version Version 2.0, Oct. 16, 2002 */ import java.util.*; public class PostOffice implements P3Type{ public static final boolean bDebug = false; //nothing to debug via var tho HashTable htExpress = new HashTable(10); /////////// //Methods// /////////// /** *Issue a mailbox with key as last name *@param mbToAdd, the mailbox to be added */ public void issue(MailBox mbToAdd){ if(htExpress.get(mbToAdd.getStrLName()) == null){ //avoid duplicate htExpress.add(mbToAdd.getStrLName(),mbToAdd); } } /** *Return the mail in a given persons box *@param strLName, the last name of the person */ public String retrieve(String strLName){ if(htExpress.get(strLName) == null){ return P3Type.NO_MAILBOX; } MailBox mb = (MailBox)htExpress.get(strLName); Enumeration e = mb.getMail(); String strLong = ""; int count = 1; //for flair while(e.hasMoreElements()){ strLong += "\nMessage " + count + ":\n" + e.nextElement().toString() + "\n"; count++; } return strLong; } /** *Send mail *@param toSend, the message to be sent */ public int send(Mail toSend){ String strFromLName = ""; String strToLName = ""; StringTokenizer st; //verify sender st = new StringTokenizer(toSend.getStrFrom()); while(st.hasMoreTokens()){ strFromLName = st.nextToken(); } if(htExpress.get(strFromLName) == null){ return P3Type.BAD_SENDER; } //verify recipient st = new StringTokenizer(toSend.getStrTo()); while(st.hasMoreTokens()){ strToLName = st.nextToken(); } if(htExpress.get(strToLName) == null){ return P3Type.NO_RECIPIENT; } //add the mail MailBox mb = (MailBox)htExpress.get(strToLName); mb.addMail(toSend); return P3Type.MAIL_SENT; }//end send(Mail) /** *@param strLName, to be removed from PostOffice *@return the number of unread messages in the box */ public int remove(String strLName){ if(htExpress.get(strLName) == null){ return P3Type.NO_MAILBOX_NUM; } int count = 0; MailBox mTemp = (MailBox)htExpress.remove(strLName); Enumeration e = mTemp.getMail(); //only because it looks cooler AND takes less typing for(;e.hasMoreElements();count++,e.nextElement()); return count; }//end remove(String) /** *@return enumeration containing the mailboxes */ public Enumeration allBoxes(){ //this is a tough one //took 4 of the 5 hours of p3phase5 return htExpress.elements(); } /** *Resize the HashTable *Doubles current size */ public void resize(){ htExpress.rehash(htExpress.size() * 2); } /* */ /*Phase 6*/ /* */ /** *Write mailboxes to file and close program */ public void leavingForTheDay(){ FileChanger fc = new FileChanger(P3Type.POST_OFFICE); fc.initWriter(); Enumeration e = htExpress.elements(); while(e.hasMoreElements()){ fc.writeObject(e.nextElement()); } fc.closeWrite(); } /**********************************************************/ /** * Debugging main for class PostOffice. * This method will rigorously test my code. * *