Search code examples
javastore

How to store updated value of an integer in java?


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?


Solution

  • 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