Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement (in Java) a program that simulates a vending machine. Produ

ID: 3591269 • Letter: D

Question

Design and implement (in Java) a program that simulates a vending machine. Products can be purchased by inserting the correct number of coins into the machine. A user selects a product from a list of available products, adds coins, and either gets the product or gets the coins returned if insufficient money was supplied or if the product is sold out. Products can be restocked and money removed by an operator.

If you could please try to make this with many classes in an object oriented way and include javadoc comments that would be helpful, thank you.

Explanation / Answer

//Product.java class

public class Product {

      int product_id,price,stock;

      String product_name;

      static int total_coins_received; // a counter to store the coins received from customers.

      public Product() {

           

      }

      Product(int id,String name,int price,int stock){

            this.product_id=id;

            this.product_name=name;

            this.price=price;

            this.stock=stock;

      }

      public int getProduct_id() { //for getting product ID

            return product_id;

      }

      public void setProduct_id(int product_id) { //for setting product ID

            this.product_id = product_id;

      }

      public int getPrice() { //for getting the price of product in coins

            return price;

      }

      public void setPrice(int price) { //for setting the price of product in coins

            this.price = price;

      }

      public int getStock() { //getting the stock count

            return stock;

      }

      public void setStock(int stock) { //setting/modifying the stock

            this.stock = stock;

      }

      public String getProduct_name() { //getting product name

            return product_name;

      }

      public void setProduct_name(String product_name) { //setting product name

            this.product_name = product_name;

      }

      public void buy(int coins){ //function to buy and dispense a product

            if(getStock()>0 && getPrice()<=coins){ //if stock is not empty and sufficient coins inserted

                  System.out.println("Thanks for purchasing "+getProduct_name()+" , your product is now dispensing");

                  //dispense(); // implement this function in real to dispense the product, not necessary for the time being ;)

                  stock--; // decreasing the stock count

                  total_coins_received+=getPrice(); // increasing the coins counter with the price of the product

                  int tmp=coins-getPrice(); //checking if there's any extra coins left

                  if(tmp>0){

                        return_coins(tmp); // returning the balance coins to the customer

                       

                  }

                 

            } else if(getStock()<=0){

                  System.out.println("Sorry, the product is out of stock");

            }else{

                  System.out.println("Insufficient coins to buy this product...");

                  return_coins(coins); // implement this function in real to return the coins to the customer

            }

      }

     

      public int getTotalCoinsReceived(){ /* a function to retrieve the count of coins received; implement this when the operator withdraw the coins*/

            return total_coins_received;

      }

      public void return_coins(int c){

            System.out.println(c+" coins are dispensing, please take it");

      }

     

     

}

// end of Product.java

//Tester.class

import java.util.Scanner;

public class Tester { //a tester class for checking the functionalities of vending machine user interaction

      Product[] product=new Product[5]; //initializing an array of products that can have 5 different products

      Product selected_product;

      public static void main(String[] arg){

            Tester machine=new Tester();

            machine.addProducts();

            while(true){ //looping the products menu until manual program termination

                  machine.displayProducts();

                  machine.getUserInput();

            }

           

           

      }

      public void addProducts(){ //function to add products (for testing purposes)

            product[0]=new Product(1,"Coca Cola",10,20);    /* adding coca cola drink as a product with ID=1 , price=10 coins & total stock of 20*/

            product[1]=new Product(2,"Pepsi",12,1);

            product[2]=new Product(3,"Red Bull",15,15);

            product[3]=new Product(4,"Water",5,20);

           

            product[4]=new Product(); /*adding a new product and setting the values using setter methods of Product class */

            product[4].setProduct_id(5);

            product[4].setProduct_name("Lime Soda");

            product[4].setPrice(8);

            product[4].setStock(5);

      }

     

      public void displayProducts(){ //function to display products

            if(product==null){

                  System.out.println("No products available"); //no products have added

            }else{

                  System.out.println("Available Products");

                  System.out.println("-------------------");

                  System.out.println("ID Name Price Available Stock");

                  System.out.println("-------------------");

                  for(int i=0;i<product.length;i++){

                        System.out.println(product[i].getProduct_id()+" "+product[i].getProduct_name()+" "+product[i].getPrice()+" "+product[i].getStock());

                  }

                  System.out.println();              

                 

            }

           

      }

      public void getUserInput(){

            if(product!=null){

                  System.out.println("Enter the product id to select a product");

                  try{

                        Scanner scanner=new Scanner(System.in);

                        int ch=scanner.nextInt();

                        for(int i=0;i<product.length;i++){

                              if(ch==product[i].getProduct_id()){

                                    selected_product=product[i];

                              }

                        }

                        if(selected_product==null){

                              System.out.println("Invalid choice,Please try again");

                              getUserInput();

                        }else{

                              System.out.println("your choice: "+selected_product.getProduct_name());

                              System.out.println("Enter the coins");

                              int coins=scanner.nextInt();

                              selected_product.buy(coins);

                        }

                  }catch(Exception e){

                        System.out.println("ERROR,invalid choice");

                        getUserInput();

                  }

            }          

      }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote