create an application for a vending machine ithat dispenses items such as snacks
ID: 3571314 • Letter: C
Question
create an application for a vending machine ithat dispenses items such as snacks ( 5 types) and beverages (at least 5types), to customers automatically, after the customer inserts currency the machine. The application should display the list of all items available to thecustomer and should return the balance money to the customer after dispensing the requested item. this is for java can the gui have pictures and buttons and stuff thanks. it has to look good thanks everyone a good java code please I appreciate it gui please thanks I just need a gui java code ty y'all are lifesavers I have the ui here I just need a code for it thanks
Explanation / Answer
package vending;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class implementvendingmachine implements VendingMachine {
private Inventory<Coin> totalcashInventory = new Inventory<TheCoin>();
private Inventory<Item> itemInventory = new Inventory<Item>();
private long calculatetotalSales;
private Item currentItem;
private long thecurrentBalance;
public implementvendingmachine(){
toinitialize();
}
private void toinitialize(){
//This is used to initialize machine with 5 coins of each denomination and 5 items in all
for(TheCoin c : TheCoin.values()){
totalcashInventory.put(c, 5);
}
for(Item i : Item.values()){
itemInventory.put(i, 5);
}
}
public long letsselectItemAndGetPrice(Item item) {
if(itemInventory.hasItem(item)){
currentItem = item;
return currentItem.getPrice();
}
throw new SoldOutException("This has been sold Out, Please buy another item");
}
public void pleaseinsertCoin(Coin coin) {
thecurrentBalance = thecurrentBalance + coin.getDenomination();
cashInventory.add(coin);
}
public Bucket<Item, List<Coin>> collectItemAndChange() {
Item item = collectItem();
calculatetotalSales = calculatetotalSales + currentItem.getPrice();
List<Coin> change = tocollectChange();
return new Bucket<Item, List<Coin>>(item, change);
}
private Item collectItem() throws NotSufficientChangeException,
NotFullPaidException{
if(isFullPaid()){
if(hasSufficientChange()){
itemInventory.deduct(currentItem);
return currentItem;
}
throw new NotSufficientChangeException("Not Sufficient change in
Inventory");
}
long theremainingBalance = currentItem.getPrice() - thecurrentBalance;
throw new NotFullPaidException("The Price not full paid, the remaining money is: ",
theremainingBalance);
}
private List<Coin> collectChange() {
long totalchangeAmount = thecurrentBalance - currentItem.getPrice();
List<Coin> change = getChange(totalchangeAmount);
updateCashInventory(change);
thecurrentBalance = 0;
currentItem = null;
return change;
}
public List<Coin> torefund(){
List<Coin> refundamount = getChange(currentBalance);
updateCashInventory(refundamount);
thecurrentBalance = 0;
currentItem = null;
return refundamount;
}
private boolean tocheckfsFullPaid() {
if(thecurrentBalance >= currentItem.getPrice()){
return true;
}
return false;
}
private List<Coin> togetChange(long amount) throws NotSufficientChangeException{
List<Coin> totalchanges = Collections.EMPTY_LIST;
if(amount > 0){
totalchanges = new ArrayList<Coin>();
long balance = amount;
while(balance > 0){
if(balance >= Coin.QUARTER.getDenomination()
&& cashInventory.hasItem(Coin.QUARTER)){
totalchanges.add(Coin.QUARTER);
balance = balance - Coin.QUARTER.getDenomination();
continue;
}else if(balance >= Coin.DIME.getDenomination()
&& cashInventory.hasItem(Coin.DIME)) {
totalchanges.add(Coin.DIME);
balance = balance - Coin.DIME.getDenomination();
continue;
}else if(balance >= Coin.NICKLE.getDenomination()
&& cashInventory.hasItem(Coin.NICKLE)) {
totalchanges.add(Coin.NICKLE);
balance = balance - Coin.NICKLE.getDenomination();
continue;
}else if(balance >= Coin.PENNY.getDenomination()
&& cashInventory.hasItem(Coin.PENNY)) {
totalchanges.add(Coin.PENNY);
balance = balance - Coin.PENNY.getDenomination();
continue;
}else{
throw new NotSufficientChangeException("NotSufficientChange,
now you can Please try another product");
}
}
}
return totalchanges;
}
public void toreset(){
cashInventory.clear();
itemInventory.clear();
calculatetotalSales = 0;
currentItem = null;
thecurrentBalance = 0;
}
public void printStats(){
System.out.println("Total Sales : " + calculate totalSales);
System.out.println("Current Item Inventory : " + itemInventory);
System.out.println("Current Cash Inventory : " + cashInventory);
}
private boolean checkiftheuser_hasSufficientChange(){
return hasSufficientChangeForAmount(thecurrentBalance - currentItem.getPrice());
}
private boolean hasSufficientChangeForAmount(long amount){
boolean hasChange = true;
try{
getChange(amount);
}catch(NotSufficientChangeException nsce){
return hasChange = false;
}
return hasChange;
}
private void updateCashInventory(List change) {
for(Coin c : change){
cashInventory.deduct(c);
}
}
public long getTotalSales(){
return calculatetotalSales;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.