Solved

create payroll swing applet

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

I've been stuck with this program for hours and can't figure out how to make it work. I keep getting compiler errors.

I hope somebody can help me. Here is my code:

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

      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);
            con.add(perPersonResult);
            con.add(totalResult);
      }

      public void start()
      {
            perPersonResult.setText("Payroll.");
            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");
      }
      else if(source == okButton)
      {
            String response = JOptionPane.showInputDialog(null, "Enter the hourly rate");


            int totalPay;
            int hoursWorked = Integer.parseInt(response);
            int hourlyRate = 0;
            int x = 0, a = 0;

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

Thanks
0
Comment
Question by:stdebernardi
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 50 total points
ID: 9597313
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

public class JCalculatePay
    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" ) ;

  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( "Payroll." ) ;
    totalResult.setText( "Calculate hours worked and hourly rate" ) ;
    repaint() ;
  }

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

      int hoursWorked = Integer.parseInt( response ) ;

      response = JOptionPane.showInputDialog( null,
          "Enter the hourly rate" ) ;

      float hourlyRate = Float.parseFloat( response ) ;
      float totalPay ;

      int x = 0, a = 0 ;

      {
        totalPay = hoursWorked * hourlyRate ;
        x = 0 ;
      }
      totalResult.setText( "$" + totalPay ) ;
    }
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9597317
The calc button does nothing with my version, all of the code hangs off the OK button, but it works...I hope you get the idea, and this helps you :-)

I use a float for hourlyrate, and totalpay, and now it reads the hourlyrate (you used to throw the value away)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 9597328
You need to get the dialog boxes appearing consecutively:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCalculatePay 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("Payroll.");
         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);
    totalPay = hoursWorked * hourlyRate;
    totalResult.setText("$" + totalPay);
}
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
blackwednesday earned 50 total points
ID: 9601345
well, the code you have here doesn't compile because you haven't declared the okButton variable.  

I've made your code work (completely, as far as I can see), with the following mods:
- i declared the JButton okButton with the calcButton
- i added your ints (totalPay, hourlyPay, hoursWorked) as instance fields, as well as a fourth, personPay, to keep track of the pay of the current person (don't know if you need this or not).
- i parsed the "response" inside each of the conditions of the ActionPerformed method, and set the fields hourlyPay and hoursWorked to be equal to the parsed result in each case
- i added a method, update, that looks like this:

public void update()//**
    {
      personPay = hoursWorked * hourlyRate;
      totalPay += personPay;
      perPersonResult.setText(" Pay: $" + personPay);
      totalResult.setText(" Total Pay: $" + totalPay);
      repaint();
    }

- i call this method inside the okButton's condition in ActionPerformed (there are certainly some bugs here, like it's important for the user to hit OK after Calculate, but if you need help dealing with that, just ask)

hope this helps.
0
 

Expert Comment

by:blackwednesday
ID: 9601373
By the way, if you find my answer helpful, please give me points (or give the points to the others, if they're helpful).  I want to offer more points to get my own question answered :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9602023
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 36
java 8 lambda expresssions exception handling 3 72
stackato and cloud 4 64
servlet  URL Rewriting 1 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 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

10 Experts available now in Live!

Get 1:1 Help Now