mward92 0 Newbie Poster

I have made 4 classes that represent an Employee based Java GUI. They are Person, Employee (extends the Person class), EmployeeFrame (used to model a JFrame), and EmployeeTester (contains the main method to run the GUI).

What I want the GUI to be able to do, is allow the user to choose between 5 employees and carry out basic functions on the salary of the chosen employee, currentEmployee. I must use the methods contained in the Employee class which are: increaseSalary(), addBonus(), and calculateMonthlyWage. I have completed the basic front end GUI and I want to know what I should add to the ActionListener in EmployeeFrame.

Any help is greatly appreciated.

Person

public abstract class Person
{
    protected String name;                              // instance variables

    public Person()                                     // constructor 1
    {
      this.name = new String();
   }

    public Person(String name)                          // constructor 2
    {
        this.name = name;
    }

    public String getName()                             // getName() method
    {
       return name;
    }

    public String toString()                            // toString() method
    {
       return name;
    }

    public boolean equals(Person personIn)         // equals() method 
    {
       if(name.equals(personIn.name))
            return true;
        else
           return false;
    }
}

Employee

public class Employee extends Person
{
    private int employeeNumber;                                                                             // Instance Variables
    private String jobDescription;
    private double salary;
    private static int nextUniqueNumber = 1;                                                 // Next available unique Employee number
    private final int MAX_SALARY = 100000;                                                                 // static - means nextUniqueNumber is SHARED
    private final double BONUS_A = 0.02, BONUS_B = 0.05, BONUS_C = 0.08;                            // amongst all Employee objects, so if one
    private final double TAX_A = 0.2, TAX_B = 0.41;                                                     // of them change it, it is changed for all.                                                                                
    private final double HI_TAX_BAND = 36000.00;

    public Employee()                                                                                               // Constructor 1
    {
        this.employeeNumber = nextUniqueNumber++;                                                           // Set employeeNumber to nextUniqueNumber, 
        this.jobDescription = new String();                                                                 // then increment it for next Employee
        this.salary = 0.0;
    }

    public Employee(String name, String jobDescription, int salary)                             // Constructor 2
    {
        super(name);
        this.employeeNumber = nextUniqueNumber++;                                                           // Set employeeNumber to nextUniqueNumber, 
        this.jobDescription = jobDescription;                                                               // then increment it for next Employee
        if(salary <= MAX_SALARY)
            this.salary = salary;
        else
           salary = MAX_SALARY;
    }

    public String getJobDescription()                                                                       // getJobDescription() method
    {
       return jobDescription;
    }

    public double getSalary()                                                                                   // getSalary() method
    {
       return salary;
    }

    public String toString()                                                                                    // toString() method to return details
    {
       return "EMPLOYEE NUMBER " + employeeNumber + " ==> " + super.toString() + ", " + jobDescription + " , SALARY : €" + salary;
    }

    public boolean equals(Employee employeeIn)                                                          // equals() method to compare 
    {
       if(employeeNumber == employeeIn.employeeNumber &&
           super.equals(employeeIn))
            return true;
        else
           return false;
    }

    public String increaseSalary(double newSalary)                                                      // increaseSalary() method
    {
        String returnMessage = "";

        if(newSalary > salary && newSalary <= MAX_SALARY)
        {
           salary = newSalary;
            returnMessage = "Salary changed to " + newSalary;
        }
        else if(newSalary > MAX_SALARY)
            returnMessage = newSalary + " exceeds allowable salary.";       
        else if(newSalary <= salary)
            returnMessage = newSalary + " must be greater than " + salary;      

        return returnMessage;                                                                                   // return message to indicate salary changed or an error occurred
    }

    public void addBonus(int bonusPercentage)                                                               // addBonus() method
    {
       switch(bonusPercentage)
        {
           case 2:salary += (salary * BONUS_A);break;
           case 5:salary += (salary * BONUS_B);break;
           case 8:salary += (salary * BONUS_C);break;
            default:;
        }
        if(salary > MAX_SALARY)
           salary = MAX_SALARY;
    }

    public double calculateMonthlyWage()                                                                    // calculateMonthlyWage() method
    {
       double wage = salary / 12;

        if(salary >= HI_TAX_BAND)
           wage -= (wage * TAX_B);
        else
           wage -= (wage * TAX_A);

        return wage;
    }   
} 

EmployeeTester

import java.util.*;
import javax.swing.*;

public class EmployeeTester
{
    public static void main(String args[])
    {
        final int NUMBER_OF_EMPLOYEES = 5;                                                          // declare an Array of 5 Employees called employees
        Employee[] employees = new Employee[NUMBER_OF_EMPLOYEES];

        Employee e1 = new Employee("Patrick Doyle", "Computer Programmer", 45000);     // create 5 new Employee objects with initial values
       Employee e2 = new Employee("Dylan Sweeney", "Project Manager", 40000);
       Employee e3 = new Employee("Louise Coyle", "Software Tester", 35000);
       Employee e4 = new Employee("Mark White", "Security Analyst", 42000);
       Employee e5 = new Employee("Emma Logan", "Database Administrator", 32000);

        employees[0] = e1;                                                                             // add new objects to the array called employees
        employees[1] = e2;
        employees[2] = e3;
        employees[3] = e4;
        employees[4] = e5;                                                                              // showInputDialog() passing in the employees array
                                                                                                                // returnedValue will get the selected Employee
        Employee returnedValue = (Employee)JOptionPane.showInputDialog(null, "Choose an Employee", 
        "EMPLOYEES", JOptionPane.INFORMATION_MESSAGE, null, employees, employees[0]);

        EmployeeFrame frame = new EmployeeFrame(returnedValue);                             // construct a EmployeeFrame object called frame, 
        frame.setTitle("Employee Salary Calculator");                                           // passing the selected Employee into the constrctor
        frame.pack();   
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);                    
        frame.setVisible(true);
    }
} 

EmployeeFrame

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EmployeeFrame extends JFrame                                                           // EmployeeFrame IS-A JFrame ==> Inheritance
{
    private Employee currentEmployee;                                                               // instance variable to keep track of currentEmployee
    private JPanel p1, p2;                                                                              // declare GUI Components here  
    private JLabel jlblCurrentSalary, jlblBonusPercentage, jlblNewSalary, jlblMonthlyWage;                      
    private JTextField jtfCurrentSalary, jtfBonusPercentage, jtfNewSalary, jtfMonthlyWage;  
    private final String requiredPassword = "LetMeIn";
    private JComboBox jcbBonusPercentage;

    public EmployeeFrame(Employee employeeIn)                                                       // setLayout & add Components here, takes in the
    {                                                                                                          // selected employee & assigns it to currentEmployee
        currentEmployee = employeeIn;                                                                  // set up currentEmployee

        p1 = new JPanel();                                                                              // construct Components
        p1.setLayout(new GridLayout(4,2,5,0));  
        p1.add(jlblCurrentSalary = new JLabel("Current Salary: "));
        jlblCurrentSalary.setFont(new Font("Helvetica", Font.BOLD, 15));
        p1.add(jtfCurrentSalary = new JTextField());    
        p1.add(jlblBonusPercentage = new JLabel("Bonus Percentage: "));
        jlblBonusPercentage.setFont(new Font("Helvetica", Font.BOLD, 15));
        String[] bonusPercentageList = {"2%", "5%", "8%"};
        p1.add(jcbBonusPercentage = new JComboBox(bonusPercentageList));
        p1.add(jlblNewSalary = new JLabel("New salary : "));
        jlblNewSalary.setFont(new Font("Helvetica", Font.BOLD, 15));
        p1.add(jtfNewSalary = new JTextField());
        p1.add(jlblMonthlyWage = new JLabel("Monthly wage: "));
        jlblMonthlyWage.setFont(new Font("Helvetica", Font.BOLD, 15));
        p1.add(jtfMonthlyWage = new JTextField());

        add(p1);                                                                                                // add JPanels to the JFrame        

        ListenerClass listener = new ListenerClass();                                 // indicate that this class will handle the button click
      jtfCurrentSalary.addActionListener(listener);
    }
    private class ListenerClass implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
           Employee currentEmployee = new Employee();
            // increaseSalary
            // addBonus
            // calculateMonthlyWage
    }
    }
}