Search code examples
javamethodsparametersstaticreturn

Passing returned variables to other static methods for calculations


Good evening all,

I have to create a program that utilizes two classes. One that will contain the main method for output and the other class will be for static methods to call to the main method. I'm getting a little better at this, but the problem now is passing returned variables in one static method to another static method to be used in calculations. Now I could be phrasing that incorrectly, if so let me know.

So far, I have created two of the four methods. The methods will input a total, the second method will consider what the commission rate will be using an if, else statement, the third method will be used to calculate the commission amount using the rate determined in the previous method, and the fourth will display the results.

The problem I get is in the first static method in the second class, it will display the question and I can enter the amount. However, the input does not transfer to the next method although I have it passing as an argument to the second method. I feel like once I understand why it is not passing through I shouldn't have a problem with composing the rest of the program. The main class is called TapCoCommission. The class with the static methods is TapCoCommissionCalc (Confusing, teacher wanted these named this way though.)

I am thinking it has something to do with either the way the return value is set up or the way the parameters have to be. Any clarification is much appreciated!

//Main class begins here
import java.util.Scanner;
import static java.lang.System.out;

public class TapCoCommission
{
    public static final int COMMISSION_2 = 2;
    public static final int COMMISSION_5 = 5;
    public static void main(String[] args)
    {
        double inputTotalMonthlySales = 0;
        double commRate = 0;

        TapCoCommissionCalc.inputTotalSales(inputTotalMonthlySales);   
        TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);


    }
}

// Class with static methods begin here

import java.util.Scanner;
import static java.lang.System.out;

public class TapCoCommissionCalc 
{

    public static final double COMMISSION_2 = 0.02;
    public static final double COMMISSION_5 = 0.05;

    public static double inputTotalSales(double inputTotalMonthlySales)
    {
        Scanner keyboard = new Scanner(System.in);      
        out.println("Enter the total sales for this month.");
        inputTotalMonthlySales = keyboard.nextDouble(); 
        return inputTotalMonthlySales;
    }

    public static double setCommRate(double inputTotalMonthlySales)
    {
        double commRate = 0;

        if (inputTotalMonthlySales <= 10000)
        {
            commRate = COMMISSION_2;
            return commRate;
        }

        else if (inputTotalMonthlySales > 10000)
        {
            commRate = COMMISSION_5;
            return commRate;
        }

        else
        {
            out.println("Please enter amount a positive numerical amount in dollars and cents.");

        }

    return commRate;
    }

    public static double calcCommAmount(double commRate, double inputTotalMonthlySales)
    {
        double commAmount = commRate * inputTotalMonthlySales;

        return commAmount;
    }

    public static void displayCommResults()
    {

    }
}

Solution

  • You don't need to pass inputTotalMonthlySales into the first method, only the second one. In fact, inputTotalMonthlySales is the value you get out of the first method.

    So you should change main to have lines like this.

        double inputTotalMonthlySales = TapCoCommissionCalc.inputTotalSales();   
        double commRate = TapCoCommissionCalc.setCommRate(inputTotalMonthlySales);
    

    And you should change inputTotalSales to look like this.

    public static double inputTotalSales(){
        Scanner keyboard = new Scanner(System.in);      
        out.println("Enter the total sales for this month.");
        double  inputTotalMonthlySales = keyboard.nextDouble(); 
        return inputTotalMonthlySales;
    }
    

    The idea is that when you have a return in the method that's being called, then that's the value that's available in the calling method. You can assign that value to a variable, when you write

    something = method call ( .... );