Results 1 to 15 of 15
  1. #1
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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 added
    Last edited by Fubarable; 01-16-2011 at 05:36 PM. Reason: Moderator Edit: Code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Print the value of your variables before you go through all those if-else-statements and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sssss View Post
    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.
    Define "does not work at all". Does its actionPerformed get called? Is data obtained from the components of the GUI? What do your debugging statements (println statement) tell you is the problem here?

  5. #5
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sssss View Post
    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.
    Again, have you added println statements? Where have you added them? What do they show is happening?

  7. #7
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, have you added println statements? Where have you added them? What do they show is happening?
    Yes, I have added println statements before the if statements begin, to see if the value of x satisfy the if statements criteria. But the x value does not print as the CandaPensionPlan() is not being called upon.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sssss View Post
    Yes, I have added println statements before the if statements begin, to see if the value of x satisfy the if statements criteria. But the x value does not print as the CandaPensionPlan() is not being called upon.
    Sorry, but I have difficulty believing this, at least not based on the code you have posted so far. Please post your latest code with the debug println statements present and let us test it ourselves.

  9. #9
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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();
    }
    });
    }
    }

  10. #10
    gandrew is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    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();
    }

  11. #11
    aspire1 is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    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.

  12. #12
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by gandrew View Post
    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();
    }
    gandrew, thank you so much. I don't believe how silly I could be.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gandrew View Post
    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.
    Good eyes. 1+ rep.

  14. #14
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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?

  15. #15
    aspire1 is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    Are you sure it isn't?

    Given hourly rate of 38.25, 20 hours, charity 1%:

    Gross Pay = 38.25 * 20 * 2 = 1530
    CPP = 59.92
    Charity = 15.30

    Net Pay = 1530 - 59.92 - 15.30 = 1454.78

    which is the net pay answer your program gives me.

    Only did this one test though.

Similar Threads

  1. Problem with multiple string in classes
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 02:48 PM
  2. uploading applet with multiple classes help
    By alacn in forum New To Java
    Replies: 3
    Last Post: 08-02-2010, 06:37 AM
  3. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM
  4. How to use multiple timer classes in swings
    By theone3nu in forum AWT / Swing
    Replies: 12
    Last Post: 12-30-2008, 02:30 AM
  5. Replies: 0
    Last Post: 08-19-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •