Results 1 to 15 of 15
- 01-16-2011, 05:32 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
help with GUI and multiple classes
I have a simple GUI program that calculates the pay of each employee, but the if statements are not working. the code is below. any help will be appreciated.
Java Code://Gross and Net biweekely salary import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.String; import java.io.*; //Net = after //gross = before public class Payrollhelp { JFrame frame; JPanel contentPane; JLabel prompt1, prompt2, prompt3, prompt4, prompt5, stat; JTextField name, wageT, hoursT, charityT, newFieldT; JButton gross, net, cpp, ei, charity, newField; public Payrollhelp(){ /* Create and set up the frame */ frame = new JFrame("Payroll Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* Create a content pane with a GridLayout and empty borders */ contentPane = new JPanel(); contentPane.setLayout(new GridLayout(0, 2, 10, 5)); contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); contentPane.setBackground(Color.white); /* Create and add a prompt and then a text field */ prompt1 = new JLabel("Enter the employee name: "); contentPane.add(prompt1); name = new JTextField(10); contentPane.add(name); /* Create and add a prompt and then a text field */ prompt1 = new JLabel("Enter the hourly wage: "); contentPane.add(prompt1); wageT = new JTextField(10); contentPane.add(wageT); /* Create and add a second prompt and then a text field */ prompt2 = new JLabel("Enter the hours worked: "); contentPane.add(prompt2); hoursT = new JTextField(10); contentPane.add(hoursT); /* Create and add a third prompt and then a text field */ prompt3 = new JLabel("Enter the charity percentage: "); contentPane.add(prompt3); charityT = new JTextField(10); contentPane.add(charityT); /* Create and add button that will display the gross pay */ gross = new JButton("Gross Pay"); gross.addActionListener(new PayCalculator()); contentPane.add(gross); /*Create and add button that will display the net pay*/ net = new JButton("Net Pay"); net.addActionListener(new NetPay()); contentPane.add(net); /* Create and add button that will display the charity */ charity = new JButton("CPP"); charity.addActionListener(new CanadaPensionPlan()); contentPane.add(charity); /* Create and add a label that will display stats(whatever is selected) */ stat = new JLabel(" "); stat.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0)); contentPane.add(stat); /* Add content pane to frame */ frame.setContentPane(contentPane); /* Size and then display the frame. */ frame.pack(); frame.setVisible(true); } /* * PayCalculator class that calculates the gross pay */ class PayCalculator implements ActionListener { public void actionPerformed(ActionEvent event) { String g1 = name.getText(); String g2 = wageT.getText(); String g3 = hoursT.getText(); String g4 = charityT.getText(); double gross = 0; gross = (Double.parseDouble(g2) * Double.parseDouble(g3))*2.00; stat.setText(" Gross Pay = $" + Double.toString(gross)); } } /* * NetPay class that calculates and displays the Net Pay */ class NetPay implements ActionListener { public void actionPerformed(ActionEvent event) { String g5 = wageT.getText(); String g6 = hoursT.getText(); String g7 = charityT.getText(); double net = 0.0; double cppAmount = 0; double charity = 0.0; double x = (Double.parseDouble(g5) * Double.parseDouble(g6)) * 2.00; if (x >= 1513.10 && x <=1523.09) { cppAmount = 59.49; } else if (x >= 1523.10 && x <=1533.09) { cppAmount = 59.92; } else if (x >= 1533.10 && x <=1543.09 ){ cppAmount = 60.35; } else if (x >= 1543.10 && x <=1553.09 ){ cppAmount = 60.78; } else if (x >= 1553.10 && x <=1563.09 ){ cppAmount = 61.21; } else if (x >= 1563.10 && x <=1573.09 ){ cppAmount = 61.64; } else if (x >= 1573.10 && x <=1583.09 ){ cppAmount = 62.07; } else if (x >= 1583.10&& x <=1593.09 ){ cppAmount = 62.50; } else if (x >= 1593.10&& x <=1603.09 ){ cppAmount = 62.93; } else if (x >= 1603.10&& x <=1613.09 ){ cppAmount = 63.36; } charity = (Double.parseDouble(g7)/100) * x; net = (x - charity); net = (net - cppAmount); stat.setText(" Net Pay = $" + Double.toString(net)); } } /* * CPP class that calculates and displays the CPP contributions */ class CanadaPensionPlan implements ActionListener { public void actionPerformed(ActionEvent event) { String g11 = wageT.getText(); String g12 = hoursT.getText(); double cppContributions = 0.0; double z = (Double.parseDouble(g11) * Double.parseDouble(g12)) * 2.00; if (z >= 1513.10 && z <=1523.09) { cppContributions = 59.49; } else if (z >= 1523.10 && z <=1533.09) { cppContributions = 59.92; } else if (z >= 1533.10&& z <=1543.09 ){ cppContributions = 60.35; } else if (z >= 1543.10&& z <=1553.09 ){ cppContributions = 60.78; } double a = cppContributions; stat.setText("CPP Contributions = &" + Double.toString(cppContributions)); } } /** * Create and show the GUI. */ private static void runGUI() { JFrame.setDefaultLookAndFeelDecorated(true); Payroll myGrades = new Payroll(); } /* * main method of the program */ public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { runGUI(); } }); } }
Moderator Edit: Code tags addedLast edited by Fubarable; 01-16-2011 at 05:36 PM. Reason: Moderator Edit: Code tags added
- 01-16-2011, 05:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Print the value of your variables before you go through all those if-else-statements and see for yourself.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-16-2011, 06:31 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
i have tried that. in the net pay class, only the first couple of if statements work. And the CanadaPensionPlan() method does not work at all, even though it is linked to the CPP button.
-
- 01-16-2011, 07:13 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
According to the program, when I click on the CPP button, the CanadaPensionPlan() is supposed to be called upon and the amount of cpp contribution must be displayed by the CanadaPensionPlan()
But in the GUI, when I click on the CPP button, the CanadaPensionPlan() is not called upon, and for the life of me I don't know why.
-
- 01-16-2011, 07:37 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
-
- 01-16-2011, 07:59 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Here is the code with the debug statements:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.io.*;
//Net = after
//gross = before
public class Payrollhelp {
JFrame frame;
JPanel contentPane;
JLabel prompt1, prompt2, prompt3, prompt4, prompt5, stat;
JTextField name, wageT, hoursT, charityT, newFieldT;
JButton gross, net, cpp, ei, charity, newField;
public Payrollhelp(){
/* Create and set up the frame */
frame = new JFrame("Payroll Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
/* Create a content pane with a GridLayout and empty borders */
contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 2, 10, 5));
contentPane.setBorder(BorderFactory.createEmptyBor der(10, 10, 10, 10));
contentPane.setBackground(Color.white);
/* Create and add a prompt and then a text field */
prompt1 = new JLabel("Enter the employee name: ");
contentPane.add(prompt1);
name = new JTextField(10);
contentPane.add(name);
/* Create and add a prompt and then a text field */
prompt1 = new JLabel("Enter the hourly wage: ");
contentPane.add(prompt1);
wageT = new JTextField(10);
contentPane.add(wageT);
/* Create and add a second prompt and then a text field */
prompt2 = new JLabel("Enter the hours worked: ");
contentPane.add(prompt2);
hoursT = new JTextField(10);
contentPane.add(hoursT);
/* Create and add a third prompt and then a text field */
prompt3 = new JLabel("Enter the charity percentage: ");
contentPane.add(prompt3);
charityT = new JTextField(10);
contentPane.add(charityT);
/* Create and add button that will display the gross pay */
gross = new JButton("Gross Pay");
gross.addActionListener(new PayCalculator());
contentPane.add(gross);
/*Create and add button that will display the net pay*/
net = new JButton("Net Pay");
net.addActionListener(new NetPay());
contentPane.add(net);
/* Create and add button that will display the charity */
charity = new JButton("CPP");
charity.addActionListener(new CanadaPensionPlan());
contentPane.add(charity);
/* Create and add a label that will display stats(whatever is selected) */
stat = new JLabel(" ");
stat.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
contentPane.add(stat);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
/*
* PayCalculator class that calculates the gross pay
*/
class PayCalculator implements ActionListener {
public void actionPerformed(ActionEvent event) {
String g1 = name.getText();
String g2 = wageT.getText();
String g3 = hoursT.getText();
String g4 = charityT.getText();
double gross = 0;
gross = (Double.parseDouble(g2) * Double.parseDouble(g3))*2.00;
stat.setText(" Gross Pay = $" + Double.toString(gross));
}
}
/*
* NetPay class that calculates and displays the Net Pay
*/
class NetPay implements ActionListener {
public void actionPerformed(ActionEvent event) {
String g5 = wageT.getText();
String g6 = hoursT.getText();
String g7 = charityT.getText();
double net = 0.0;
double cppAmount = 0;
double charity = 0.0;
double x = (Double.parseDouble(g5) * Double.parseDouble(g6)) * 2.00;
System.out.println(x);
stat.setText(Double.toString(x));
if (x >= 1513.10 && x <=1523.09) {
cppAmount = 59.49;
} else if (x >= 1523.10 && x <=1533.09) {
cppAmount = 59.92;
} else if (x >= 1533.10 && x <=1543.09 ){
cppAmount = 60.35;
} else if (x >= 1543.10 && x <=1553.09 ){
cppAmount = 60.78;
} else if (x >= 1553.10 && x <=1563.09 ){
cppAmount = 61.21;
} else if (x >= 1563.10 && x <=1573.09 ){
cppAmount = 61.64;
} else if (x >= 1573.10 && x <=1583.09 ){
cppAmount = 62.07;
} else if (x >= 1583.10&& x <=1593.09 ){
cppAmount = 62.50;
} else if (x >= 1593.10&& x <=1603.09 ){
cppAmount = 62.93;
} else if (x >= 1603.10&& x <=1613.09 ){
cppAmount = 63.36;
}
charity = (Double.parseDouble(g7)/100) * x;
net = (x - charity);
net = (net - cppAmount);
stat.setText(" Net Pay = $" + Double.toString(net));
}
}
/*
* CPP class that calculates and displays the CPP contributions
*/
class CanadaPensionPlan implements ActionListener {
public void actionPerformed(ActionEvent event) {
String g11 = wageT.getText();
String g12 = hoursT.getText();
double cppContributions = 0.0;
double z = (Double.parseDouble(g11) * Double.parseDouble(g12)) * 2.00;
System.out.println(z);
stat.setText(z);
if (z >= 1513.10 && z <=1523.09) {
cppContributions = 59.49;
} else if (z >= 1523.10 && z <=1533.09) {
cppContributions = 59.92;
} else if (z >= 1533.10&& z <=1543.09 ){
cppContributions = 60.35;
} else if (z >= 1543.10&& z <=1553.09 ){
cppContributions = 60.78;
}
double a = cppContributions;
stat.setText("CPP Contributions = &" + Double.toString(cppContributions));
}
}
/**
* Create and show the GUI.
*/
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
Payroll myGrades = new Payroll();
}
/*
* main method of the program
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}
- 01-16-2011, 08:07 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Sir: Your problem is simple. You called the wrong class. You typed in Payroll instead of Payrollhelp. The compile error would tell you this. Here is part of it. You are searching for something difficult when the answer is plain. This often happens when you are tired. Sincerely--G.O.
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Payroll cannot be resolved to a type
Payroll cannot be resolved to a type
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
Payrollhelp myGrades = new Payrollhelp();
}
- 01-16-2011, 08:07 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
The values you are checking for are within a very small range so you have to be quite specific with the values you input for pay and hours worked else you get zero as an answer most of the time for CPP. Other than that, I tried it ( had to modify it to get it running though, you don't create a new Payrollhelp() object ) but overall it works.
The advice you've been given though is the best way to proceed.
- 01-16-2011, 08:22 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
-
- 01-16-2011, 08:40 PM #14
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Hi guys. The program will still not run. When I try to calculate the net pay, both the charity and CPP have to be subtracted from the gross pay, but only the charity amount is being subtracted from the gross pay. any ideas as to why?
- 01-16-2011, 10:08 PM #15
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Problem with multiple string in classes
By sjaakie in forum New To JavaReplies: 3Last Post: 10-10-2010, 02:48 PM -
uploading applet with multiple classes help
By alacn in forum New To JavaReplies: 3Last Post: 08-02-2010, 06:37 AM -
Help with multiple frames/classes
By Celletti in forum AWT / SwingReplies: 1Last Post: 04-28-2010, 03:18 AM -
How to use multiple timer classes in swings
By theone3nu in forum AWT / SwingReplies: 12Last Post: 12-30-2008, 02:30 AM -
More efficient in memory? Multiple Jars or Single Jars with lot's of Classes
By dark_cybernetics in forum New To JavaReplies: 0Last Post: 08-19-2008, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks