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

1. Create a new multi-class Java program which implements a vending machine simu

ID: 3684619 • Letter: 1

Question

1. Create a new multi-class Java program which implements a vending machine simulator whichcontains the following functionality:

A) At program startup, the vending machine is loaded with a variety of products in a variety ofpackaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Alsoincluded is the cost of each item. The program should be designed to easily load a different setof products easily (for example, from a file).Also at program startup, money should be loaded into the vending machine. Money shouldconsist of different monetary objects for the specified currency for example $1 bills, $5 bills,quarters, dimes, etc. Your program should be designed to use different national currencieseasily (for example the Euro). Money should be maintained as paper bills and coins, not justamounts.

B) A menu of commands must be provided. At a minimum the menu should consists of thefollowing commands:1. Display the list of commands

2. Display the vending machine inventory. For each item, this command should result indisplaying a description and current quantity.

3. Display the money currently held in the vending machine.

4. Purchase an item. The result of this selection should be the following actions:

1. Prompt the user to indicate what item to purchase

2. Prompt the user to specify what monetary items are being used for payment (the actualitems for example quarters, dimes, etc.), not a money amount

3. If the user specified enough money to purchase the selected item, the item is purchased(deducted from inventory), supplied money is added to the vending machine, and anychange is returned in the form of monetary items (quarters, dimes, etc.)

.4. If the user did not specify enough money for the selected item, the transaction is abortedwith the supplied money not added to the machine (not accepted) and the product notpurchased (i.e. the state of the vending machine is unchanged).

5. Exit – exits the program displaying a departing message.

2. Additional points to consider:

A) You can use the Java Standard Edition (SE) API library as supplied by Oracle (AKA Sun)except the collection classes other than String and standard arrays (i.e. not ArrayList, Map,Vector, etc.). These other collections will be covered later in the course.

B) When developing complex classes, consider creating a main() method to test them out. Oncetested successfully, delete the main() method.

C) You should generate error messages when appropriate, for example on invalid input values ornot enough money supplied for the selected item to purchase. Exceptions will be covered laterin the course so for this program displaying appropriate messages on the console is fine.

D) Code to input data from the console will be supplied. Java I/O programming will be coveredlater in the course.

Explanation / Answer

Program:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

  import java.util.Collections;

  import java.util.List;

import java.util.ArrayList;

  import java.util.Map;

  public class DrinkMachine

{

private static List<Drink> drinkList = new ArrayList<Drink>();

private static List<Ingredient> ingredientList = new ArrayList<Ingredient>();

public static void main(String[] args)

{

addAllIngredients();

addAllDrinks();

updateCosts();

updateMakeable();

display();

startIO();

}

public static void startIO()

{

  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

  String input = "";

  while(true)

{

try

{

  input = reader.readLine().toLowerCase();

  if(input.equals(""))

{

  continue;

}

else if (input.equals("q"))

{

  System.exit(0);

}

  else if(input.equals("r"))

{

restockIngredients();

  updateMakeable();

}

else if(Integer.parseInt(input) > 0 && Integer.parseInt(input) <= drinkList.size())

{

  makeDrink(drinkList.get(Integer.parseInt(input)-1));

}

   else

   {

throw new IOException();

}

}

catch (Exception e)

{

System.out.println("Invalid selection: " + input + " ");

}

}

}

  public static void display()

{

System.out.println("Inventory: ");

for (Ingredient i : ingredientList)

{

System.out.println(i.getName() + "," + i.getStock() + " ");

}

  System.out.println("Menu: ");

int count = 1;

  for (Drink d : drinkList)

{

System.out.printf("%d,%s,$%.2f," + d.getMakeable() + " ", count, d.getName(), d.getCost());

count++;

}

}

  public static void updateMakeable()

{

   for (Drink d : drinkList)

{

Map<String, Integer> currRecipe = d.getRecipe();

  for (Ingredient i : ingredientList)

{

if (currRecipe.containsKey(i.getName()) && i.getStock() < currRecipe.get(i.getName()))

{

d.setMakeable(false);

break;

}

  d.setMakeable(true);

}

}

}

public static void updateCosts()

{

  for (Drink d : drinkList)

{

double currCost = 0;

  Map<String, Integer> currRecipe = d.getRecipe();

  for (Ingredient i : ingredientList)

{

if (currRecipe.containsKey(i.getName()))

{

  currCost += i.getCost()*currRecipe.get(i.getName());

}

}

d.setCost(currCost);

}

}

  public static void makeDrink(Drink drink)

{

   if(drink.getMakeable())

{

  System.out.println("Dispensing: " + drink.getName() + " ");

  for (Ingredient i : ingredientList)

{

if(drink.getRecipe().containsKey(i.getName()))

{

i.setStock(i.getStock()-drink.getRecipe().get(i.getName()));

}

}

}

else

{

  System.out.println("Out of stock: " + drink.getName() + " ");

}

updateMakeable();

display();

}

public static void restockIngredients()

{

for(Ingredient i : ingredientList)

{

  i.setStock(10);

}

updateMakeable();

display();

}

  public static void addIngredient(Ingredient ingredient)

{

  ingredientList.add(ingredient);

}

  public static void addDrink(String name, String[] recipe)

{

  drinkList.add(new Drink(name, recipe));

}

public static void addAllIngredients()

{

addIngredient(new Ingredient("Coffee", 0.75));

addIngredient(new Ingredient("Decaf Coffee", 0.75));

  addIngredient(new Ingredient("Sugar", 0.25));

addIngredient(new Ingredient("Cream", 0.25));

addIngredient(new Ingredient("Steamed Milk", 0.35));

addIngredient(new Ingredient("Foamed Milk", 0.35));

addIngredient(new Ingredient("Espresso", 1.10));

addIngredient(new Ingredient("Cocoa", 0.90));

addIngredient(new Ingredient("Whipped Cream", 1.00));

  Collections.sort(ingredientList);

}

public static void addAllDrinks()

{

addDrink("Coffee", new String[]{"Coffee", "Coffee", "Coffee", "Sugar", "Cream"});

  addDrink("Decaf Coffee", new String[]{"Decaf Coffee", "Decaf Coffee", "Decaf Coffee", "Sugar", "Cream"});

  addDrink("Caffe Latte", new String[]{"Espresso", "Espresso", "Steamed Milk"});

  addDrink("Caffe Americano", new String[]{"Espresso", "Espresso", "Espresso"});

  addDrink("Caffe Mocha", new String[]{"Espresso", "Cocoa", "Steamed Milk", "Whipped Cream"});

  addDrink("Cappuccino", new String[]{"Espresso", "Espresso", "Steamed Milk", "Foamed Milk"});

  Collections.sort(drinkList);

}

}

Here the below class name is DRINK, this is the other class.

import java.util.HashMap;

import java.util.Map;

  public class Drink implements Comparable<Drink>

{

private Map<String, Integer> recipe = new HashMap<String, Integer>();

private String name;

private double totalCost = 0;

private boolean makeable = false;

public Drink(String name, String[] recipe)

{

this.name = name;

setRecipe(recipe);

}

public int compareTo(Drink drink)

{

return name.compareTo(drink.getName());

}

public void setRecipe(String[] recipe)

{

  for(String s : recipe)

{

if(this.recipe.containsKey(s))

{

  this.recipe.put(s, this.recipe.get(s)+1);

}

else

{

   this.recipe.put(s, 1);

}

}

}

  public void setName(String name)

{

this.name = name;

}

public void setCost(double totalCost)

{

  this.totalCost = totalCost;

}

public void setMakeable(boolean makeable)

{

this.makeable = makeable;

}

  public Map<String, Integer> getRecipe()

{

return recipe;

}

  public double getCost()

{

return totalCost;

}

  public String getName()

{

return name;

}

public boolean getMakeable()

{

  return makeable;

}

}

Here the below class is the Ingredient class

public class Ingredient implements Comparable<Ingredient>

{

  private String name = "";

private double cost = 0.00;

private int stock = 0;

public Ingredient(String name, double cost)

{

  this.name = name;

  this.cost = cost;

  this.stock = 10;

}

public int compareTo(Ingredient ingredient)

{

return name.compareTo(ingredient.getName());

}

  public void setName(String name)

{

  this.name = name;

}

  public void setCost(double cost)

{

this.cost = cost;

}

public void setStock(int stock)

{

this.stock = stock;

}

  public String getName()

{

  return name;

}

public double getCost()

{

return cost;

}

public int getStock()

{

  return stock;

}

}