Solved

create payroll swing applet

Posted on 2003-10-22
6
850 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

813 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