Home > Blockchain >  How to store updated value of an integer in java?
How to store updated value of an integer in java?

Time:01-26

Here's my code for vending machine-

import java.util.Scanner;

public class VendingMachine
{
    public static final Scanner sc = new Scanner(System.in);
    public static double totalAmt;
    public static double change;
    public static String food = null;
    public static int stockPC = 2;
    public static int stockCo = 2;
    public static int stockCa = 2;
    
    public static void MainMenu(){
        
         System.out.println("Main menu - "); 
         System.out.println("a - Potato Chips -$1.25");
         System.out.println("b - Cookies - $0.85 ");
         System.out.println("c - Candies - $0.95");
         
         
         
         String input = sc.next().toLowerCase(); 
         change = 0;
         if(input.equals("a")){
            stockPC = stockPC -1;
            change = totalAmt - 1.25;
            food ="Potato chips";
                if(stockPC == 0){
                 System.out.println("Sorry we're out of Potato Chips:(");
                }else{
                    System.out.println("Please take your "  food   "\n");       
                     System.out.println("Here is your change $"   change);
                }

         }else if ( input.equals("b")){
            stockCo = stockCo -1;
            change = totalAmt - 0.85;
            food ="Cookies";
                 if(stockCo == 0){
                 System.out.println("Sorry we're out of Cookies:(");
                }else{
                    System.out.println("Please take your "  food   "\n");       
                     System.out.println("Here is your change $"   change);
                }
         }else if ( input.equals("c")){
            stockCa = stockCa -1;
            change = totalAmt - 0.95;
            food ="Candies";
                 if(stockCa == 0){
                 System.out.println("Sorry we're out of Candies:(");
                }else{
                    System.out.println("Please take your "  food   "\n");       
                     System.out.println("Here is your change $"   change);
                }
         }else{
               System.out.println("Our system only accepts a,b or c");
         }
         
        }
    
    
    public static void main(String[] args){
         System.out.println("Hi! Welcome to Vending Machine!");
         
        
         System.out.println("How many quarters do you have?");
         double noOfQt = sc.nextDouble();
         totalAmt = noOfQt * 0.25;
         
         System.out.println("How many dimes do you have?");
         double noOfDm = sc.nextDouble();
         totalAmt = totalAmt   (noOfDm * 0.1);
         
         System.out.println("How many nickels do you have?");
         double noOfNk = sc.nextDouble();
         totalAmt = totalAmt   (noOfNk * 0.05);
         
         System.out.println("You have = $"   totalAmt);
         
         MainMenu();
            
         }
    }

I having trouble in my MainMenu method. After selecting one of the inputs in my MainMenu method, I want to update the value of 'integers' like change, stockPC, stockCo and stockCo at the end of the method. I don't want to print these value. I just want to use these updated value in my main method.

How can I do it?

CodePudding user response:

where do you instantiate your scanner? It needs to be a part of the main method, and then pass the value from the user to the MainMenu(-put value here-) method. That way -> Scanner would work.

MainMenu(double amount);

Do your calculations in the receiving method and just pass a "generic" double input from the user

Also, it's not a good practice in your case to declare the items in your stock as public static. Remember that it's always a good practice to declare variables as private, and use setters/getters for those.

The data members shouldn't be initialized like you did, but instead:

private int stockPC;
private int stockCo;
private int stockCa;

Use a constructor to define the value of those upon initialisation:

 public VendingMachine(int stockPC, int stockCo, int stock){
 this.stockPC = stockPC;
 this.stockCo = stockCo;
 this.stockCa = stockCa;
}

And finally getters/setters:

public int getStockPC(){
return stockPC;}

public int getstockCo(){
return stockCo;}

public int stockCa(){
return stockCa;}

public void setstockPC(int stockPC){
this.stockPC = stockPC;}

public void stockCo(int stockCo){
this.stockCo = stockCo;}

public void stockCa(int stockCa){
this.stockCa = stockCa;}

I hope that's helpful

CodePudding user response:

I have made a vending machine which never stop serving. :D I have added condition for amount as well if you have lesser amount than price of select option it will show message.

Your code was correct as you needed just few condition was missing and was not correct. what you was wanting was while loop. but I have corrected your conditions and added needed conditions as per my understanding. if you want you can remove these conditions.

import java.util.Scanner;

public class Main
{
public static final Scanner sc = new Scanner(System.in);
public static double totalAmt;
public static double change;
public static String food = null;
public static int stockPC = 2;
public static int stockCo = 2;
public static int stockCa = 2;

public static void MainMenu(){
    
     System.out.println("Main menu - "); 
     System.out.println("a - Potato Chips -$1.25");
     System.out.println("b - Cookies - $0.85 ");
     System.out.println("c - Candies - $0.95");
     
     String input = sc.next().toLowerCase(); 
     change = 0;
     if(input.equals("a")){
        stockPC = stockPC -1;
        //when stockPC is -1 means stock was zero and you want 1 more
        if(stockPC == -1){
            System.out.println("Sorry we're out of Potato Chips:(");
        }else{
            change = totalAmt - 1.25;
            if(change<0){
                System.out.println("You don't have enough amount to buy");
            }else{
                food ="Potato chips";
                System.out.println("Please take your "  food   "\n");       
                System.out.println("Here is your change $"   change);
            }
        }
     }else if ( input.equals("b")){
        stockCo = stockCo -1;
        //when stockCo is -1 means stock was zero and you want 1 more
        if(stockCo == -1){
            System.out.println("Sorry we're out of Cookies:(");
        }else{
            change = totalAmt - 0.85;
            if(change<0){
                System.out.println("You don't have enough amount to buy");
            }else{
                food ="Cookies";
                System.out.println("Please take your "  food   "\n");       
                System.out.println("Here is your change $"   change);
            }
        }
     }else if ( input.equals("c")){
        stockCa = stockCa -1;
        //when stockCa is -1 means stock was zero and you want 1 more
        if(stockCa == 0){
            System.out.println("Sorry we're out of Candies:(");
        }else{
            change = totalAmt - 0.95;
            if(change<0){
                System.out.println("You don't have enough amount to buy");
            }else{
                food ="Candies";
                System.out.println("Please take your "  food   "\n");       
                System.out.println("Here is your change $"   change);
            }
        }
     }else{
           System.out.println("Our system only accepts a,b or c");
     }
    }

public static void main(String[] args){
     System.out.println("Hi! Welcome to Vending Machine!");
     
    
     System.out.println("How many quarters do you have?");
     double noOfQt = sc.nextDouble();
     totalAmt = noOfQt * 0.25;
     
     System.out.println("How many dimes do you have?");
     double noOfDm = sc.nextDouble();
     totalAmt = totalAmt   (noOfDm * 0.1);
     
     System.out.println("How many nickels do you have?");
     double noOfNk = sc.nextDouble();
     totalAmt = totalAmt   (noOfNk * 0.05);
     
     System.out.println("You have = $"   totalAmt);
     
     while(true){
        MainMenu();
     }
        
     }
}
  •  Tags:  
  • Related