Create A Vending Machine Program. A user selects a product from a list of availa
ID: 3722342 • Letter: C
Question
Create A Vending Machine Program. 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. I have attached the code and compared it with the solution provided from The textbook Solutions. Problem 2.18 from Object Oriented design and Patterns 2nd edition.
LANGUAGE MUST BE JAVA. NOTHING COMPLICATED , VERY BASIC JAVA PLEASE
I HAVE COPIED AND PASTED ALL MY CURRENT WORK JUST GETTING SOME MINOR ERRORS COULD YOU PLEASE FIX IT THANKS
---VendingMachineMain,java---
public class VendingMachineMain
{
public static void main(String args[]) {
// Create an Object of the VendingMachine class
VendingMachine machine = new VendingMachine();
MachineUI u = new MachineUI(machine);
u.domainMenu();
}
}
---VendingMachineItem,java---
public class VendingMachineItem {
private String name;
private int cost;
private int quantity;
private static final int RESTOCK_AMOUNT = 10;
// Overloaded Constructor
public VendingMachineItem(String name, int cost) {
this.name = name;
this.cost = cost;
this.quantity = RESTOCK_AMOUNT;
}
// Accessor Functions
// Retrieves Name of the Item
public String getName() {
return name;
}
// Retrieves cost of Item
public int getCost() {
return cost;
}
// Retrieves The Quantity of the item
public int getQuantity() {
return quantity;
}
// Allows the Item To be restocked
public void restock() {
quantity = RESTOCK_AMOUNT;
}
// Keeps track of Quantity
// When User buys it deducts
public void dispense() {
quantity = quantity - 1;
}
System.out.println("The Item You Ordered is " + this.getName() + " Has Successfully arrived");
}
---VendingMachine.java---
import java.util.ArrayList;
public class VendingMachine {
private ArrayList itemList = new ArrayList();
// Object for the Moneysafe
private MoneyBox box = new MoneyBox();
// Default constructor
public VendingMachine() {
// Inserting All the Items Into Vending Machine
itemList.add(new VendingMachineItem(" Odwalla Berries GoMega", 2));
itemList.add(new VendingMachineItem(" Odwalla BlueBerry Monster", 2));
itemList.add(new VendingMachineItem(" Odwalla Citrus C Monster", 2));
itemList.add(new VendingMachineItem(" Odwalla Mango Tango", 2));
itemList.add(new VendingMachineItem(" Odwalla Red Rhapsody", 2));
itemList.add(new VendingMachineItem(" Odwalla Strawberry Banana", 2));
itemList.add(new VendingMachineItem(" Alo Drink", 2));
itemList.add(new VendingMachineItem(" PitayaPlus Super Juice", 2));
itemList.add(new VendingMachineItem(" Mamma Chia", 2));
itemList.add(new VendingMachineItem(" KeVita", 2));
itemList.add(new VendingMachineItem(" Lipton Iced Tea", 2));
itemList.add(new VendingMachineItem(" Lipton Iced Coffee", 2));
itemList.add(new VendingMachineItem(" Gatorade Lemon Lime", 2));
itemList.add(new VendingMachineItem(" Gatorade Watermelon Blast", 2));
itemList.add(new VendingMachineItem(" Gatorade Fruit Punch", 2));
itemList.add(new VendingMachineItem(" Water From Dasani", 2));
}
public void listItems() {
// for loop variable
int i;
// Display All Information of the Products
// Such As Product Name, Price, Quantity
for (VendingMachineItem x: itemList) {
System.out.println("Item" + (i + 1) + " " + x.getName() + "Cost" + x.getCost() +
" Quarters " + " Quantity Available " + x.getQuantity() + " ");
i++;
}
// Will Show User How Many Quarters Inserted
System.out.println(" You Have Inserted " + box.getAmount() + "Quarter(s) ");
//}
public boolean isSelectionValid(int i) {
return 1 <= i && i <= itemList.size();
}
// Will Completely Withdraw All Funds from Vending Machine
public void emptyMoneyBox() {
box.removeCoins();
}
// Used to transfer Amount Of quarters into Money Safe
public void addcoin() {
box.addCoin();
}
// This Method Responsible for Restocking Merchandise
public void restock() {
for (VendingMachineItem x: itemList) {
x.restock();
}
System.out.println(" All Products Have been restocked and Are Now Available ");
}
// This Function responsible for Allowing the User to
// Select a product from the Vending Machine
public void selectItem() {
if (selection >= 1 && selection <= itemList.size()) {
VendingMachineItem i = itemList.get(selection - 1);
if (box.getAmount() < i.getCost()) {
System.out.println(" Insufficient Funds!");
// Function Call To Return Funds to User
box.returnCoins();
} else if (i.getQuantity() == 0) {
System.out.println(" This Product is Currently Out ");
// Return The funds back to the user due to inavailability
box.returnCoins();
}
// Otherwise Keep the coins and Display Product
// Decrement the Product
else if {
box.keepCoins();
i.dispense();
}
} else {
System.out.println("Not A Valid Selection!");
}
}
---MoneyBox.Java----
public class MoneyBox {
// This will be for User to check
// How much he has currently
int keptAmount ;
// This Will Store the Overall Amount
// the Machine has gathered
int holdingAmount ;
public int getAmount() {
return holdingAmount;
}
public void addCoin() {
holdingAmount++ ;
System.out.println(" You Current Balance is Now " + holdingAmount +" Quarters");
}
public void returnCoins() {
System.out.print(" The Machine Will Now Return You your Amount of ");
System.out.print(holdingAmount);
System.out.println(" Quarter(s) ");
}
// Method Will Accumulate OverAll Amount
public void keepCoins() {
keptAmount = keptAmount + holdingAmount ;
// Display Total Amount
System.out.print(" The Total Balance In the Vending Machine Is $ " );
System.out.println(keptAmount);
// Inititalize the Amount to 0
holdingAmount = 0 ;
}
public void removeCoins() {
keptAmount = keptAmount + holdingAmount ;
// Inititalize the Amount to 0
holdingAmount = 0 ;
System.out.print( "The System Is Going to Release ");
System.out.print( keptAmount);
System.out.println("Quarter(s)");
// Set the Total To 0 After Withdrawl
keptAmount = 0 ;
}
}
-----MachineUI.java-----
import java.util.Scanner;
public class MachineUI {
// Object Created
private VendingMachine machine;
// Scanner Object for User input
private Scanner user_input = new Scanner(System.in);
// Overloaded constructor
// Accepts an Object from VendingMachine Class
public MachineUI(VendingMachine machine) {
this.machine = machine;
}
//
public void domainMenu() {
VendingMachine m = new VendingMachine();
// Display Message to User
System.out.println(" Greetings! Welcome to The Best Vending Machine EVER! ");
System.out.println(" We Are The Only Vending Machine That cares for your Health");
System.out.println(" We ONLY Accept Quarters! ");
// Take User Input For Menu Selection
boolean loop_variable = true;
while (loop_variable = true) {
System.out.println(" Please Select an Option from The menu Below ");
System.out.println(" Option 1: Insert Quarter ");
System.out.println(" Option 2: Display Products ");
System.out.println(" Option 3: Make A Product Selection ");
System.out.println(" Option 4: Remove Money (For Operator Use ONLY)");
System.out.println(" Option 5: Restock Merchandise (For Operator Use ONLY) ");
System.out.println(" Option 6: Abort Vending Machine ");
// Take User Input Option
int x = user_input.nextInt();
// Insert Quarter case
if (x == 1) {
this.addCoin();
}
// Display Products to user Case
else if (x == 2) {
machine.listItems();
}
// Make A Product Selection Case
else if (x == 3) {
this.selectItem();
}
// Remove Money From Machine Case
else if (x == 4) {
machine.emptyMoneyBox();
}
// Restock Merchandise case
else if (x == 5) {
machine.restock();
}
Abort Vending Machine
else if (x == 6) {
machine.abortVendingMachine();
}
// For an Invalid Entry
else {
System.out.println(" Sorry There was An Error In Processing Your Request!");
System.out.println(" Please retry Selecting something from the menu. ");
}
}
}
// Add Quarter method
void addCoin() {
this.addCoin();
}
void displayProducts() {
machine.listItems();
}
void selectItem() {
machine.selectItem();
}
}
Explanation / Answer
here is your modified program : --------->>>>>>>>>
public class VendingMachineMain
{
public static void main(String args[]) {
// Create an Object of the VendingMachine class
VendingMachine machine = new VendingMachine();
MachineUI u = new MachineUI(machine);
u.domainMenu();
}
}
---------------------------
public class VendingMachineItem {
private String name;
private int cost;
private int quantity;
private static final int RESTOCK_AMOUNT = 10;
// Overloaded Constructor
public VendingMachineItem(String name, int cost) {
this.name = name;
this.cost = cost;
this.quantity = RESTOCK_AMOUNT;
}
// Accessor Functions
// Retrieves Name of the Item
public String getName() {
return name;
}
// Retrieves cost of Item
public int getCost() {
return cost;
}
// Retrieves The Quantity of the item
public int getQuantity() {
return quantity;
}
// Allows the Item To be restocked
public void restock() {
quantity = RESTOCK_AMOUNT;
}
// Keeps track of Quantity
// When User buys it deducts
public void dispense() {
quantity = quantity - 1;
System.out.println("The Item You Ordered is " + this.getName() + " Has Successfully arrived");
}
}
---------------------------------------------------
import java.util.ArrayList;
public class VendingMachine {
private ArrayList<VendingMachineItem> itemList = new ArrayList<>();
// Object for the Moneysafe
private MoneyBox box = new MoneyBox();
// Default constructor
public VendingMachine() {
// Inserting All the Items Into Vending Machine
itemList.add(new VendingMachineItem(" Odwalla Berries GoMega", 2));
itemList.add(new VendingMachineItem(" Odwalla BlueBerry Monster", 2));
itemList.add(new VendingMachineItem(" Odwalla Citrus C Monster", 2));
itemList.add(new VendingMachineItem(" Odwalla Mango Tango", 2));
itemList.add(new VendingMachineItem(" Odwalla Red Rhapsody", 2));
itemList.add(new VendingMachineItem(" Odwalla Strawberry Banana", 2));
itemList.add(new VendingMachineItem(" Alo Drink", 2));
itemList.add(new VendingMachineItem(" PitayaPlus Super Juice", 2));
itemList.add(new VendingMachineItem(" Mamma Chia", 2));
itemList.add(new VendingMachineItem(" KeVita", 2));
itemList.add(new VendingMachineItem(" Lipton Iced Tea", 2));
itemList.add(new VendingMachineItem(" Lipton Iced Coffee", 2));
itemList.add(new VendingMachineItem(" Gatorade Lemon Lime", 2));
itemList.add(new VendingMachineItem(" Gatorade Watermelon Blast", 2));
itemList.add(new VendingMachineItem(" Gatorade Fruit Punch", 2));
itemList.add(new VendingMachineItem(" Water From Dasani", 2));
}
public void listItems() {
// for loop variable
int i = 0;
// Display All Information of the Products
// Such As Product Name, Price, Quantity
for (VendingMachineItem x: itemList) {
System.out.println("Item" + (i + 1) + " " + x.getName() + "Cost" + x.getCost() +
" Quarters " + " Quantity Available " + x.getQuantity() + " ");
i++;
}
// Will Show User How Many Quarters Inserted
System.out.println(" You Have Inserted " + box.getAmount() + "Quarter(s) ");
//
}
public boolean isSelectionValid(int i) {
return 1 <= i && i <= itemList.size();
}
// Will Completely Withdraw All Funds from Vending Machine
public void emptyMoneyBox() {
box.removeCoins();
}
// Used to transfer Amount Of quarters into Money Safe
public void addCoin() {
box.addCoin();
}
// This Method Responsible for Restocking Merchandise
public void restock() {
for (VendingMachineItem x: itemList) {
x.restock();
}
System.out.println(" All Products Have been restocked and Are Now Available ");
}
// This Function responsible for Allowing the User to
// Select a product from the Vending Machine
public void selectItem(int selection) {
if (selection >= 1 && selection <= itemList.size()) {
VendingMachineItem i = itemList.get(selection - 1);
if (box.getAmount() < i.getCost()) {
System.out.println(" Insufficient Funds!");
// Function Call To Return Funds to User
box.returnCoins();
} else if (i.getQuantity() == 0) {
System.out.println(" This Product is Currently Out ");
// Return The funds back to the user due to inavailability
box.returnCoins();
}
// Otherwise Keep the coins and Display Product
// Decrement the Product
else{
box.keepCoins();
i.dispense();
}
} else {
System.out.println("Not A Valid Selection!");
}
}
}
-------------------------=---------------------------
public class MoneyBox {
// This will be for User to check
// How much he has currently
int keptAmount ;
// This Will Store the Overall Amount
// the Machine has gathered
int holdingAmount ;
public int getAmount() {
return holdingAmount;
}
public void addCoin() {
holdingAmount++ ;
System.out.println(" You Current Balance is Now " + holdingAmount +" Quarters");
}
public void returnCoins() {
System.out.print(" The Machine Will Now Return You your Amount of ");System.out.print(holdingAmount);
System.out.println(" Quarter(s) ");
}
// Method Will Accumulate OverAll Amount
public void keepCoins() {
keptAmount = keptAmount + holdingAmount;
// Display Total Amount
System.out.print(" The Total Balance In the Vending Machine Is $ " );
System.out.println(keptAmount);
// Inititalize the Amount to 0
holdingAmount = 0 ;
}
public void removeCoins() {
keptAmount = keptAmount + holdingAmount;
// Inititalize the Amount to 0
holdingAmount = 0 ;
System.out.print( "The System Is Going to Release ");
System.out.print( keptAmount);
System.out.println("Quarter(s)");
// Set the Total To 0 After Withdrawl
keptAmount = 0 ;
}
}
------------------------------------------------------
import java.util.Scanner;
public class MachineUI {
// Object Created
private VendingMachine machine;
// Scanner Object for User input
private Scanner user_input = new Scanner(System.in);
// Overloaded constructor
// Accepts an Object from VendingMachine Class
public MachineUI(VendingMachine machine) {
this.machine = machine;
}
//
public void domainMenu() {
VendingMachine m = new VendingMachine();
// Display Message to User
System.out.println(" Greetings! Welcome to The Best Vending Machine EVER! ");
System.out.println(" We Are The Only Vending Machine That cares for your Health");
System.out.println(" We ONLY Accept Quarters! ");
// Take User Input For Menu Selection
boolean loop_variable = true;
while (loop_variable == true) {
System.out.println(" Please Select an Option from The menu Below ");
System.out.println(" Option 1: Insert Quarter ");
System.out.println(" Option 2: Display Products ");
System.out.println(" Option 3: Make A Product Selection ");
System.out.println(" Option 4: Remove Money (For Operator Use ONLY)");
System.out.println(" Option 5: Restock Merchandise (For Operator Use ONLY) ");
System.out.println(" Option 6: Abort Vending Machine ");
// Take User Input Option
int x = user_input.nextInt();
// Insert Quarter case
if (x == 1) {
this.addCoin();
}
// Display Products to user Case
else if (x == 2) {
machine.listItems();
}
// Make A Product Selection Case
else if (x == 3) {
machine.listItems();
int i = -1;
while(!machine.isSelectionValid(i)){
System.out.println("Select A index from above to select that item");
i = user_input.nextInt();
}
this.selectItem(i);
}
// Remove Money From Machine Case
else if (x == 4) {
machine.emptyMoneyBox();
}
// Restock Merchandise case
else if (x == 5) {
machine.restock();
}
//Abort Vending Machine
else if (x == 6) {
loop_variable = false;
}
// For an Invalid Entry
else {
System.out.println(" Sorry There was An Error In Processing Your Request!");
System.out.println(" Please retry Selecting something from the menu. ");
}
}
}
// Add Quarter method
void addCoin() {
machine.addCoin();
}
void displayProducts() {
machine.listItems();
}
void selectItem(int i) {
machine.selectItem(i);
}
}