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

JavatmCommented:
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
savalouCommented:
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
JavatmCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Like : y = x * .28;

Use . ok!
0
JavatmCommented:
// 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
savalouCommented:
Javatm, dude/dudette, where does x get it's value?
0
JavatmCommented:
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

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
JavatmCommented:
If you want to you can customize it like :

//  For hourlyRate
x = hourlyRate * .28;

and

//  For hoursWorked
x = hoursWorked * .28;


0
jimmackCommented:
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
stdebernardiAuthor Commented:
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
stdebernardiAuthor Commented:
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
JavatmCommented:
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
JavatmCommented:
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
JavatmCommented:
Sorry I think your a girl :)
0
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.