hi Guys, I am currently coding for a GUI based program that delivers the amount of time a tutor works in minutes and the amount they go paid total, giving them details on the amount of time work total, and wage per hour and if it is below, minimum, or above minimum wage. I currently have to problems and not sure how to work them out.
1. Program does not let me enter more then one set of hours work and wages paid, and also does not show this within the GUI when I press the Enter button. ( using a two dimensional array to store that info)
2. Im not sure how to change the IF statement regarding the minimum wage to reflect,
if average wages per hour is < minimum wage, then below average
- if average wages per hour is ≥ minimum wage AND ≤ minimum wage × 2.00,
then average
- if average wages per hour is > minimum wage × 2.00, then above average

Please see my code below thanks in advance

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Tutorhelp extends JFrame 
{
    private JPanel panel;
    private JLabel TutorTimeLabel; // Time label
    private JTextField timeField; // Field for time info
    private JLabel TutorWageLabel; //Label for Tutor wages
    private JTextField wageField; // Field to enter info for Wage
    private JTextArea textArea;
    private JButton enterButton; //Enter Button in GUI
    private JButton runReportsButton; // Run report in GUI
    private JButton quitButton; // Exit program button
    final int WINDOW_WIDTH = 600; // Window Width
    final int WINDOW_HEIGHT = 600; // Window height
    double [] tutorMin = new double [2];
    double [] tutorWage = new double [2];


    public Tutorhelp()
    {
        setTitle("Tutor Earnings");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);

    }

    private void buildPanel()
    {
        TutorTimeLabel = new JLabel("Tutor Time");
        timeField = new JTextField(10);
        TutorWageLabel = new JLabel ("Tutor Earnings");
        wageField = new JTextField(10);
        textArea = new JTextArea(25,50);
        enterButton = new JButton("Enter");
        enterButton.addActionListener(new EnterButtonListener());
        runReportsButton = new JButton("Run Reports");
        runReportsButton.addActionListener(new runReportsButtonListener());
        quitButton = new JButton("Quit");
        quitButton.addActionListener(new quitButtonListener());
        panel = new JPanel();
        panel.add(TutorTimeLabel);
        panel.add(timeField);
        panel.add(TutorWageLabel);
        panel.add(wageField);
        panel.add(textArea);
        panel.add(enterButton);
        panel.add(runReportsButton);
        panel.add(quitButton);    
    }

    private class EnterButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {

        int count = 0;
        double num1 = 0;
        double num2 = 0;  
        double m = Double.parseDouble(timeField.getText());
        double w = Double.parseDouble(wageField.getText());
        num1 = m ;
        num2 = w ;

        if (num1 <=0)
        {
            JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }else if (num1 > 240)
        {
            JOptionPane.showMessageDialog(null, "You have worked to many mintues this week", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }else{

        }

            for(double i = 0; i< 3; i++)
            {


            }
            if(num2 < 0)
            {
                JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
                        JOptionPane.ERROR_MESSAGE);
            }
            for(int i = 0; i < 3; i++)
            {


                for(count = 1; count <=3; count++){
                }
                tutorMin[0] = m;
                tutorWage[1] = w;

            timeField.setText("");
            wageField.setText("");
            wageField.requestFocus();
            timeField.requestFocus();

                }
        }
}
    private class runReportsButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double average = 0.00d;
            double num1 = 0.00d;
            double num2 = 0.00d;
            double totalMin = 0.0d;
            double totalEarnings = 0.0d;
            final double MIN_WAGE = 7.25;
            double totalWage = 0.0d;
            String wage = "";

            for (int i = 0; i < tutorMin.length; i++)
            {
                totalMin += tutorMin[i];
            }
            for (int j = 0; j < tutorWage.length; j++)
            {
                totalEarnings +=tutorWage[j];
            }
            for (int j = 0; j < tutorWage.length; j++)
            {
                totalWage +=j;
                average = totalEarnings /60;
            }
            if (average > MIN_WAGE)
            {
                wage = "Above Minimum Wage";
            }else if ((average < MIN_WAGE)&&(average > MIN_WAGE))
            {
                wage = "Equal to Minimum Wage";
            }else if (average < MIN_WAGE)
            {
                wage = "Below Minimum Wage";
            }

            String a = "Total mintues are " +totalMin +"\n";
            a+=("Average Earnings are:" + average + "\n");
            a+=("Minimum wage is: " +wage + "\n");
            textArea.setText(a);

            }}
    private class quitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
        public static void main (String[] args)
        {
            Tutorhelp th = new Tutorhelp();

        }
        }

What do the if statements do incorrectly? What happens when the code executes?
Can you show the output and explain what is wrong with it?

Can you explain how the program is supposed to work? What does the program do, what does the user do and what does the program then do?

I sure can. Currently the IF statement works however I am missing something:

wage = "Above Minimum Wage";
            }else if ((average < MIN_WAGE)&&(average > MIN_WAGE))
            {
                wage = "Equal to Minimum Wage";
            }else if (average < MIN_WAGE)
            {
                wage = "Below Minimum Wage";
            }

and the requirements are:
if average wages per hour is < minimum wage, then below average
- if average wages per hour is ≥ minimum wage AND ≤ minimum wage × 2.00,
then average
- if average wages per hour is > minimum wage × 2.00, then above average
the bit that holds me up there, is how do i Change to code to reflect that requirement.

here is a picture before enter is pressed [IMG]http://i47.tinypic.com/dyaj3t.jpg[/IMG]
here is a picture after enter is pressed [IMG]http://i50.tinypic.com/mhd4qs.jpg[/IMG]
here is a pic after the report is run [IMG]http://i45.tinypic.com/nxiky.jpg[/IMG]

As you can see, Once enter is pressed, the program doesnt display in the TextArea what is entered, and also if i type more then one set of mintues and wages, it forgets the first set.

The code has several useless loops in it. Why are they still there? Why are there for loops that go around 3 times and do the same thing each time around the loop?

however I am missing something:

Please explain what the code does incorrectly in those if/else if statements. I don't see the problem there.

if i type more then one set of mintues and wages, it forgets the first set.

Where is the data that the user enters supposed to be stored? How does the code keep track of how much data the user has entered?

To answer the IF else statement question, I am not sure if the IF statement i wrote is the same as the requirements, maybe I should of explained that better..

Also im supposed to be using two-dimensional array to store the information after I hit enter and im not sure how to implement, by the looks of it.

What goes in each dimension of the array? If you talk of the array as having rows and columns,
what goes in the columns of a row? What goes in each row?
To access a row you will need an index variable to keep track of which row you are working with.
Depending on what goes in the columns, you may need an index for them also.

In the first column will be mintues and the second will be tutor wage:

    60.00      50.00
    100.00     200.00

So column 1 is minutes worked and column 2 is the wage.
What are in the rows?

The individual workings for example if they tutored two different people on the same day, the first for 60mins getting paid 50dollars and then second person working for 100mins getting paid 200.00.

So the rows are for each tutor and there can be many tutors? How many diferent tutors can there be? How does the user indentify which tutor the data is for?

I'm sorry I think you misunderstood, its one tutor, who tutor's many students. so they are entering there own information. So my example above would be the same tutor who worked twice on that day once for 60 mintues and then once for a 100 minutes

Then the rows are for each student.
When you define the array, you will need to know the maximum number of students to be able to define an array large enough to hold the data.

The rows are for each student, and the maximum is 5.

OK. Now define the array and write code to get the user's input and put it into that array.

is this start even in the ball park? and where within the code do i want to enter it, ive never really used an 2d array like this before.....

public class wageInput
{
   public static void main(String[] args)
   {
       double [ ] [ ] wages = new double [ 2 ] [ 5 ] ; //create memory space for entire matrix

       // Fill the matrix with user input
       int row, column;
       double sum;

Normally rows are the first dimension and columns the second: [5][2]
The index variable for the rows could be named for the data it contains. Something like studentIdx or thisStudent. You don't need an index for the columns since their locations are set: first is minutes, second wages.

So basicly its going to look like

public class wageInput
{
   public static void main(String[] args)
   {
       double [ ] [ ] wages = new double [ 5 ] [ 2 ] ; //create memory space for entire matrix
       // Fill the matrix with user input
       int row, column;
       double sum;
       for (row= 0;row++ )
       {
       sum =0;
       for(column= 0;column++ )
       {
            sum[row][column] =

This is me guessing I know there is more to write, but this seems overly complicated for some reason, I cant wrap my head around this for some reason

You won't need a loop or indexes for the columns. You will directly index the two columns to store what goes in each column. The row index will change with the data for each student.
Use a variable name like I suggested for keeping track of the student's data.

So i gather this is where i need to change the coding within the Enter button section

{

    public void actionPerformed(ActionEvent e)
    {

    int count = 0;
    double num1 = 0;
    double num2 = 0; 
    double m = Double.parseDouble(timeField.getText());
    double w = Double.parseDouble(wageField.getText());
    num1 = m ;
    num2 = w ;

    if (num1 <=0)
    {
    JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
    JOptionPane.ERROR_MESSAGE);
    }else if (num1 > 240)
    {
    JOptionPane.showMessageDialog(null, "You have worked to many mintues this week", "Error",
    JOptionPane.ERROR_MESSAGE);
    }else{

    }

    for(double i = 0; i< 3; i++)
    {

    }
    if(num2 < 0)
    {
    JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
    JOptionPane.ERROR_MESSAGE);
    }
    for(int i = 0; i < 3; i++)
    {

    for(count = 1; count <=3; count++){
    }
    tutorMin[0] = m;
    tutorWage[1] = w;

    timeField.setText("");
    wageField.setText("");
    wageField.requestFocus();
        timeField.requestFocus();

    }
    }
    }

I understand the array has to go in here somewhere, but i have tried 8 different variation of this piece of code and no a single one works. I dont understand this at all.

Is this what i want to put in there instead

What is the code on lines 26, 35 and 38 supposed to do? Why are those lines of code in the program?

Where is the two dim array that the data is supposed to be saved in? You need to define a two dim array as discussed earlier. Then you need to use an index that points to the row that will hold the next student's data. Something like this:

 final int minsIdx = 0;  //  define index for minutes
 final int wagesIdx = 1; // define index for wages
 ....

 //  save data for next student
 studentData[nextStudent][minsIdx] = mins;
 studentData[nextStudent][wagesIdx] = wages;
 nextStudent++;  //  move index to next student's slot

Thank you for the information. i actually the Enter button to work now to work on the report side of things!

Okay so i have re-worked my code, everything functions except I still for some reason cannot add more then one student data. Always does the latest one i hit enter for. here is the full code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


class Tuthelp extends JFrame {

    private JPanel panel;
        private JLabel TutorTimeLabel; // Time label
    private JTextField timeField; // Field for time info
    private JLabel TutorWageLabel; //Label for Tutor wages
    private JTextField wageField; // Field to enter info for Wage
    private JTextArea textArea;
    private JButton enterButton; //Enter Button in GUI
    private JButton runReportsButton; // Run report in GUI
    private JButton quitButton; // Exit program button
    final int WINDOW_WIDTH = 600; // Window Width
    final int WINDOW_HEIGHT = 600; // Window height
    //double [] tutorMin = new double [2];
    //double [] tutorWage = new double [2];
    double [][] studentData = new double [20][2];

        public Tuthelp()
    {
    setTitle("Tutor Earnings ");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
}


        public void buildPanel(){


        TutorTimeLabel = new JLabel("Tutor Time");
    timeField = new JTextField(10);
    TutorWageLabel = new JLabel ("Tutor Earnings");
    wageField = new JTextField(10);
    textArea = new JTextArea(25,50);
    enterButton = new JButton("Enter");
    enterButton.addActionListener(new Tuthelp.enterButtonListener());
    runReportsButton = new JButton("Run Reports");
    runReportsButton.addActionListener(new Tuthelp.runReportsButtonListener());
    quitButton = new JButton("Quit");
    quitButton.addActionListener(new quitButtonListener());
    panel = new JPanel();
    panel.add(TutorTimeLabel);
    panel.add(timeField);
    panel.add(TutorWageLabel);
    panel.add(wageField);
    panel.add(textArea);
    panel.add(enterButton);
    panel.add(runReportsButton);
    panel.add(quitButton); 
    }

    private class enterButtonListener implements ActionListener
    {

    public void actionPerformed(ActionEvent e)
        {
        int nextStudent=0;
    double minsIdx = Double.parseDouble(timeField.getText());
        double wagesIdx= Double.parseDouble(wageField.getText());     

        studentData[nextStudent][0] = minsIdx;        
        studentData[nextStudent][1] = wagesIdx;

        nextStudent++;


    if (minsIdx <=0)
    {
    JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
    JOptionPane.ERROR_MESSAGE);
    }else if (minsIdx > 240)
    {
    JOptionPane.showMessageDialog(null, "You have worked to many mintues this week", "Error",
    JOptionPane.ERROR_MESSAGE);
    }else{
    if(wagesIdx < 0)
    {
    JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
    JOptionPane.ERROR_MESSAGE);
    }


    timeField.setText("");
        wageField.setText("");
    wageField.requestFocus();
        timeField.requestFocus();

    }
    }
    }
        private class quitButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }
    private class runReportsButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        int cols=2;
        int rows=studentData[0].length;
        double average = 0.00d;
    double num1 = 0.00d;
    double num2 = 0.00d;
    double totalMin = 0.0d;
    double totalEarnings = 0.0d;
    final double MIN_WAGE = 7.25;
    double totalWage = 0.0d;
    String wage = "";

    wage+=
"****************************************************\n\n";
        wage+= "Raw Tutoring Earnings Data \n\n";
        wage+= "\n";
        wage+= "Mintues       Earnings\n";

        for(int i = 0; i< rows;i++){
            for(int j =0; j <cols; j++ ){
               wage += studentData[i][j];
               wage += "              ";

               if (j == 0) {
                   totalMin += studentData[i][j];
               }
               else if (j == 1){
                   totalWage += studentData[i][j];
            }
        }
            wage+= "\n";
        }

        if (studentData[0].length >0 ) {
            average = totalWage / studentData[0].length;
        }

        wage +=
"************************************************************************\n\n";
        wage += "Reports of your wages to Date:\n";
        wage += "_____________________________\n\n";
        wage += "Total Mintues spent tutoring = " + totalMin + "\n";
        wage += "Total Earnings = $" + totalWage + "\n";
        wage += "Average Per Hour = $ " + average + "\n";
        wage += "Minimum wage = $" + MIN_WAGE + "per hour" + "\n";

        wage += "Your average wage are ";

        if (average < MIN_WAGE){
            wage +=" Below Average";
            } else if (average >= MIN_WAGE && average <= MIN_WAGE * 2.00) {
                    wage += "Average";
            } else if (average > MIN_WAGE *2.00){
                wage += "Above Average";
            }
        textArea.setText(wage);
        }}
        public static void main (String[] args)
        {
            Tuthelp th = new Tuthelp();
        }
}

Can you see the problem?

Where do you initialize the index into the studentData array? It should be done only ONCE per data collection cycle!!!

72: nextStudent++;

You have the code to update nextStudent correctly, but there ate two related problems that prevent that working as you expect:
1. Every time you enter your actionPerformed the first thing you do is to set nextStudent back to 0
2. You declare nextStudent inside the actionPerformed method, so its just a local variable that gets discarded after each execution of the method. and a new one is created each time you enter the method, so it cannot possiby carry the value forward from one call to the next.

Okay so i need to modifiy the int nextStudent=0 correct?

That statement needs to be outside of any method, at the class level, so that the variable retains its value across calls to any methods.

Thank you, i just realized it was inside the method after i said that! this was a pain in the ass. but thank you Norm and James as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.