first commit
This commit is contained in:
368
CS1322/p3/Manager.java
Normal file
368
CS1322/p3/Manager.java
Normal file
@@ -0,0 +1,368 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Manager.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 11, 2002
|
||||
* Created the Manager class
|
||||
* 1.1 Oct. 16, 2002
|
||||
* Compiled, Commented, Finished
|
||||
* 1.2 Oct. 17, 2002
|
||||
* Corrected Last Name only being shown on to and from for mail
|
||||
* 1.3 Oct. 17, 2002
|
||||
* Corrected infinite loop if letter entered in CcustomerHere()
|
||||
* 2.0 Oct. 17, 2002
|
||||
* Added some comments to make what I did clearer as well
|
||||
* as made certain aspects of the code "better"
|
||||
* 2.1 Oct. 18, 2002
|
||||
* Fixed a Comment in readText()
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* 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 <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 2.1, Oct. 18, 2002
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Manager implements P3Type{
|
||||
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
//declaration of globals
|
||||
private String strCustFName ;
|
||||
private String strCustLName ;
|
||||
private PostOffice objPostOffice = new PostOffice();
|
||||
|
||||
private BufferedReader KB_IN;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
/*
|
||||
This constructor would have been used if readFile() wasn't run on every
|
||||
run of managerActions(). Having the constructor read the file and then
|
||||
waiting to write it after the program is done would mean any mailbox
|
||||
deleted from the PostOffice would not be removed from the file and in
|
||||
case of system failure, when the RAM is flushed the deleted mailboxes would
|
||||
never actually be deleted.
|
||||
|
||||
public Manager(){
|
||||
readFile();
|
||||
}
|
||||
*/
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Initialize the Keyboard input
|
||||
*/
|
||||
private void initInput(){
|
||||
KB_IN = new BufferedReader(new InputStreamReader(System.in));
|
||||
}
|
||||
|
||||
/**
|
||||
*Read contents of MyPO
|
||||
*/
|
||||
private void readFile(){
|
||||
FileChanger fc = new FileChanger(P3Type.POST_OFFICE);
|
||||
boolean truth = fc.initReader();
|
||||
Object temp;
|
||||
|
||||
if(truth != false){
|
||||
do{
|
||||
temp = fc.nextObject();
|
||||
if(temp != null){
|
||||
objPostOffice.issue((MailBox)temp);
|
||||
}
|
||||
}while(temp != null);
|
||||
fc.closeRead();
|
||||
}
|
||||
|
||||
}//end readFile()
|
||||
|
||||
/**
|
||||
*Read Input
|
||||
*/
|
||||
private Object[] readText(String KB_INPUT,int KB_INPUT_PARSE){
|
||||
boolean trust = true;
|
||||
|
||||
//do..while() for purposes of keeping the code running until\
|
||||
//an acceptable value is entered
|
||||
do{
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException in readText()");
|
||||
}
|
||||
|
||||
try{
|
||||
KB_INPUT_PARSE = Integer.parseInt(KB_INPUT);
|
||||
trust = true;
|
||||
}
|
||||
catch(NumberFormatException nfe){
|
||||
System.err.println("Please enter a number next time");
|
||||
trust = false;
|
||||
}
|
||||
}while(trust != true);
|
||||
|
||||
//set-up return array
|
||||
Object[] toReturn = new Object[2];
|
||||
toReturn[0] = KB_INPUT;
|
||||
toReturn[1] = new Integer(KB_INPUT_PARSE);
|
||||
return toReturn;
|
||||
}//end readText(String,int)
|
||||
|
||||
/**
|
||||
*Perform actions as the manager
|
||||
*@return false if done
|
||||
*/
|
||||
public boolean managerActions(){
|
||||
|
||||
//A Note: if the readFile() function/code is run everytime
|
||||
//managerActions() is run, then the file must be rewritten everytime
|
||||
//a mailbox is deleted. This is good and bad, good for performance
|
||||
//bad in case of a crash
|
||||
//I opted for the lesser performance but better reliability
|
||||
//we don't want the mailboxes people deleted reappearing in case of
|
||||
//system failure do we?
|
||||
readFile();
|
||||
|
||||
System.out.println("\n"+P3Type.INIT_ACTIONS);
|
||||
|
||||
//let's keep this archane Hungarian Style Notation, shall we?
|
||||
String KB_INPUT = "";
|
||||
int KB_INPUT_PARSE = 0;
|
||||
initInput(); //I also moved this for cleanliness
|
||||
|
||||
//In order to lessen the clutter of this code, I created a separate
|
||||
//function to handle the text input
|
||||
Object[] temp = readText(KB_INPUT,KB_INPUT_PARSE);
|
||||
KB_INPUT = (String)temp[0];
|
||||
Integer tucow = (Integer)temp[1];
|
||||
KB_INPUT_PARSE = tucow.intValue();
|
||||
|
||||
|
||||
switch(KB_INPUT_PARSE){
|
||||
case 1:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
boolean trueness = true;
|
||||
do{
|
||||
System.out.println("Please enter the Customer's name");
|
||||
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException in managerAction()"+
|
||||
" when trying to readLine().");
|
||||
}
|
||||
|
||||
trueness = customerHere(KB_INPUT);
|
||||
}while(trueness);
|
||||
break;
|
||||
//end case 1
|
||||
case 2:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.print("Adding Rooms: ");
|
||||
objPostOffice.resize();
|
||||
System.out.println("Done.");
|
||||
break;
|
||||
//end case 2
|
||||
case 3:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
System.out.println("The current boxes are: ");
|
||||
Enumeration e = objPostOffice.allBoxes();
|
||||
if(e != null){
|
||||
while(e.hasMoreElements()){
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
}
|
||||
break;
|
||||
//end case 3
|
||||
case 0:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
objPostOffice.leavingForTheDay();
|
||||
return false;
|
||||
//end case 0
|
||||
|
||||
default:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println("You didn't input 0-3 did you?");
|
||||
return managerActions(); //restart
|
||||
//end default
|
||||
}
|
||||
return true;
|
||||
}//end managerActions()
|
||||
|
||||
/**
|
||||
*Customer Actions
|
||||
*@return false when done
|
||||
*/
|
||||
public boolean customerHere(String strName){
|
||||
for(;;){
|
||||
System.out.println("\n"+P3Type.CUST_HERE);
|
||||
|
||||
initInput();
|
||||
|
||||
String KB_INPUT = "";
|
||||
int KB_INPUT_PARSE = 0;
|
||||
|
||||
//In order to lessen the clutter of this code, I created a separate
|
||||
//function to handle the text input
|
||||
Object[] temp = readText(KB_INPUT,KB_INPUT_PARSE);
|
||||
KB_INPUT = (String)temp[0];
|
||||
Integer tucow = (Integer)temp[1];
|
||||
KB_INPUT_PARSE = tucow.intValue();
|
||||
|
||||
/*get names*/
|
||||
StringTokenizer st = new StringTokenizer(strName);
|
||||
strCustFName = st.nextToken();
|
||||
|
||||
while(st.hasMoreTokens()){
|
||||
strCustLName = st.nextToken();
|
||||
}
|
||||
/////////////
|
||||
|
||||
switch(KB_INPUT_PARSE){
|
||||
case 1:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
MailBox objNewBox = new MailBox(strCustLName,strCustFName);
|
||||
objPostOffice.issue(objNewBox);
|
||||
objPostOffice.leavingForTheDay(); //rewrites mailbox list
|
||||
break;
|
||||
//end case 1
|
||||
case 2:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
int iValue = objPostOffice.remove(strCustLName);
|
||||
if(iValue == -3){
|
||||
System.out.println(P3Type.NO_MAILBOX);
|
||||
}
|
||||
else{ //if(iValue >= 0)
|
||||
System.out.println(P3Type.NUM_TELEGRAMS + iValue);
|
||||
objPostOffice.leavingForTheDay(); //rewrites mailbox list
|
||||
}
|
||||
break;
|
||||
//end case 2
|
||||
case 3:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
String LName = "";
|
||||
String FName = "";
|
||||
String message ="";
|
||||
int iVal;
|
||||
|
||||
System.out.println("Who do you want to send the message to?");
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException READLine() in case3"+
|
||||
"@ customerHere()");
|
||||
}
|
||||
|
||||
st = new StringTokenizer(KB_INPUT);
|
||||
FName = st.nextToken();
|
||||
while(st.hasMoreTokens()){
|
||||
LName = st.nextToken();
|
||||
}
|
||||
|
||||
System.out.println("What is the message?");
|
||||
|
||||
try{
|
||||
message = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("Error in case3 readLine() message" +
|
||||
"@ customerHere()");
|
||||
}
|
||||
|
||||
Mail objNewMail = new Mail(message,strCustFName+
|
||||
" "+strCustLName,FName+" "+LName);
|
||||
iVal = objPostOffice.send(objNewMail);
|
||||
|
||||
if(iVal == P3Type.BAD_SENDER){
|
||||
System.out.println("You need to open a mailbox " +
|
||||
"with us first.");
|
||||
}
|
||||
else if(iVal == P3Type.NO_RECIPIENT){
|
||||
System.out.println("The person you are trying " +
|
||||
"to send to doesn't have a " +
|
||||
"mailbox here.");
|
||||
}
|
||||
else {//if(message.equals(P3Type.MAIL_SENT))
|
||||
System.out.println("Mail sent successfully, please allow" +
|
||||
" 6 to 8 weeks for delivery.");
|
||||
}
|
||||
break;
|
||||
//end case 3
|
||||
case 4:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println(objPostOffice.retrieve(strCustLName));
|
||||
break;
|
||||
case 0:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
return false;
|
||||
default:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println("Didn't enter 0-4, try again");
|
||||
return customerHere(strName); //restart
|
||||
}//end switch
|
||||
}//end for(;;)
|
||||
}//end customerHere(String)
|
||||
|
||||
/**
|
||||
* Debugging main for class Manager.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Manager Bob = new Manager();
|
||||
Bob.managerActions();
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Manager
|
||||
Reference in New Issue
Block a user