Help with Payroll calculations!

I am VERY new to Java programming and am so lost right now.  I am to create a payroll application that accepts input of time in/out 6 times and calculates the pay.  Well, I already know that I will not be able to calculate using the in/outs, so now all I want is to somehow just have a button on the screen that just says "total hours worked" and than calculate the pay.  I have changed my code around numerous times and cannot figure this out.  Here's my code.  If you cannot do anything with it because it is so messed up, that's fine.  If you can, I'm posting this as 500 pts; do what you want with it.  I also do not know how to cut down on the typing...I know you can create a method so you don't have to type each of the commands so many times, but am unaware of how to do this and make them separate.  I am probably not making sense....I have been working on this for HOURS.  Sorry.  Any help is GREATLY appreciated.

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

//Create payroll class - extension of the JApplet class
public class Payroll extends JApplet implements ActionListener
{
 
  float totalResult;
  //The following creates all fields and labels for the screen
  JLabel title = new JLabel("Our Payroll");
  JLabel datePrompt = new JLabel("Date:");
  JTextField dateVal = new  JTextField(8);
  JLabel empIdPrompt = new JLabel("Employee ID number:");
  JTextField idVal = new JTextField(9);
  JLabel sickPrompt = new JLabel("Sick time for today");
  JTextField sickVal = new  JTextField(8);
  JLabel pLPrompt = new JLabel("Personal Leave time for today");
  JTextField pLVal = new  JTextField(8);
  JLabel inPrompt1 = new  JLabel("Start Time:");
  JTextField in1Val = new  JTextField(5);
  JLabel outPrompt1 = new  JLabel("Out:");
  JTextField out1Val = new  JTextField(5);
  JLabel inPrompt2 = new  JLabel("In:");
  JTextField in2Val = new  JTextField(5);
  JLabel outPrompt2 = new  JLabel("Out:");
  JTextField out2Val = new  JTextField(5);
  JLabel inPrompt3 = new  JLabel("In:");
  JTextField in3Val = new  JTextField(5);
  JLabel outPrompt3 = new  JLabel("Out:");
  JTextField out3Val = new  JTextField(5);
  JLabel inPrompt4 = new  JLabel("In:");
  JTextField in4Val = new  JTextField(5);
  JLabel outPrompt4 = new  JLabel("Out:");
  JTextField out4Val = new  JTextField(5);
  JLabel inPrompt5 = new  JLabel("In:");
  JTextField in5Val = new  JTextField(5);
  JLabel outPrompt5 = new  JLabel("Out:");
  JTextField out5Val = new  JTextField(5);
  JLabel inPrompt6 = new  JLabel("In:");
  JTextField in6Val = new  JTextField(5);
  JLabel outPrompt6 = new  JLabel("Out:");
  JTextField out6Val = new  JTextField(5);
  JLabel totalHrs = new JLabel("Total Hours Worked Today:");
  JTextField totalHrsVal = new JTextField(4);
  JButton calcButton = new JButton("Calculate pay"); //need to figure out how to have user
                                                                           //then add hourly rate, etc.
 
  Font bigFont = new Font("Helvetica", Font.ITALIC, 16);

  public void init()
  {
    //places the labels/fields created above on screen
    Container con = getContentPane();
    con.setLayout(new FlowLayout(FlowLayout.LEFT));
    title.setFont(bigFont);
    con.add(title);
    con.add (datePrompt);
    con.add (dateVal);
    con.add (empIdPrompt);
    con.add (idVal);
    con.add (sickPrompt);
    con.add (sickVal);
    con.add (pLPrompt);
    con.add (pLVal);
    con.add (inPrompt1);
    con.add (in1Val);
    con.add (outPrompt1);
    con.add (out1Val);
    con.add (inPrompt2);
    con.add (in2Val);
    con.add (outPrompt2);
    con.add (out2Val);
    con.add (inPrompt3);
    con.add (in3Val);
    con.add (outPrompt3);
    con.add (out3Val);
    con.add (inPrompt4);
    con.add (in4Val);
    con.add (outPrompt4);
    con.add (out4Val);
    con.add (inPrompt5);
    con.add (in5Val);
    con.add (outPrompt5);
    con.add (out5Val);    
    con.add (inPrompt6);
    con.add (in6Val);
    con.add (outPrompt6);
    con.add (out6Val);
    con.add (totalHrs);
    con.add (totalHrsVal);
    con.add (calcButton);
    idVal.addActionListener(this);
    sickVal.addActionListener(this);
    pLVal.addActionListener(this);
    in1Val.addActionListener(this);
    out1Val.addActionListener(this);
    in2Val.addActionListener(this);
    out2Val.addActionListener(this);
    in3Val.addActionListener(this);
    out3Val.addActionListener(this);
    in4Val.addActionListener(this);
    out4Val.addActionListener(this);
    in5Val.addActionListener(this);
    out5Val.addActionListener(this);
    in6Val.addActionListener(this);
    out6Val.addActionListener(this);
    totalHrsVal.addActionListener(this);
  }

  public void start()
  {
    //Sets values for the input boxes and puts focus on that box for input request
       Date today = new Date();
    dateVal.setText("today");
    idVal.setText("");
    sickVal.setText("");
    pLVal.setText("");
    in1Val.setText("");
    out1Val.setText("");
    in2Val.setText("");
    out2Val.setText("");
    in3Val.setText("");
    out3Val.setText("");
    in4Val.setText("");
    out4Val.setText("");
    in5Val.setText("");
    out5Val.setText("");
    in6Val.setText("");
    out6Val.setText("");
    totalHrsVal.setText("");
    totalResult.setText("Calculate hours worked and hourly rate");
    repaint();
   

  }
 public void actionPerformed(ActionEvent e)
    {
     Object source = e.getSource();
     String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
     int hourlyRate = Integer.parseInt(response);
     response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
     int hoursWorked = Integer.parseInt(response);
     float totalPay;
     float totalResult;
     totalPay = hoursWorked * hourlyRate;
     totalResult.setText("$" + totalPay);
    }
angeloriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lwinkenbCommented:
float totalResult;
totalResult.setText("Calculate hours worked and hourly rate");

You cant do this.  The setText method applies to objects like JTextFields, not floating point variables.

Also, you redefine totalResult in the actionPerformed method.  You already declared it as a class instance variable, so you shouldnt redeclare it in the same scope.
0
JavatmCommented:
Trying following this sample and add some of your buttons to it
and you can change the type of calculation :

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20775060.html

Hope it helps . . .
Javatm
0
InNoCenT_Ch1ldCommented:
as lwinkenb said, declare your totalResult as JTextField.

JTextField totalResult = new JTextField(5);

then replace your actionPerformed(ActionEvent){} with this code:

public void actionPerformed(ActionEvent e)
    {
     //Object source = e.getSource(); (not necessary if you trigger the action by the calcButton only...)
     String hours = JOptionPane.showInputDialog(null, "Enter the   number of hours worked");
     int hoursWorked = Integer.parseInt(hours);

     String rate = JOptionPane.showInputDialog(null, "Enter the hourly rate");
     int hourlyRate = Integer.parseInt(rate);

     float totalPay;
     //float totalResult; (must declare in JTextField as i stated just now)

     totalPay = hoursWorked * hourlyRate;
     totalResult.setText("$" + totalPay);
    }


Besides, this is unnecessary if you want the calculation to perform after you click the calculate button. I strongly advise you to trigger the action by the button ONLY in your case.

   idVal.addActionListener(this);
    sickVal.addActionListener(this);
    pLVal.addActionListener(this);
    in1Val.addActionListener(this);
    out1Val.addActionListener(this);
    in2Val.addActionListener(this);
    out2Val.addActionListener(this);
    in3Val.addActionListener(this);
    out3Val.addActionListener(this);
    in4Val.addActionListener(this);
    out4Val.addActionListener(this);
    in5Val.addActionListener(this);
    out5Val.addActionListener(this);
    in6Val.addActionListener(this);
    out6Val.addActionListener(this);
    totalHrsVal.addActionListener(this);

replace the above codes with this line:
   calcButton .addActionListener(this);

**Remember:
- you already declare the "totalResult" globally, so avoid redefine it inside you methods.
>>as lwinkenb said: Also, you redefine totalResult in the actionPerformed method.  You already declared it as a class instance variable, so you shouldnt redeclare it in the same scope. ;)

hope it helps ;)

-Ch1ld-



 
0
InNoCenT_Ch1ldCommented:
this code should work fine:

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

//Create payroll class - extension of the JApplet class
public class Payroll extends JApplet implements ActionListener
{
 
  JTextField totalResult = new JTextField(10);
  //The following creates all fields and labels for the screen
  JLabel title = new JLabel("Our Payroll");
  JLabel datePrompt = new JLabel("Date:");
  JTextField dateVal = new  JTextField(8);
  JLabel empIdPrompt = new JLabel("Employee ID number:");
  JTextField idVal = new JTextField(9);
  JLabel sickPrompt = new JLabel("Sick time for today");
  JTextField sickVal = new  JTextField(8);
  JLabel pLPrompt = new JLabel("Personal Leave time for today");
  JTextField pLVal = new  JTextField(8);
  JLabel inPrompt1 = new  JLabel("Start Time:");
  JTextField in1Val = new  JTextField(5);
  JLabel outPrompt1 = new  JLabel("Out:");
  JTextField out1Val = new  JTextField(5);
  JLabel inPrompt2 = new  JLabel("In:");
  JTextField in2Val = new  JTextField(5);
  JLabel outPrompt2 = new  JLabel("Out:");
  JTextField out2Val = new  JTextField(5);
  JLabel inPrompt3 = new  JLabel("In:");
  JTextField in3Val = new  JTextField(5);
  JLabel outPrompt3 = new  JLabel("Out:");
  JTextField out3Val = new  JTextField(5);
  JLabel inPrompt4 = new  JLabel("In:");
  JTextField in4Val = new  JTextField(5);
  JLabel outPrompt4 = new  JLabel("Out:");
  JTextField out4Val = new  JTextField(5);
  JLabel inPrompt5 = new  JLabel("In:");
  JTextField in5Val = new  JTextField(5);
  JLabel outPrompt5 = new  JLabel("Out:");
  JTextField out5Val = new  JTextField(5);
  JLabel inPrompt6 = new  JLabel("In:");
  JTextField in6Val = new  JTextField(5);
  JLabel outPrompt6 = new  JLabel("Out:");
  JTextField out6Val = new  JTextField(5);
  JLabel totalHrs = new JLabel("Total Hours Worked Today:");
  JTextField totalHrsVal = new JTextField(4);
  JButton calcButton = new JButton("Calculate pay"); //need to figure out how to have user
                                                                           //then add hourly rate, etc.
 
  Font bigFont = new Font("Helvetica", Font.ITALIC, 16);

  public void init()
  {
    //places the labels/fields created above on screen
    Container con = getContentPane();
    con.setLayout(new FlowLayout(FlowLayout.LEADING));

    title.setFont(bigFont);
    con.add(title);
    con.add (datePrompt);
    con.add (dateVal);
    con.add (empIdPrompt);
    con.add (idVal);
    con.add (sickPrompt);
    con.add (sickVal);
    con.add (pLPrompt);
    con.add (pLVal);
    con.add (inPrompt1);
    con.add (in1Val);
    con.add (outPrompt1);
    con.add (out1Val);
    con.add (inPrompt2);
    con.add (in2Val);
    con.add (outPrompt2);
    con.add (out2Val);
    con.add (inPrompt3);
    con.add (in3Val);
    con.add (outPrompt3);
    con.add (out3Val);
    con.add (inPrompt4);
    con.add (in4Val);
    con.add (outPrompt4);
    con.add (out4Val);
    con.add (inPrompt5);
    con.add (in5Val);
    con.add (outPrompt5);
    con.add (out5Val);    
    con.add (inPrompt6);
    con.add (in6Val);
    con.add (outPrompt6);
    con.add (out6Val);
    con.add (totalHrs);
    con.add (totalHrsVal);
    con.add (calcButton);
    con.add (totalResult);
    calcButton.addActionListener(this);
  }

  public void start()
  {
    //Sets values for the input boxes and puts focus on that box for input request
      Date today = new Date();
    dateVal.setText("today");
    idVal.setText("");
    sickVal.setText("");
    pLVal.setText("");
    in1Val.setText("");
    out1Val.setText("");
    in2Val.setText("");
    out2Val.setText("");
    in3Val.setText("");
    out3Val.setText("");
    in4Val.setText("");
    out4Val.setText("");
    in5Val.setText("");
    out5Val.setText("");
    in6Val.setText("");
    out6Val.setText("");
    totalHrsVal.setText("");
    totalResult.setText("Calculate hours worked and hourly rate");
    repaint();
   

  }
public void actionPerformed(ActionEvent e)
    {
     //Object source = e.getSource(); (not necessary if you trigger the action by the calcButton only...)
     String hours = JOptionPane.showInputDialog(null, "Enter the   number of hours worked");
     int hoursWorked = Integer.parseInt(hours);

     String rate = JOptionPane.showInputDialog(null, "Enter the hourly rate");
     int hourlyRate = Integer.parseInt(rate);

     float totalPay;
     //float totalResult; (must declare in JTextField as i stated just now)

     totalPay = hoursWorked * hourlyRate;
     totalResult.setText("$" + totalPay);
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.