Solved

payroll swing applet version 2

Posted on 2003-10-22
18
606 Views
Last Modified: 2013-12-29
I need to have a payroll Swing applet that allows the user to enter two double values: hours worked and hourly rate. When the user clicks a JButton, gross pay is calculated.

In addition I need to have the federal withholding tax substracted from gross pay based on:

Income$                 Withholding%
0 to 99.99                      10
100 to 299.99      15
300 to 599.99      21
600 and up      28

I am stuck trying to have the program to substract in %.
Any help would be appreciated. Here is my code:

// Stephanie De Bernardi
// POS 406

// Modify the payroll Swing applet so that federal withholding tax is substracted from gross pay based on:
// 0 to 99.99 = 10%
// 100 to 299.99 = 15%
// 300 to 599.99 = 21%
// 600 and up = 28%


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    int totalPay;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {

          String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
          int[] hoursLimit = {0, 100, 300, 600};
          int[] withholding = {10%, 15%, 21%, 28%};
          int hourlyRate = Integer.parseInt(response);

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


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

Thanks
0
Comment
Question by:stdebernardi
  • 11
  • 3
  • 2
  • +1
18 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 9602179
To stdebernardi;

>> I am stuck trying to have the program to substract in %.

     It will be better to if you will get the %percentage 1st before you subtract it
 
>> totalPay = (hoursWorked * hourlyRate) - percentage;
     
Hope it helps . . .
JAVATM
         
0
 
LVL 3

Expert Comment

by:savalou
ID: 9602319
Something flexible would be like:

// calculate gross pay
int percentage = 0;
for (int i = hoursLimit.length; i > 0; --i) {
   if (grossPay >= hoursLimit[i]) {
      percentage = withholding[i];
      break;
   }
}
totalPay = grossPay * (1.0 - percentage);

Of course, if you are expressing percentages as integers, then divide by 100 first.

I'm a bit confused by your code, are you actually trying to use % signs in it?  % is the modulus operator.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602332
Do something like this, just a simple if & else statement;

// 0 to 99.99 = 10%
// 100 to 299.99 = 15%
// 300 to 599.99 = 21%
// 600 and up = 28%

double x;
double y;

if (x<='99') {

y = x  *  10;

}

else if (x<='299') {

y = x * 15;

}

else if (x<='599) {

y = x * 21;

else if (x>='600') {

y = x * 28;

}

totalPay = (hoursWorked * hourlyRate) - y;

Hope this helps . . .
JAVATM





 
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:jimmack
ID: 9602333
int wholding = 0;

for (int i = 0; i < 4; i++)
{
    if (hoursWorked > hoursLimit[i])
    {
        wholding = withholding[i];
    }
}

This will give you the appropriate percentage (based on your two arrays).

To start with, it assumes 0% and 0 hours.  It then steps through the hoursLimit array.  If the hoursWorked exceeds the limit, it sets the new withholding percentage (wholding).

I'll leave the rest of the calculations to you :-)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602353
Oops Sorry I'm at work this should work;

double x;
double y;

if (x<='99.99') {

y = x  *  10;

}

else if (x<='299.99') {

y = x * 15;

}

else if (x<='599.99') {

y = x * 21;

}

else if (x>='600.99') {

y = x * 28;

}

totalPay = (hoursWorked * hourlyRate) - y;

Hope it helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9602367
Javatm.  There's no apostrophes around double values.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602397
Ok sorry I'm at work ! :)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602432
Ops sorry it should be like

Like : y = x * .28;

Use . ok!
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9602684
// Try this :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    double totalPay;
    double x;
    double y;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {
    String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
    double hourlyRate = Integer.parseInt(response);

    response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
    double hoursWorked = Integer.parseInt(response);

    try {

    if (x<=99.99) {
    y = x  * .10;
    }
    else if (x<=299.99) {
    y = x * .15;
    }
    else if (x<=599.99) {
    y = x * .21;
    }
    else if (x>=600.99) {
    y = x * .28;
    }
    }
    catch(NumberFormatException ex) {
    JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);
    }

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

Hope it helps . . .
JAVATM
0
 
LVL 3

Expert Comment

by:savalou
ID: 9602921
Javatm, dude/dudette, where does x get it's value?
0
 
LVL 15

Accepted Solution

by:
Javatm earned 450 total points
ID: 9603040
Sorry I'm at work have mercy on me :)
Try this :

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

public class JCalculatePay2 extends JApplet implements ActionListener
{
    JLabel companyName = new
    JLabel("Accounting Inc");
    JButton calcButton = new JButton("Calculate");
    JButton okButton = new JButton("OK");
    JLabel perPersonResult = new JLabel("Payroll.");
    JLabel totalResult = new JLabel("Calculate hours worked and hourly rate");
    double totalPay;
    double x;

    Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

    public void init()
    {
         Container con = getContentPane();
         con.setLayout(new FlowLayout());
         companyName.setFont(bigFont);
         con.add(companyName);
         con.add(calcButton);
         con.add(okButton);
         calcButton.addActionListener(this);
         okButton.addActionListener(this);
         con.add(perPersonResult);
         con.add(totalResult);
    }

    public void start()
    {
         perPersonResult.setText("Your pay is.");
         totalResult.setText("Calculate hours worked and hourly rate");
         repaint();
    }

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source == calcButton)
    {
    String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
    double hourlyRate = Integer.parseInt(response);

    response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
    double hoursWorked = Integer.parseInt(response);

    try {

// For hourlyRate

    if (hourlyRate<=99.99) {
    x = 99.99  * .10;
    }
    else if (hourlyRate<=299.99) {
    x = 299.99 * .15;
    }
    else if (hourlyRate<=599.99) {
    x = 599.99 * .21;
    }
    else if (hourlyRate>=600.99) {
    x = 600.99 * .28;
    }

// For hoursWorked

    if (hoursWorked<=99.99) {
    x = 99.99  * .10;
    }
    else if (hoursWorked<=299.99) {
    x = 299.99 * .15;
    }
    else if (hoursWorked<=599.99) {
    x = 599.99 * .21;
    }
    else if (hoursWorked>=600.99) {
    x = 600.99 * .28;
    }

    }
    catch(NumberFormatException ex) {
    JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);
    }

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

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603073
If you want to you can customize it like :

//  For hourlyRate
x = hourlyRate * .28;

and

//  For hoursWorked
x = hoursWorked * .28;


0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 50 total points
ID: 9603189
Here is the replacement for your actionPerformed method.  Just put this into your original code.

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == calcButton)
        {
   
             String response = JOptionPane.showInputDialog(null, "Enter the number of hours worked");
             double[] payLimit = {0.0, 100.0, 300.0, 600.0};
             double[] withholding = {0.10, 0.15, 0.21, 0.28};
             try
             {
                 double hourlyRate = Double.parseDouble(response);
       
                 response = JOptionPane.showInputDialog(null, "Enter the hourly rate");
                 double hoursWorked = Double.parseDouble(response);
       
                 double totalPay = (hoursWorked * hourlyRate);
   
                 double wholding = 0.0;
                 for (int i = 0; i < 4; i++)
                 {
                     if (totalPay > payLimit[i])
                     {
                         wholding = withholding[i];
                     }
                 }
   
                 totalPay = totalPay - (totalPay * wholding);
                 
                 DecimalFormat fmt = new DecimalFormat("#.00");
                 totalResult.setText("$" + fmt.format(totalPay));
             }
             catch (NumberFormatException nfe)
             {
                 totalResult.setText("Only enter decimal numbers");
             }
        }
    }
0
 

Author Comment

by:stdebernardi
ID: 9603744
Jimmack,

I am getting the following error for the latest version you proposed.

C:\Documents and Settings\Stephanie\Desktop\Program\WEEK 5\JCalculatePay\JCalculatePay2.java:77: cannot resolve symbol
symbol  : class DecimalFormat
location: class JCalculatePay2
                 DecimalFormat fmt = new DecimalFormat("#.00");
                 ^
C:\Documents and Settings\Stephanie\Desktop\Program\WEEK 5\JCalculatePay\JCalculatePay2.java:77: cannot resolve symbol
symbol  : class DecimalFormat
location: class JCalculatePay2
                 DecimalFormat fmt = new DecimalFormat("#.00");
                                         ^

I will take a closer look at the several solutions presented by all of you, but until now, none of them compile correctly even when I try to change or debug. But thanks anyway. Hopefully we will find a solution.
0
 

Author Comment

by:stdebernardi
ID: 9603887
After looking at every solutions I found one that works.
It seems always so easy when you actually see the errors you did or what you missed.
Thanks to all of you for your help, I understood my errors.
Thanks again.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603916
If your receiving that error try to add this to your import statement.

import java.text.DecimalFormat;

or

import java.text.*;

hope this helps . . .
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9603955
Thanks dude, I dont actually have a compiler or a jdk on my machine.
I just tried to compile it in the web because I'm at work. Anyways thanks again.
I just want to correct something.

JOptionPane.showInputDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);

Change it to this :

JOptionPane.showMessageDialog(null, "Invalid Value Entered","Error . . .",JOptionPane.ERROR_MESSAGE);

because your declaring a message.
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9604463
Sorry I think your a girl :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
spring jars download 1 35
Desingning Refactoring existing code 2 26
Glassfish admin console not working 1 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

839 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