Question

In: Computer Science

Given a snack vending machine, assume the machine accepts bills and coins, after customer input money...

Given a snack vending machine, assume the machine accepts bills and coins, after customer input money and select items, the machine can calculate and dispose snack.

a. Finish a class diagram of the snack vending machine using correct UML notations.

b. Convert the class diagram above to Java code.

Solutions

Expert Solution

import java.util.Scanner;

public class VendingMachine
{
private Product[] items;

public VendingMachine() {
items = new Product[5];
items[0] = new Product("Chips", 20.0, 20);
items[1] = new Product("Pepsi", 40.0, 10);
items[2] = new Product("Mazza", 40.0, 10);
items[3] = new Product("Bingo", 40.0, 10);
items[4] = new Product("Dairy Milk", 20.0, 20);
}

public void displayInventory() {
for (int i = 0; i < items.length; i++)
{
System.out.print(items[i].getName());
System.out.print('\t');
System.out.print(items[i].getQty());
System.out.println();
}
}

public void Productdispenser(int itemCode) {
Scanner in = new Scanner(System.in);
if (items[itemCode].getQty() <= 0) {
System.out.println("Oops!.., out of stock"); // Check for stock
}
else
{
  
System.out.println("MRP: " + items[itemCode].getPrice()); // Prints MRP
System.out.print("Insert the coin: "); // Insert coin
double amt = in.nextDouble();
if (amt < items[itemCode].getPrice()) {
System.out.println("Insufficient money paid, can't dispense " +
items[itemCode].getName());
System.out.println("Refunding " + amt);
}
else
{
System.out.println("Dispensing one " + items[itemCode].getName());
double changeAmt = amt - items[itemCode].getPrice();
if (changeAmt > 0)
System.out.println("Here is your change amount of " + changeAmt);
items[itemCode].reduceQty();
}
}
}

Product[] getItems() {
return items;
}

public static void main(String args[])
{

Scanner in = new Scanner(System.in);
VendingMachine v = new VendingMachine();
Product[] Vending_Items = v.getItems();
  
  
System.out.println("Vending Machine Menu");
for (int i = 0; i < Vending_Items.length; i++)
{
System.out.println("Enter " + (i+1) + " for " + Vending_Items[i].getName()); // Display Product List with the MRP
}

System.out.println("Enter 6 to stop the Vending Machine");
  
int option;
do {
System.out.print("Enter your option: ");
option = in.nextInt();

if (option < 1 || option > 6) {
System.out.println("Incorrect...!! Please choose another option");
}
else if(option == 6) {
System.out.println("Stopping Vending Machine...Thank you");
}
else {
v.Productdispenser(option - 1);
}
} while(option != 6);

}
}
public class Product
{
private String name;
private double price;
private int qty;
  
public Product(String itemName, double itemPrice, int itemQty)
{
name = itemName;
price = itemPrice;
qty = itemQty;
}
  
public String getName() {
return name;
}
  
public double getPrice() {
return price;
}
  
public int getQty() {
return qty;
}
  
public void reduceQty() {
if (qty > 0)
qty -= 1;
}
}


Related Solutions

Write a program that simulates a vending machine. The machine holds six snack items labeled them...
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices: Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 The program then should ask the user to enter the item to purchase along with a sum of money....
Everyone knows what money is. It is the collection of coins and bills that we carry...
Everyone knows what money is. It is the collection of coins and bills that we carry around in our pockets, purses. and wallets. Or at least that people often think that is what money is? In fact, “money” is merely a “social convention” as described by the text – Money is “what it does” rather than “what it is.” i.     (1) Briefly describe three (3) of the four (4) functions that must be present to make money, well, money, AND; (2)...
Construct a finite-state machine, using combinational logic for an apple vending machine that only accepts nickels...
Construct a finite-state machine, using combinational logic for an apple vending machine that only accepts nickels (5 cents). When 15 cents is deposited the user can push a button to receive an apple and the machine resets. If the user inserts more than 15 cents no money will be returned. What are the machine states? What are the inputs? What are the outputs? Draw state table Draw state diagram Write the combinational logic for next state and output Explain if...
1. Bills and coins make up about __________.a) 50–60% of the total money supplyb)...
1. Bills and coins make up about __________.a) 50–60% of the total money supplyb) 5–10% of the total money supplyc) 100% of the money supplyd) 15–20% of the money supply2.Many theories of the reasons for the development of states suggest that __________.a. they are based on kinship or tribal groups and are an attempt to maintain the autonomy of such groups within specified boundariesb. they were formed to combine smaller states with similar cultures into larger groups to afford them...
Author Anna Schwartz writes this about money: “The U.S. money supply comprises currency—dollar bills and coins...
Author Anna Schwartz writes this about money: “The U.S. money supply comprises currency—dollar bills and coins issued by the Federal Reserve System and the Treasury—and various kinds of deposits held by the public at commercial banks and other depository institutions such as savings and loans and credit unions.” (Schwartz, n.d) In this threaded discussion, complete the following: 1) Discuss the concept of money. First, define the functions of money and explain how currency meets these functions. 2) Historically, a number...
Write a complete MiniMIPS program that accepts a seqeunce of integers at input and after the...
Write a complete MiniMIPS program that accepts a seqeunce of integers at input and after the receipt of each new input value, displays the largest and smallest integers thus far. An input of 0 indicates the end of input values and is not an input value itself. Note that you do not need to keep all integers in memory.
Assume a dimension that a customer is interested in is given by R = x0.5+y. The...
Assume a dimension that a customer is interested in is given by R = x0.5+y. The variables x and y are determined using standard calibration gage pins, where the nominal value of x is 0.9” and the nominal value of y is 0.2”. The manufacturer of the gage pins states that the pins have error bounds of +0.0002” with a quality rate of 95.45%. Assume that the gage pin diameter follows a normal distribution. What is the expanded uncertainty with...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
Assume revenues decrease and expenses increase with the age of the machine as given in the...
Assume revenues decrease and expenses increase with the age of the machine as given in the table below and it can be sold for $200,000 at the end of year five. Calculate NPV, payback, BCR, and IRR, should the equipment be purchased if the discount rate is 6% or 10%?            Revenue   Expense    Year 0       -      $1,500,000 (investment)    Year 1       $850,000   $200,000    Year 2       $750,000   $250,000    Year 3       $650,000  ...
you are borrowing money that musy be paid back after 6 months. You are given the...
you are borrowing money that musy be paid back after 6 months. You are given the following 12%per year, 11%per year compounded daily, 1% per month and 11.5% per year compounded monthly . Determine the effective semi annual intrest rate. which loan option do you prefer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT