1. Create a new multi-class Java program which implements a vending machine simu
ID: 3684904 • 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
import java.util.ArrayList;
public class VendingMachine {
private ArrayList<Product> availableProducts;
private ArrayList<Coin> insertedCoins;
public VendingMachine()
{
availableProducts = new ArrayList<Product>();
insertedCoins = new ArrayList<Coin>();
}
public void addCoin(Coin c)
{
insertedCoins.add(c);
}
public ArrayList<Coin> selectProduct(Product product)
{
return null;
}
public ArrayList<String> getProductListNames()
{
ArrayList<String> items = new ArrayList<String>();
for(int i = 0; i < availableProducts.size(); i++ )
{
Product p = availableProducts.get(i);
items.add(p.getName());
}
return items;
}
public void addProduct(Product product)
{
availableProducts.add(product);
}
public double getInsertedCoinValue()
{
return 0.0;
}
}
For Products:
public class Product {
private String name;
private double price;
private int quantity;
public Product()
{
name = null;
price = 0;
}
public void setName(String n)
{
name = n;
}
public void setPrice(double p)
{
price = p;
}
public void setQuantity(int q)
{
quantity = q;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
}
A...
import java.util.Scanner;
public class VendingMachineSimulation {
public static void main(String[] args) {
Product p = new Product();
VendingMachine v = new VendingMachine();
Scanner in = new Scanner(System.in);
boolean done = false;
while(!done)
{
System.out.println("S)how products I)nsert coin B)uy A)dd product R)emove coin Q)uit");
String input = in.next();
if(input.equalsIgnoreCase("Q"))
{
System.out.println("bye");
done = true;
}
else if(input.equalsIgnoreCase("A"))
{
System.out.print("Description: ");
String item = in.next();
p.setName(item);
v.addProduct(p);
}
}
System.out.println(v.getProductListNames());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.