Variable doesn't decrease its value by one

Advertisements

I am currently trying to create an application in which I am trying to recreate a juice store, where people can order something.
The amount of left juices is 3 and with every purchased juice the amount decreases by one and form some reason that exact decrease doesn’t work.

Hope you can help me…

I have created two classes:

  1. Program, in which I have given 3 orders:
public class Program {

    public static void main(String[] args) {
        
        
        JuiceStore Juice1 = new JuiceStore(14);
        JuiceStore Juice2 = new JuiceStore(7);
        JuiceStore Juice3 = new JuiceStore(17);
        
        try {
            Juice1.buyJuice();
            
        }
        catch(NoJuiceException e) {
            System.out.println();
            System.out.println(e.getMessage());
        }
        catch(TooColdException e) {
            System.out.println(e.getMessage());
        }
        catch(TooWarmException e) {
            System.out.println("The juice is too warm.");
        }
        
        try {
            Juice2.buyJuice();
            
        }
        catch(NoJuiceException e) {
            System.out.println();
            System.out.println(e.getMessage());
        }
        catch(TooColdException e) {
            System.out.println(e.getMessage());
        }
        catch(TooWarmException e) {
            System.out.println(e.getMessage());
        }
        
        try {
            Juice3.buyJuice();
            
        }
        catch(NoJuiceException e) {
            System.out.println();
            System.out.println(e.getMessage());
        }
        catch(TooColdException e) {
            System.out.println(e.getMessage());
        }
        catch(TooWarmException e) {
            //e.getMessage();
            System.out.println(e.getMessage());
        }
    }
}

2.JuiceStore, in which I have declared the purchase method:

public class JuiceStore {

    private int temperature;
    private int leftJuices = 3;
    
    JuiceStore(int temperature) {
        this.temperature = temperature;
    }
    
    public void buyJuice() throws NoJuiceException, TooColdException, TooWarmException  {
        if(this.leftJuices < 1) throw new NoJuiceException("Unfortunately, there is no juice left. Come back tomorrow.");
        
        this.leftJuices = leftJuices-1;
        System.out.println();
        System.out.println("You have bought a juice, there are " + this.leftJuices + " left.");
        if (this.temperature < 9) throw new TooColdException("The juice is too cold.");
        if (this.temperature > 15)throw new TooWarmException("The juice is too warm.");
        System.out.println("Drink successful.");
    }
    
    
}

>Solution :

You’re making 3 separate instances of JuiceStore, all with their own leftJuices counter. When you’re done with all of them, all their leftJuices variables will be at 2.

If you want to share a variable between instances, make it static, or just make one JuiceStore total.

Leave a ReplyCancel reply