Solved

create payroll swing applet

Posted on 2003-10-22
6
855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

752 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