//=============================================================
//  PROJECT:  Income tax form
//  FILE:  IncomeTax.java
//  PURPOSE:  Design an income tax form from a paycheck stub          
//  VERSION:  1.1
//  TARGET:  Java v1.1 and above
//  UPDATE HISTORY:  1.1 10/22/00
//  PROGRAMMER:  Philip N. Geissler                            
//=============================================================

//------------------------- NOTES -----------------------------
/**
  Design an income tax form from a paycheck stub using three 
  panels and a textfield within a Border Layout format.
    1. EmployeePanel(NORTH) - employer's header title
    2. EarningsPanel(WEST) - employee's gross earnings
    3. DeductionsPanel(EAST) - employee's deductions
    4. instructions<TextField>(SOUTH) - employee's personal data
*/

//------------------------ IMPORTS ---------------------------

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

//==================== IncomeTax CLASS =======================
/** 
  This class simulates an income tax form from a paycheck 
  stub.  It contains Panels, TextFields and  one Button, 
  "Calculate".  The employee information is calculated by the 
  push of the "Calculate" button
*/

public class IncomeTax extends Applet
{
  //----------------------- DATA ------------------------------
  private EarningsPanel earnings;     
  private DeductionsPanel deductions; 
  private EmployeePanel employee;
  private TextField instructions;
  
  private TextField payRate,  //input for employee's hourly rate
                     regHrs,  //input for employees's regular hours                     
                      otHrs;  //input for employee's overtime hours
 	                      
 	private Label     regWgs,   //employee's regular wages label
 	                   otWgs,   //employee's overtime wages label
 	                  grsTot,   //employee's gross wages label
 	                    ssTx,   //Social Security tax label 
 	                   fedTx,   //Federal tax label
                   	  paTx,   //PA tax label
                     medTx,  //Medicare tax label
                    bethTx,  //Bethlehem tax label
                    netTot;  //Net Total after taxes label
                    
                    
 
  private double rate,     //regular hours worked in pay period
                 regHours, //overtime hours worked in pay period
 	               otHours,  //wages employee earns in pay period
 	               total,    //total of employees hours and wages
 	               totals,   //total of employees pay after taxes
 	               regwgs,   //regular wages in a pay period
 	               otwgs;    //overtime wages in a pay period
 	               
 	private final static double sstx   = 0.06,  //Social security tax
                              fedtx  = 0.06,  //Federal tax
                              patx   = 0.02,  //PA tax
                              medtx  = 0.01,  //Medicare tax
                              bethtx = 0.01; 	//Bethlehem tax                                           
 	               
 	private Listener myListener = new Listener(this);
 	
 	//---------------------- METHODS --------------------------
 	/**
 	   Set Layout formats for the four components of class 
 	   IncomeTax
  */
 	 public void init()
 	 {
 	   //class IncomeTax's parameters 
 	   setLayout(new BorderLayout());
     Color backColor = new Color(81,201,236);
     setBackground(backColor);
     setForeground(Color.black);
     
     //Place EarningsPanel in the West part of the Applet 
     earnings = new EarningsPanel(myListener);
     add(BorderLayout.WEST, earnings);
     
     //Place DeductionsPanel in the East part of the Applet
     deductions = new DeductionsPanel(myListener);      
     add(BorderLayout.EAST, deductions);
     
     //Place EmployeePanel in the North part of the Applet
     employee = new EmployeePanel();
     add(BorderLayout.NORTH, employee);
     
     //Place TextField "instructions" in the South Part of 
     //the Applet and set it's parameters
     instructions = new TextField();
     add(BorderLayout.SOUTH, instructions);
     instructions.setBackground(Color.white);
     instructions.setForeground(Color.red);
     instructions.setFont(new Font("SansSerif", Font.BOLD, 12));
     instructions.setText("Please enter your hourly rate and hours...then press 'Calculate'");
   } 
   
   /**
     Paint "MORAVIAN" in the center of the Applet
   */   
   public void paint(Graphics g)
   {
     //Dimension variables   
     Dimension d = getSize();
     int xGo = d.width / 2 - 15;
     int yGo = d.height / 2 - 70;
     final int GAP = 21;
     
     //Position vertically in the center panel of the Applet
     g.setFont(new Font("TimesRoman", Font.BOLD, 18));
     Color letterColor = new Color(0,0,128);
     g.setColor(letterColor);
     g.drawString("M", xGo, yGo);
     g.drawString("O", xGo, yGo + GAP);
     g.drawString("R", xGo, yGo +(GAP*2));
     g.drawString("A", xGo, yGo +(GAP*3));
     g.drawString("V", xGo, yGo +(GAP*4));
     g.drawString("I", xGo, yGo +(GAP*5));
     g.drawString("A", xGo, yGo +(GAP*6));
     g.drawString("N", xGo, yGo +(GAP*7));
   }
   
   //--------------------- Button Method --------------------
   /**
     Get users input, calculate the employee's total wages and
     income and display the results
   */       
   public void processCalcButton()
   { 
     //users enter these values
     rate = earnings.getpayRate();
     regHours = earnings.getregHrs();
     otHours = earnings.getotHrs();
                
     //calculations of gross wages and net wages
     total = (rate * regHours) + ((rate + (rate * 0.50)) * otHours);
     totals = total - ((sstx + fedtx + patx + medtx + bethtx) * total);
     
     //EarningsPanel variables for calculations and display
     earnings.showRegWgs(rate * regHours);
     earnings.showOtWgs((rate + (rate * 0.50)) * otHours);
     earnings.showGrsTot(total);
     
     //DeductionsPanel variables for calculations and display
     deductions.showSsTx(sstx * total);
     deductions.showFedTx(fedtx * total);
     deductions.showPaTx(patx * total);
     deductions.showMedTx(medtx * total);
     deductions.showBethTx(bethtx * total);
     deductions.showNetTot(totals);
     
     //TextField "instructions" display after calculations 
     instructions.setForeground(Color.blue);
     instructions.setText("Thank you for your service at Moravian College!!!");
   } 
}  
   
//=================== EarningsPanel CLASS ===================
/**
  This Panel class initializes 3 TextFields for users input and 9
  Labels for output of text and new values after calculations
*/
class EarningsPanel extends Panel
{
  //The Button that starts the calculation process
  private Button bCalc = new Button("Calculate");
  
  //displays textfields where user's input begins
  private TextField payRate = new TextField("0.00");
  private TextField regHrs  = new TextField("0.00");
  private TextField otHrs   = new TextField("0.00");
  
  //labels for output of text and calculated input values
  private Label regWgs       = new Label("");
 	private Label otWgs        = new Label(""); 
 	private Label grsTot       = new Label("");
 	private Label payRateLabel = new Label("Hourly Rate");
 	private Label regHrsLabel  = new Label("Regular Hours");  
 	private Label  otHrsLabel  = new Label("Overtime Hours"); 
 	private Label  regWgsLabel = new Label("Regular Wages");
 	private Label  otWgsLabel  = new Label("Overtime Wages");
 	private Label  grsTotLabel = new Label("Gross Wages");       	        
  
  //set parameters and add textfields and labels 
  public EarningsPanel(Listener theListener)
  {
    bCalc.addActionListener(theListener);
    
    Color backColor = new Color(81,201,236);  
    setBackground(backColor);
    setForeground(Color.black);
        
    setLayout(new GridLayout(8,1));
    
    add(payRateLabel);
    add(payRate);
    add(regHrsLabel);
    add(regHrs);
    add(otHrsLabel);
    add(otHrs);
    add(regWgsLabel);
    add(regWgs);
    add(otWgsLabel);
    add(otWgs);
    add(grsTotLabel);
    add(grsTot);
    add(bCalc);
  }
  
    /**
      EarningsPanels methods for receiving input and 
      displaying the calculated results
    */
    public double getpayRate()
    {
      return new Double(payRate.getText()).doubleValue();
    }
    
    public double getregHrs()
    {
      return new Double(regHrs.getText()).doubleValue();
    }
    
    public double getotHrs()
    {
      return new Double(otHrs.getText()).doubleValue();
    }
    
    public void showRegWgs(double wages)
    {
      regWgs.setText(String.valueOf(Math.ceil(wages)));
    }
    
    public void showOtWgs(double wages)
    {
      otWgs.setText(String.valueOf(Math.ceil(wages)));
    }
    
    public void showGrsTot(double wages)
    {
      grsTot.setText(String.valueOf(Math.ceil(wages)));
    }  
}   

//=================== DeductionsPanel CLASS =================
/**
  This Panel class initializes 6 labels that have a returned
  double value and 6 labels that represent the textfield 
  values for the previous labels
*/
class DeductionsPanel extends Panel
{ 
  private Label ssTx   = new Label("");
 	private Label fedTx  = new Label("");  
 	private Label paTx   = new Label("");
  private Label medTx  = new Label("");
  private Label bethTx = new Label("");
  private Label netTot = new Label("");
        	          
 	private Label   ssTxLabel   = new Label("Social Security Tax");
 	private Label   fedTxLabel  = new Label("Federal Tax"); 	
 	private Label   paTxLabel   = new Label("PA Tax");
 	private Label   medTxLabel  = new Label("Medicare Tax");
	private Label  bethTxLabel  = new Label("Bethlehem Tax");
 	private Label   netTotLabel = new Label("Net Total");
  
  //set parameters and add labels
  public DeductionsPanel(Listener theListener)
  {
    Color backColor = new Color(81,201,236);
    setBackground(backColor);
    setForeground(Color.black);
    
    setLayout(new GridLayout(6,1));
        
    add(ssTxLabel);
    add(ssTx);
    add(fedTxLabel);
    add(fedTx);
    add(paTxLabel);
    add(paTx);
    add(medTxLabel);
    add(medTx);
    add(bethTxLabel);
    add(bethTx);
    add(netTotLabel);
    add(netTot);
  }
  
    /**
      DeductionsPanel's methods for receiving input and 
      displaying the calculated results
    */
    
    public void showSsTx(double ded)
    {
      ssTx.setText(String.valueOf(Math.ceil(ded)));
    }
    
    public void showFedTx(double ded)
    {
      fedTx.setText(String.valueOf(Math.ceil(ded)));
    }
    
    public void showPaTx(double ded)
    {
      paTx.setText(String.valueOf(Math.ceil(ded)));
    }
    
    public void showMedTx(double ded)
    {
      medTx.setText(String.valueOf(Math.ceil(ded)));
    }
    
    public void showBethTx(double ded)
    {
      bethTx.setText(String.valueOf(Math.ceil(ded)));
    }
    
    public void showNetTot(double ded)
    {
      netTot.setText(String.valueOf(Math.ceil(ded)));
    }
}

//================== EmployeePanel CLASS ====================
/**
  This Panel consists of three Textfields and three Labels 
  relating to the employee's personal information.  This 
  panel is not interactive - it is just for show.
*/

class EmployeePanel extends Panel
{
  private TextField date = new TextField();
  private TextField name = new TextField();
 	private TextField  ssn = new TextField();
 	 	        	           
 	private Label  dateLabel = new Label("Date:");
 	private Label  nameLabel = new Label("Name:");
 	private Label  ssnLabel  = new Label("SSN:");        	           
  
  //set parameters and add textfields and labels
  public EmployeePanel()
  {
    Color letterColor = new Color(0,0,128);
    setBackground(Color.lightGray);
    setForeground(letterColor);
    
    setLayout(new GridLayout(1,3));
        
    add(dateLabel);
    add(date);
    add(nameLabel);
    add(name);
    add(ssnLabel);
    add(ssn);
  }
}

//-------------------- Listener CLASS -----------------------
/**
  Initialize a Listener class to receive the user's input and 
  output a result
*/  
class Listener implements ActionListener
{
  private IncomeTax theApplet;
  
  public Listener(IncomeTax IHateTaxes)
  {
    theApplet = IHateTaxes; //Don't we all :-)
  }
  
  //respond to "Calculate" button engagement 
  public void actionPerformed(ActionEvent e)
  {
    String bttnName = e.getActionCommand();
    
    if(bttnName.equals("Calculate"))
    {
      theApplet.processCalcButton();
    }
  }
}