Stephan Garland
Stephan Garland

Reputation: 155

Chaining returns through methods in Java

first off, I'm positive this is a really easy answer, and I apologize.

I'm trying to figure out if I can chain a returned value through methods in the same class, using overloaded methods. In the code below, the computeBill(double bookPrice, int quantity) portion is what I'm currently stuck on.

What I want it to do, is be passed the price of the book, and the quantity. (main has an input section that passes outputs to these methods; that part works fine). It will then pass those to moneyConversion(), which does math, converts to BigDecimal, and returns the result as variable bookPriceDisp. I want computeBill() to return that same value back to main for display.

I'm getting cannot find symbol in class Billing. Do I need to make a new object in each method?

public class Billing
{

public static BigDecimal computeBill(double bookPrice)
{
    BigDecimal bookPriceTaxed = new BigDecimal(bookPriceInput*1.08);
    return bookPriceTaxed;
}

public static BigDecimal computeBill(double bookPrice, int quantity)
{
    moneyConversion(bookPrice, quantity);
    return bookPriceDisp;
}

public static void computeBill(double bookPrice, int quantity, double coupon)
{
    System.out.println("Please enter the percentage of your coupon: ");
    double couponInput = keyboard.nextDouble();
    System.out.println("You entered " + couponInput + "%");
    moneyConversion(bookPrice, quantity);
    BigDecimal couponSavings = new BigDecimal(bookPrice * quantity * couponInput);
    System.out.println("Your books will cost $" + ((bookPriceDisp - couponSavings)*1.08));
    System.out.println("Your coupon saved you $" + couponSavings);

}

public static BigDecimal moneyConversion(double bookPrice, int quantity)
{
 //Using BigDecimal to get correct dollars/cents.
 BigDecimal bookPriceDisp = new BigDecimal(bookPrice * quantity);
 bookPriceDisp = bookPriceDisp.setScale(2, BigDecimal.ROUND_HALF_UP);
 return bookPriceDisp;
}

Thank you,

-Stephan

Upvotes: 0

Views: 308

Answers (3)

RizJa
RizJa

Reputation: 2041

There's several problems if I try to compile this issue. As already mentioned, you're trying to reference values that are out of scope. Rather than using those variables, you need to be using the values the methods are returning directly or using a variable to hold them and then using that variable in your calculations. This is a compiling version of your program:

import java.math.*;
import java.util.Scanner;

public class Billing
{
   static Scanner keyboard = new Scanner(System.in);

public static BigDecimal computeBill(double bookPrice)
{
BigDecimal bookPriceTaxed = new BigDecimal(bookPrice * 1.08);
return bookPriceTaxed;
}

public static BigDecimal computeBill(double bookPrice, int quantity)
{
    return moneyConversion(bookPrice, quantity);
}

public static void computeBill(double bookPrice, int quantity, double coupon)
{
    System.out.println("Please enter the percentage of your coupon: ");
    double couponInput = keyboard.nextDouble();
    System.out.println("You entered " + couponInput + "%");
    BigDecimal bookPriceDisp = moneyConversion(bookPrice, quantity);
    BigDecimal couponSavings = new BigDecimal(bookPrice * quantity * couponInput);

     System.out.println("Your books will cost $" + ((bookPriceDisp.subtract(couponSavings)).multiply(new BigDecimal(1.08))));
    System.out.println("Your coupon saved you $" + couponSavings);

}

public static BigDecimal moneyConversion(double bookPrice, int quantity)
{
 //Using BigDecimal to get correct dollars/cents.
 BigDecimal bookPriceDisp = new BigDecimal(bookPrice * quantity);
 bookPriceDisp = bookPriceDisp.setScale(2, BigDecimal.ROUND_HALF_UP);
 return bookPriceDisp;
}
}

Note that you can't use BigDecimal to simply multiple or subtract double values as regular operations. You need to use the methods available in BigDecimal such as subtract() and multiply() to be able to do your calculations.

Hope that helps!

Upvotes: 1

vatsal
vatsal

Reputation: 3943

I think the problem is in your method computeBill(double bookPrice, int quantity). You just need to say return moneyConversion(bookPrice, quantity);

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692013

bookPriceDisp is a local variable of moneyConversion(). A local variable, as its name indicates, is local, and thus not visible/usable outside of the block (i.e. the moneyConversion() method) where it's defined.

What you want is to return the same value of what the moneyConversion() method returns, so:

// assign the result of moneyConversion() to a local variable result
BigDecimal result = moneyConversion(bookPrice, quantity);
// return this local variable
return result;

or simply:

// return the value returned by moneyConversion()
return moneyConversion(bookPrice, quantity);

Upvotes: 4

Related Questions