Link to home
Start Free TrialLog in
Avatar of stdebernardi
stdebernardi

asked on

payroll swing applet version 2

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
Avatar of Javatm
Javatm
Flag of Singapore image

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
         
Avatar of savalou
savalou

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.
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





 
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 :-)
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
Javatm.  There's no apostrophes around double values.
Ok sorry I'm at work ! :)
Ops sorry it should be like

Like : y = x * .28;

Use . ok!
// 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
Javatm, dude/dudette, where does x get it's value?
ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to you can customize it like :

//  For hourlyRate
x = hourlyRate * .28;

and

//  For hoursWorked
x = hoursWorked * .28;


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stdebernardi

ASKER

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.
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.
If your receiving that error try to add this to your import statement.

import java.text.DecimalFormat;

or

import java.text.*;

hope this helps . . .
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
Sorry I think your a girl :)