Wacky Boss
Wacky Boss

Reputation: 21

Java: How to make a variable's value change outside a switch? (just read the post)

EDIT: Full Code for people who need it: https://hastebin.com/tovazutuni.cs

ok so im working on a big burger shop challenge, but to keep it simple, here is a similair problem im facing:

public class Burger {

private double priceWithAdditions = 5.00;
private double price = 5.00;
private String addition1;
private double addition1Price;

switch(burgersAdditionScanner()){
   case "mayo": 

       this.addition1 = "Mayo";
       this.addition1Price = 0.40;
       this.priceWithAdditions += this.addition1Price;
       System.out.println("Added Additions " + this.addition1 + " for the price of $" +this.addition1Price);

           break;

default: System.out.println("Get Out");

Later down in the code, I have a method to print the receipt, but the values of the variables changed inside the switch do not update outside of it:

public void Receipt() {

    System.out.println("You Bought " + this.name + " burger for the price of $" + this.price + " with the following \nAdditions:");

    if(this.addition1 != null ) {

        System.out.println("Addition " + this.addition1 + " for $" + this.addition1Price);

    }


System.out.println("The final price is " + this.priceWithAdditions); }

Considering the values of addition1 and priceWithAdditions didnt change in the case, addition 1 is null and priceWithAdditions is still 5.00 instead of 5.40. Does anyone know how to make the values of these variables update ourside of the switch. If you can help me Id really appreciate it, please and thank you

Console Output: (There's a scanner so I can type my choices)

    add addition 1

Choose Addition 1
Lettuce  -  $0.50 
Cheese  -   $1.00 
Ketchup  -  $0.20 
Mayo  -   $0.40

mayo

Added Addition Mayonaise for the price of $0.4

Add Additions or none
Add Addition 1 
Add Addition 2 
Add Adition 3 
Add Adition 4 
Receipt

receipt

You Bought Amer's Booty burger for the price of $5.0 with the following 
Additions:
The final price is 5.0
Get Out

Upvotes: 0

Views: 107

Answers (1)

Vikram
Vikram

Reputation: 333

The problem is you are calling Receipt() on the wrong instance of Burger. For all calls to Receipt() (that I'm seeing), you are calling it on a new instance of a Burger, like so:

case "receipt": 
    HealthyBurger burgerReceipt = new HealthyBurger();
    burgerReceipt.Receipt();

burgerReceipt is a new instance of a HealthyBurger, and not the one that you called any of your addition operators on. As such, it will contain the default values of your HealthyBurger. You can fix this, by calling Receipt on the same instance of HealthyBurger that was mutated. For example:

HealthyBurger burger = new HealthyBurger()
burger.addAddition6()
burger.Receipt()

Your code is a bit too messy for me to give you an exact solution to your problem, but you are creating new instances of Burger all over the place, in a Restaurant class which also happens to be a Burger itself. I think if you take the time to read up on basic inheritance/object oriented principles, you should be able to simplify your code significantly.

Upvotes: 2

Related Questions