[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with Payroll calculations!

Posted on 2003-12-10
4
Medium Priority
?
570 Views
Last Modified: 2012-05-04
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);
    }
0
Comment
Question by:angelori
  • 2
4 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9917706
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
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 300 total points
ID: 9918302
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
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9918425
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
 
LVL 3

Accepted Solution

by:
InNoCenT_Ch1ld earned 1200 total points
ID: 9919122
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question