Solved

create payroll swing applet

Posted on 2003-10-22
6
837 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Accepted Solution

by:
blackwednesday earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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