Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable doesn't decrease its value by one

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading