Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create payroll swing applet

Posted on 2003-10-22
6
Medium Priority
?
878 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 200 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 800 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Accepted Solution

by:
blackwednesday earned 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
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 …
Suggested Courses

722 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