Solved

payroll swing applet version 2

Posted on 2003-10-22
18
600 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 video teaches viewers about errors in exception handling.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now