/** *
 *  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. * *

* @param args a String array of command line arguments. */ public static void main(String[] args) { //System.testclass.for.me //If Java is so smart, why doesn't it have that! //by the way, how's that commenting style I just thought of /*stuff*/ /*Begin Test Class*/ PostOffice po = new PostOffice(); //create test dummie...er...beta testers //game gods po.issue(new MailBox("Romero","John")); //hmm, two John's maybe po.issue(new MailBox("Carmack","John")); //I need a name change? po.issue(new MailBox("Sweeney","Tim")); po.issue(new MailBox("Miyamato","Shigeru")); //musical greatness po.issue(new MailBox("Beethoven","Ludwik von"));//hehe test StringToken too po.issue(new MailBox("Stravinsky","Igor")); po.issue(new MailBox("Reznor","Trent")); po.issue(new MailBox("Bowie","David")); //good anime characters po.issue(new MailBox("Starwind","Gene")); po.issue(new MailBox("Spiegel","Spike")); //assorted po.issue(new MailBox("McGee","Mike")); //that's Mike McGee of the IN po.issue(new MailBox("Dole","Bob")); //viva la Pfiezer! /*Test Exception*/ po.issue(new MailBox("Romero","John")); /*Test Retrieve*/ po.send(new Mail("You're career has hit rock bottom","Carmack","Romero")); po.send(new Mail("I mean games for the PalmPC!?","Sweeney","Romero")); /*Test Exception*/ if(po.send(new Mail("BOOOOOO","Bob","Romero")) == -1){ System.out.println("BAD_SENDER"); } //despite not receiving the mail, a response! if(po.send(new Mail("FU!","Romero","Bob")) == -2){ System.out.println("BAD_RECIPIENT"); } System.out.println(po.retrieve("Romero")); /*Test Remove*/ /*Test Exception*/ System.out.println(po.remove("Romreo")); //oops :) System.out.println(po.remove("Romero")); /*Nothing to test for resize() and allBoxes(), tested in HashTable*/ /*End Test Class*/ }// end of main(String[] args) }// end of class PostOffice