Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>//Note that you declare this method static: as such it will have no access to object //member fields, only to class fields: these are member fields declared 'static' public static void calculatePrice(){ Scanner userInput = new Scanner(System.in);//Note this has little to do with actually calculating price. int orderNumber; double totalBill = 0;//You a declaring a new variable that will disappear once it goes out of the scope of this method orderNumber = userInput.nextInt(); switch(orderNumber){ case 1: totalBill = totalBill + American; displayMenu(); calculatePrice();//Recursive call! break; //... eliding for brevity case 0: System.out.println("Your total bill will be $"+ totalBill); break; } } //this is the point where all method variables go out of scope, because this 'stack frame' is unloaded. Any subsequent call to this method will not 'remember' any state that was not saved elsewhere. </code></pre> <p>So, note that you're making a recursive call. What this means is that you're <em>re-entering</em> your function. Your 'totalBill' variable is newly instantiated, masking all other previous 'totalBill' variables. Those variables are still around... just inaccessible on lower stack frames. Think of stack frames as a new table, one upon which your function is run. When you call a new function, a new table is placed on top... but you can only see the top table. All other tables are beneath it.</p> <p>By declaring the variable static, what it means is you're setting it aside so all functions in all objects of this type will have access to the same memory space. <code>static</code> variables are available to ALL objects of that class; hence they are often referred to as 'class fields', whereas non-static member variables are referred to as 'object fields'. Your class signature should, in a well-designed world look like:</p> <pre><code>public class RegisterOrder { public double totalBill;//Available to only ONE instance (object) of this class. public void calculatePrice() {//note no 'static' modifier! //some code } } </code></pre> <p>This will probably not work with the way you're currently calling your calculatePrice function, since your current method is <code>static</code>. You'll have to change that to:</p> <pre><code>RegisterOrder order = new RegisterOrder(); order.calculatePrice(); </code></pre> <p>Note, too, that your recursive call means that if you order many, many orders your program will crash. Think of this as stacking the tables too high. In a modern system this is hard to do (lots of memory), but good design demands a loop here:</p> <pre><code>public function getOrder() { //instantiate variables orderNumber = userInput.nextInt(); while (orderNumber != 0) { switch(orderNumber){ case 1: totalBill = totalBill + American; break; case 2: totalBill = totalBill + Expresso; break; case 3: totalBill = totalBill + Latte; break; default: //Error handling is important! break; } displayMenu(); orderNumber = userInput.nextInt(); } System.out.println("Your total bill will be $"+ totalBill); } </code></pre> <p>Note that in this version you are not re-calling your input function. Note, too, that a 'totalBill' variable declared within this function is never unloaded or masked by another call to this function. In truly clean code, you'd separate your input-getting method from your bill-calculating method, but baby steps. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload