FileDocCategorySizeDatePackage
Supermarket2.javaAPI DocExample6582Wed Nov 14 17:00:54 GMT 2001None

Supermarket2.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import java.util.*;

public class Supermarket2 extends Applet
{
  private Controller controlObj;//declaration part
	private Panel marketPan, conPan, tillPan;
  
	public Supermarket2( )
	{
		//constructor of Supermarket2 applet
	}

	public void init( )//set up the supermarket system
	{
    marketPan = new Panel( );
  	marketPan.setLayout(new GridLayout(2,1));//two rows, one column
  	add(marketPan); //attach to applet surface

  	conPan = new Panel( );//panel to diplay controller
  	conPan.setBackground(new Color(175,175,175));
  	marketPan.add(conPan); //display on market panel

 

		controlObj = new Controller(conPan);
		
    Till till1Obj = new Till(controlObj,"Denise");
    add(till1Obj.getTillPanel());

    Till till2Obj = new Till(controlObj,"Spotty Bob");
    add(till2Obj.getTillPanel());

  }


	class Controller
	{
    private int tillCount = 0; //NEW
    private String[] prodName = {"McEwan's Export", "Vindaloo", "Chart CD", "Brown Underpants"};
    private double[] prodPrice = {3.99, 2.50, 11.99, 1.99};
    private String[] barCodes = {"501", "502", "503", "504"};//not used
    private final int MAX_TILLS = 10;//NEW

    private Till[] tillObjArr = new Till[MAX_TILLS];//NEW
    private Panel[] tillPanArr = new Panel[MAX_TILLS]; //NEW
    private Label[] tillIDLabArr = new Label[MAX_TILLS];// NEW
    private TextField[] userTFArr = new TextField[MAX_TILLS];//NEW
    private TextField[] totalTFArr = new TextField[MAX_TILLS];//NEW
    private Checkbox[] indCBArr = new Checkbox[MAX_TILLS];//NEW
    

    private double totalCost = 0.00;

    private final int MAX_PRODUCTS = prodName.length;


		public Controller(Panel conPanel)//changed
		{
      conPan = conPanel;
			//Controller's constructor
      conPan.setLayout(new GridLayout(3,4));
	    
	    conPan.setVisible(true);
      showStatus("Initialised Controller");
		}

    public int registerTill(Till tObj, String checkoutPerson)//changed
    {
      showStatus("Initialising till...");
      tillObjArr[tillCount] = tObj; //reference available throughout class
      tillPanArr[tillCount] = new Panel();//NEW
      userTFArr[tillCount] = new TextField(checkoutPerson);//NEW
      conPan.add(userTFArr[tillCount]);//NEW
      totalTFArr[tillCount] = new TextField(); //NEW
      conPan.add(totalTFArr[tillCount]); //NEW
      indCBArr[tillCount] = new Checkbox("*",false);//NEW
      conPan.add(indCBArr[tillCount]);//NEW
      return tillCount++;//NEW
    }

    public void newCustomer(int till) //changed
    {
	    tillObjArr[till].resetTill( ); //NEW
    	indCBArr[till].setState(true);  //NEW
    }

    public void endTransaction(int till, double cost)//changed
    {
	    totalCost = totalCost + cost;
	    totalTFArr[till].setText("£"+totalCost);//NEW
	    indCBArr[till].setState(false);//changed
    }

    public double getPrice(int productCode)
    {
	    if (productCode >=  MAX_PRODUCTS)
	    {
		    return -1;
	    }
      else
      {
        return prodPrice[productCode];
	    }
    }

    public String getProduct(int productCode)
    {
      if (productCode >=  MAX_PRODUCTS)
	    {
		    return "";
	    }
      else
      {
        return prodName[productCode];
	    }
    }
	}

	class Till implements ActionListener
	{
    private int id; //NEW
    private Label nameLab, prodCodeLab, priceLab;
    private TextField prodNameTF, prodCodeTF, priceTF;
    private Button tillBut;
    private boolean tillStatus = false;
    private double totalCost;
    private Panel tillPan = new Panel();  //NEW

		public Till(Controller tillControl, String name )
		{
      //Till's constructor
      nameLab = new Label("Product Name :");
      tillPan.add(nameLab);

      prodNameTF = new TextField(12);
      prodNameTF.setText("");
      tillPan.add(prodNameTF);

      prodCodeLab = new Label("Product Code :");
      tillPan.add(prodCodeLab);

      prodCodeTF = new TextField(12);
      prodCodeTF.setText("");
      prodCodeTF.addActionListener(this);
      tillPan.add(prodCodeTF);

      priceLab = new Label("Price :");
      tillPan.add(priceLab);

      priceTF = new TextField(12);
      priceTF.setText("");
      tillPan.add(priceTF);

      tillBut = new Button("New");//new customer
      tillBut.addActionListener(this);
      tillPan.add(tillBut);

      id = controlObj.registerTill(this, name);//register this till
		}

    public Panel getTillPanel()  //NEW
    {
      return tillPan;
    }

    public void resetTill()
    {
	    prodNameTF.setText("88888888");
    	prodCodeTF.setText("88888888");
    	priceTF.setText("88888888");
    	waitaMo( );
      prodNameTF.setText("");
	    prodCodeTF.setText("");
	    priceTF.setText("£00.00");
	    totalCost = 0;
    }

    public void waitaMo( )
    {
	    long nowTime = System.currentTimeMillis( );
	    long t = 0;
	    do
	    {
		    t = t + 1; //or, do nothing at all and save a bit of typing
	    } while (System.currentTimeMillis( ) < (nowTime + 1000));
    }


    public void actionPerformed(ActionEvent ae)
    {
      String pCode;
      int prodNo;
      double prodPrice;
      String prodName;

	    if (ae.getSource() == tillBut)
    	{
        if (tillStatus == false)//till currently not processing a customer

        {//then begin processing one
          controlObj.newCustomer(id); //new customer.NEW
          // It must either be a new customer, or the
          // current customer wanting the bill (this
          // is a dual-purpose button)
        tillBut.setLabel("Total");
	      }
        else
        {
          controlObj.endTransaction(id,totalCost);//NEW
          priceTF.setText(""+totalCost);
          tillBut.setLabel("New");
        }
        tillStatus =! tillStatus;
      }

      if (ae.getSource() == prodCodeTF && tillStatus == true)
	    {
        // it must be another product to be added to the bill
		    pCode = prodCodeTF.getText();
		    prodNo = Integer.parseInt(pCode);
		    prodPrice = controlObj.getPrice(prodNo);
	    	prodName = controlObj.getProduct(prodNo);
		    totalCost = totalCost + prodPrice;
		    prodCodeTF.setText("");
		    if (prodPrice < 1)
		    {
			    prodNameTF.setText("Invalid");
		    }
	    	else
		    {
			    priceTF.setText("£"+prodPrice);
			    prodNameTF.setText(prodName);
		    }
	    }
    }
	}
}