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

Assume that you are a vending machine dealer. You plan to purchase a vending machine for...
Assume that you are a vending machine dealer. You plan to purchase a vending machine for $200,000. One year later, you are expected to sell it back and receive $224,000 as cash. What is the IRR (internal rate of return) on this investment? Be sure to apply the definition of IRR and show your work. 3 points> In order to buy the vending machine, you plan on borrowing $80,000 from Bank at 5% of interest rate and raise $120,000 from...
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...
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.
Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
Design a finite state machine that accepts binary strings divisible by 5. Input is fed to...
Design a finite state machine that accepts binary strings divisible by 5. Input is fed to the FSM one bit at a time, from the most significant bit to the least significant bit (left to right). For example, 1010 should be accepted, but 1100 should be rejected. Clearly explain how you designed your state transitions. Showing the FSM alone is worth minimal credit!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT