0

I am trying to pass variables to a function, have it loop through a for loop and pass the new variable to an existing function. For example my original class looks like this

class Calculations {

//declare the variables in the class
double TotalPay;
double Sales;
double Salary;
double Commission;
double SalesTarget;
double AccelFactor;
double SalesIncentive;
double Increment;
double NewAccelFactor;
double TableSales = Sales * .5;

public Calculations(double Sales, double Salary, double Commission, double SalesTarget, double AccelFactor, double SalesIncentive, double Increment) { //calculations function - can be used when processing multiple inputs as well
    this.Salary = Salary;
    this.Sales = Sales;
    this.Commission = Commission;
    this.AccelFactor = AccelFactor;
    this.SalesTarget = SalesTarget;
    this.SalesIncentive = SalesIncentive;
    this.Increment = Increment;

}

public void calculatePay() { //calculate the total pay using the above variables that have been set

    if (Sales < SalesIncentive) {
        TotalPay = Salary + (Sales * Commission);
    }
    if (Sales == SalesIncentive || (Sales > SalesIncentive && Sales < SalesTarget * 2)) {
        TotalPay = Salary + ((Sales * Commission) * AccelFactor);
    }
    if (Sales > SalesTarget * 2) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 2));
    }
    if (Sales > SalesTarget * 3) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 3));
    }
    if (Sales > SalesTarget * 4) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 4));
    }
    if (Sales > SalesTarget * 5) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 5));
    }
    if (Sales > SalesTarget * 6) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 6));
    }
    if (Sales > SalesTarget * 7) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 7));
    }
    if (Sales > SalesTarget * 8) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 8));
    }
    if (Sales > SalesTarget * 9) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 9));
    }
    if (Sales > SalesTarget * 10) {
        TotalPay = Salary + ((Sales * Commission) * (AccelFactor * 10));
    }
}

public double getTotalPay() {
    return TotalPay; //return total pay to use in the output
}

}

My new function looks like this

    class TableOutput { //trying to output the table, but it's not working so well

//declare the variables in the class
double TotalPay;
double Sales;
double Salary;
double Commission;
double SalesTarget;
double AccelFactor;
double SalesIncentive;
double Increment;
double NewAccelFactor;
double TableSales;
double newSales;
double totalSales;
//int j = 0;

public TableOutput(double Sales, double Salary, double Commission, double SalesTarget, double AccelFactor, double SalesIncentive, double Increment) {
    //assign instances of the variables
    this.Salary = Salary;
    this.Sales = Sales;
    this.Commission = Commission;
    this.AccelFactor = AccelFactor;
    this.SalesIncentive = SalesIncentive;
    this.Increment = Increment;
    this.SalesTarget = SalesTarget;
    this.TableSales = Sales * 0.5;
    this.totalSales = TableSales + Sales;
    this.newSales = newSales;

}

public void createTable() {

    Calculations calc2; //create a new calculations instance
    if (Sales < totalSales) {  //while the sales are less than the TableSales then do this         
        for (double i = Sales; i < totalSales; i += 20000) {  //for the Sales that are less than the TableSales, up the Sales by 20k and keep going until it reaches the limit of Sales *.5
            newSales = i;
            System.out.println("Total Sales             Total Compensation\n");
            calc2 = new Calculations(newSales, Salary, Commission, SalesTarget, AccelFactor, SalesIncentive, Increment);
            calc2.calculatePay();
            for (double j=Sales; j < totalSales; j += 20000) { //keep adding 20k to Sales and send it back to get a running total
                System.out.println(newSales + "            " + calc2.getTotalPay());
            }
        }

    }
}

What I am trying to do is add 20k to the sales and have it increment by 20k while passing that new sales amount back to the calculatePay function. What am I missing here? Shouldn't this be doing exactly that?

     for (double i = newSales; i < TableSales; i =+20000) {
                    Calculations calc2;
                    calc2 = new Calculations(newSales, Salary, Commission, SalesTarget, AccelFactor, SalesIncentive, Increment);
                    calc2.calculatePay();

In my main function I am calling on this class for the output in such a manner:

TableOutput Table1 = new TableOutput(Sales, Salary, Commission, SalesTarget, AccelFactor, SalesIncentive, Increment);
            Table1.createTable();

but still no output.

5
  • The statement ` this.Sales = newSales;` doesn't look proper. Where are you initialising newSales? Commented Aug 11, 2015 at 4:29
  • I was setting newSales = to Sales. This was so that it could be incremented without making changes to the original Sales variable. Even with changing it to Sales I am still running into the same problem. Commented Aug 11, 2015 at 4:38
  • 1
    I still don't see it. In the constructor you are assigning Sales to newSales and effectively initialising both to 0. Can you print the value of Sales and newSales before the loop and see what values they hold? Commented Aug 11, 2015 at 4:41
  • Interesting. I modified my code some and get 20k for newSales and Sales is equal to whatever I put in. Commented Aug 11, 2015 at 4:47
  • After going to all the trouble to define i and make sure it's incremented, don't you actually want to do anything with it? Commented Aug 11, 2015 at 4:55

2 Answers 2

3

There are actually quite a few problems with your code.

1) You set TableSales = Sales * .5; on initialization. You don't initialize Sales to anything so it will default to 0. Thus, TableSales = 0 *.5; which means TableSales is always 0, which means your while loop while (Sales < TableSales) will always be false.

Rather than setting TableSales when you first declare it, you could set it in your constructor, like this.

public TableOutput(double Sales, double Salary, double Commission, double SalesTarget, double AccelFactor, double SalesIncentive, double Increment) {
    this.Salary = Salary;
    this.Sales = Sales; //corrected this line as well (although you pass the variable newSales in to your constructor, you named the variable Sales in your constructor)
    this.Commission = Commission;
    this.AccelFactor = AccelFactor;
    this.SalesIncentive = SalesIncentive;
    this.Increment = Increment;
    this.SalesTarget = SalesTarget;
    this.TableSales = Sales * 0.5;
}

2) Your for loop is all kinds of messed up.

For one, you are incrementing it improperly. You say i = +20000; which is merely saying i = 20000 on every iteration of your loop. You want i += 20000, which will add 20000 to i on every iteration.

Also, you are never using i in your for loop anyway. I believe what you are trying to do is increment the newSales value by 20000 every time through the loop, but what your code does currently is passes in 0 to the Calculations constructor every time, because you always pass in the newSales variable, which is always 0 since you never assign a value to it.

I believe what you are trying to accomplish would look more like this.

for (double sales = 0; sales < TableSales; sales += 20000) {
    Calculations calc2;
    calc2 = new Calculations(sales, Salary, Commission, SalesTarget, AccelFactor, SalesIncentive, Increment);
    calc2.calculatePay();
    System.out.println(calc2.getTotalPay());
}

EDIT after OP's code update:

Your while (Sales < TableSales) loop will still never run.

This is because of two things:

First, is because you are passing in 0 for Sales when you call the constructor of your TableOutput class, so TableSales and Sales will both be 0. You would need to start sales out at a non-zero number.

Second, you need to re-think your logic here anyway. When you create a TableOutput instance, you are setting TableSales to half of whatever Sales is. So:

If Sales = 10000, then TableSales = 5000
If Sales = 30000, then TableSales = 15000
...

So Sales will always be greater than TableSales so your while loop will never run.

Sign up to request clarification or add additional context in comments.

1 Comment

What class is your main function in? And where are you setting the values that you are passing in to your TableOutput constructor?
0

One of the problem I see is

Each time in iteration you are creating new object Calculations calc2

You need to do the following:

Calculations calc2;//Place this here
for (double i = newSales; i < TableSales; i =+20000) {
    calc2 = new Calculations(newSales, Salary, Commission, SalesTarget, AccelFactor, SalesIncentive, Increment);
    calc2.calculatePay();
    }

Next problem

i =+20000//incorrect// This is equal to i = 20000

i +=20000//correct// This is equal to i = i + 20000

6 Comments

That seemed to work for the iteration, but I am still not getting any output from the entire class.
@kerinr What do you get? Error? Unexpected output?
No output from this function at all.
Looks like while (Sales < TableSales) is always false. Are you sure, Sales is less than TableSales?
Correct. I said that newSales= Sales and TableSales = Sales*.5. This should have made it so that Sales could catch up to TableSales correct? eventually making it false and kicking it out?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.