// joi/4/estore/EStore.java
//
//
// Copyright 2003 Bill Campbell and Ethan Bolker
/**
* An EStore object simulates the behavior of a simple on line
* shopping web site.
*
* It contains a Terminal object to model the customer's browser
* and a Catalog of Items that may be purchased and
* then added to the customer's shoppingCart.
*
* @version 4
*/
public class EStore
{
private String storeName;
private Terminal browser;
private Catalog catalog;
/**
* Construct a new EStore.
*
* @param storeName the name of the EStore
* @param browser the visitor's Terminal.
*/
public EStore( String storeName, Terminal browser )
{
this.browser = browser;
this.storeName = storeName;
this.catalog = new Catalog();
catalog.addItem( new Item("quaffle", 55) );
catalog.addItem( new Item("bludger", 15) );
catalog.addItem( new Item("snitch", 1000) );
}
/**
* Visit this EStore.
*
* Execution starts here when the store opens for
* business. User can visit as a customer, act as
* the manager, or exit.
*/
public void visit()
{
// Print a friendly welcome message.
browser.println( "Welcome to " + storeName );
while (true) { // an infinite loop ...
browser.println();
String whoAreYou = browser.readWord(
storeName + " (manager, visit, exit): ");
if (whoAreYou.equals("exit")) {
break; // leave the while loop
}
if (whoAreYou.equals("manager")) {
managerVisit();
}
if (whoAreYou.equals("visit")) {
customerVisit();
}
}
}
/**
* Manager options:
*
* examine the catalog
* add an Item to the catalog
* quit
*/
private void managerVisit( )
{
while (true) {
String cmd =
browser.readWord("manager command (show, new, quit):");
if (cmd.equals("quit")) {
break; // leave manager command while loop
}
else if (cmd.equals("show")) {
catalog.show(browser);
}
else if (cmd.equals("new")) {
String itemName = browser.readWord(" item name: ");
int cost = browser.readInt( " cost: ");
catalog.addItem( new Item(itemName, cost) );
}
else {
browser.println("unknown manager command: " + cmd);
}
}
}
/**
* Customer visits this EStore.
*
* Loop allowing customer to select items to add to her
* shoppingCart.
*/
private void customerVisit( )
{
// Create a new, empty ShoppingCart.
ShoppingCart basket = new ShoppingCart();
browser.println( "Currently available:");
catalog.show(browser);
while ( true ) { // loop forever ...
String nextPurchase = browser.readWord(
"select your purchase, checkout, help: ");
if ( nextPurchase.equals("checkout" )) break; // leave loop!
if ( nextPurchase.equals("help" )) {
catalog.show(browser);
continue; // go back to top of while loop
}
// customer has entered the name of an Item
basket.addItem( catalog.getItem(nextPurchase) );
}
int numberPurchased = basket.getCount();
browser.println("We are shipping these " +
basket.getCount() + " Items:");
basket.showContents(browser);
browser.println("and charging your account $" + basket.getCost());
browser.println("Thank you for shopping at " + storeName);
}
/**
* The EStore simulation program begins here when the user
* issues the command java EStore
*
* If first command line argument is "-e" instantiate a
* Terminal that echoes its input.
*
* The next command line argument (if there is one)
* is the name of the EStore.
*
* @param args <-e>
*/
public static void main( String[] args )
{
String storeName = "Virtual Minimal Minimall"; //default
// check to see if first argument is "-e"
boolean echo = ( (args.length > 0) && (args[0].equals("-e")) );
// if first argument was "-e" then look at second for store name
int nextArg = (echo ? 1 : 0 );
if (args.length > nextArg) {
storeName = args[nextArg];
}
// Print this to simulate internet search.
System.out.println("connecting ...");
// Create an EStore object and visit it
(new EStore(storeName, new Terminal(echo))).visit();
}
}