/*
// $Author: bagonzal $
// $Date: 1996/05/27 04:08:55 $
// $Source: /home/phoenix/cscstd/ijkl/jlarsen/box/RCS/Keypad.java,v $
// $Revision: 1.3 $
// $Log: Keypad.java,v $
# Revision 1.3  1996/05/27  04:08:55  bagonzal
# Changed checkValidInput to return type of boolean.  Also modified the constructor to initialize local variables with references to classes.
#
# Revision 1.2  1996/05/24  04:03:29  bagonzal
# Changed the one letter variables to descriptive variables.
#
# Revision 1.1  1996/05/18  04:44:35  bagonzal
# Initial revision
#
*/
package com.nanosoft.pcm.machine;

import com.nanosoft.pcm.machine.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class Keypad {
   String input;
   Customer customer;
   CurrencyAcceptor currAccptr;
   ReportGenerator reportGen;
   MainDisplay mainDisplay;
   StorageUnit storageUnit;

   // Constructor 
   public Keypad(Customer myCustomer, CurrencyAcceptor myCurrAccptr, 
	ReportGenerator myReportGen, MainDisplay myMainDisplay, StorageUnit 
	myStorageUnit) {
      customer = myCustomer;
      currAccptr = myCurrAccptr;
      reportGen = myReportGen;
      mainDisplay = myMainDisplay;
      storageUnit = myStorageUnit;
   }

   // Public methods

   public String getItemSelection() {
      return input;
   }

   public void getAnotherSelection() {
      mainDisplay.promptForDecision();

      if (!customer.decideOnPurchase()) {
         mainDisplay.promptForReceipt();
         getReceiptDecision();
      }
   }

   public void getReceiptDecision() {
      mainDisplay.promptForReceipt();

      if(customer.decideOnReceipt())
         reportGen.printReceipt();
      else
         reportGen.clearLog();
   }

   public void itemSelection(String aSelection) {
      input = aSelection;
      if (checkValidInput(input)) {
         mainDisplay.displaySelection(input);
	 if (currAccptr.completeTransaction(input))
	    getAnotherSelection();
	 else
	    getReceiptDecision();
      }
   }    

   // Private methods

   private boolean checkValidInput() {
      if (storageUnit.isValidSelection(input)) {
 	 if (storageUnit.isInStock(input))
 	    return true;
         else {
            mainDisplay.displayErrorMessage("Out of stock");
	    return false;
	 }
      }
      else {
	 mainDisplay.displaySelectionError();
         return false;
      }
   }

}