//====================================================================
// Author: Bill Brooks      Date Revised: $Date: 1996/06/05 08:17:29 $
// Class Name: Customer     Version: $Revision: 1.7 $
// Filename: Customer.java
// Package: machine
// Purpose: This class models a customer entering
//          information into the PCM. 
//===================================================================
package com.nanosoft.pcm.machine;

import com.nanosoft.pcm.machine.*;
import java.util.StringTokenizer;
import java.util.Vector;

public class Customer extends Object {
    Vector shoppingList;
    MoneyAmount custCredit;
    String custItemSelection;
    boolean anotherPurchaseDecision;
    boolean custReceiptDecision;

   public Customer() {
      super();
      shoppingList = new Vector(4);
     
   }

   public
     boolean grabShoppingList(String piece) {
       boolean results = true;
       if ( !piece.startsWith("#") && piece != null ) {
           shoppingList.addElement(piece);
          }
       else 
         {
          results = false;          
         }
     return(results);
     }
  private
    void printShoppingList() {
       for (int i=0; i< shoppingList.size(); i++) {
           // run through the shopping list
           System.out.println( shoppingList.elementAt(i)); 
       } 

    }
  private
    void printStatus() {
         System.out.print("I inserted exactly ");
         System.out.println(custCredit);
         System.out.println("I choose item " + custItemSelection);
         if ( anotherPurchaseDecision )
            System.out.println("I DID wish to purchase another item "); 
         else
            System.out.println("I did not wish to purchase another item "); 
         if ( custReceiptDecision )
            System.out.println("I DID want to get a receipt "); 
         else
            System.out.println("I did not want a receipt "); 
         System.out.println("---------------------------------------");
       } 
  public
    void shop() {
           String aListLine;
           // set all the attributes
           for (int i=0; i < shoppingList.size(); i++) { 
                aListLine = (String)shoppingList.elementAt(i);
                decode(aListLine);
             }
           printStatus();    
           
         }
  public
    void decode(String anAction) {
            
         if ( anAction.startsWith("I") ) 
            {
              // how much money am I inserting?
              StringTokenizer theseTokens = new StringTokenizer(anAction);
              String custAction = theseTokens.nextToken();
              custCredit = new MoneyAmount(theseTokens.nextToken());
             }            
           else
             {
              Character aChar = new Character(anAction.charAt(0));
              if (aChar.isUpperCase(anAction.charAt(0)) )
                   {
                     // I am making a selection or deciding whether or not
                     //  to get a receipt                     
                    if ( anAction.compareTo("NO")==0 )
                       custReceiptDecision = false;
                    else
                      {
                       if (anAction.compareTo("YES")==0 )
                           custReceiptDecision = true;
                       else
                         {
                          // we are making a selection
                          custItemSelection = anAction; 
                         }
                      }
                   }
             }
    }

}