/*
	Programmmer: Joshua Howard
	Sales Commission
    Mar. 25, 2010
    This applet calculates sales commission using a sales amount, input by the user,
    and a sales code, chosen from checkbox buttons.
    */

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;

    public class CommissionApplet3 extends Applet implements ItemListener, ActionListener
    {

     //declare variables and construct a color

     double  dollars, answer, commission, sales;
     int     empCode;
     Image   dollarSign;
     Color darkRed = new Color(160, 50, 0);

     //declare (create) object components

     Label promptLabel = new Label("Enter the sales amount (do not use commas or dollar signs): ");
           TextField salesField = new TextField(20);

     Label codeLabel = new Label("Select the appropriate commission code: ");

     CheckboxGroup codeGroup = new CheckboxGroup();
          Checkbox telephoneBox = new Checkbox("Telephone Sales",false,codeGroup);
          Checkbox inStoreBox = new Checkbox("In-Store Sales",false,codeGroup);
          Checkbox outsideBox = new Checkbox("Outside Sales",false,codeGroup);
          Checkbox hiddenBox = new Checkbox("",true,codeGroup);

          Button calcButton = new Button("Calculate");

     Label outputLabel = new Label("");


     public void init( )
     {
       //set color scheme

       setBackground(darkRed);
       setForeground(Color.white);


      // add intro label and textfield for user to input total sales value into

       add(promptLabel);
       add(salesField);
       salesField.requestFocus();
       salesField.setForeground(Color.black);

      // add checkbox Group representing Sales Categories

      add(codeLabel);
       add(telephoneBox);
       telephoneBox.addItemListener(this);

       add(inStoreBox);
       inStoreBox.addItemListener(this);

       add(outsideBox);
       outsideBox.addItemListener(this);

		setForeground(Color.black);
		add(calcButton);
		calcButton.addActionListener(this);

       add(outputLabel);


      }

      // the itemStateChanged method is triggered by the user clicking a checkbox button


      public void itemStateChanged(ItemEvent choice)
      {

       		 sales = Double.parseDouble(salesField.getText());

       		 if(telephoneBox.getState())

			  commission = .10 * sales;


	          if(inStoreBox.getState())

	          commission = .14 * sales;


	          if(outsideBox.getState())

             commission = .18 * sales;


}
			public void actionPerformed(ActionEvent e)
			{


	          DecimalFormat twoDigits = new DecimalFormat("$ #,##0.00");
	   		  outputLabel.setText("Your commission on sales of " + twoDigits.format(sales) + " is " + twoDigits.format(commission));

	   }
}
