Please help me get started! Javascript (Basic) This application simulates a vend
ID: 3684922 • Letter: P
Question
Please help me get started!
Javascript (Basic)
This application simulates a vending machine that dispenses soft drinks.
• If the user clicks one of the money deposit buttons, this application adds the amount to the total deposited and updates the display.
• If the user clicks the Refund button, this application displays the amount refunded and resets the amount deposited to zero.
• If the user clicks a drink button and it is the last drink in stock, this application disables the button for that drink to indicate that it is out of stock.
• If the user clicks a drink button, but the user has not deposited enough money, this application displays how much more money needs to be deposited to purchase the drink.
• If the user clicks a drink button, and the user has deposited enough money, this application displays a message to enjoy the drink. In addition, if the user has deposited too much money, this application displays how much change is being returned.
Specifications
• The drinks cost $0.75.
• When this application is started, the vending machine has 2 of each drink in stock, and the buttons for all drinks are enabled.
Vending Machine Dollar Qusiter Dime Nicke! Refund Dollar Quarte DimeNickelRefund $ 0.00 Coke D Diet Coke Mountain Dew Dr. Pepper Root Beer Water Please enjoy your Diet Coke and take your $0.25 change.Explanation / Answer
<!DOCTYPE html>
<html>
<head>
<script>
function init_storage()
{
localStorage.balance = 0.000;
localStorage.coke = 2;
localStorage.dcoke = 2;
localStorage.mdew = 2;
localStorage.dpep = 2;
localStorage.rbeer = 2;
localStorage.water = 2;
document.getElementById("mon").value="0.00";
}
function desable_button(but_id) {
document.getElementById(but_id).disabled = true;
}
function add_money(money)
{
current = parseFloat(document.getElementById("mon").value);
if(money == "dol")
{
current=(current + 1).toFixed(2);
document.getElementById("mon").value=current;
}
if(money == "qua")
{
current=(current + .25).toFixed(2);
document.getElementById("mon").value=current;
}
if(money == "dim")
{
current=(current + .1).toFixed(2);
document.getElementById("mon").value=current;
}
if(money == "cen")
{
current=(current + .01).toFixed(2);
document.getElementById("mon").value=current;
}
document.getElementById("msg").innerHTML="Click Deposite to add $" + current +" to your balance";
}
function deposit()
{
current = parseFloat(document.getElementById("mon").value);
localStorage.balance=current;
document.getElementById("msg").innerHTML="$" + current +" successfully deposited to your balance";
reset_mon();
}
function refund()
{
refund_amount=localStorage.balance;
if(refund_amount>0)
{
document.getElementById("mon").value="0.00";
localStorage.balance=0.00;
document.getElementById("msg").innerHTML="$" + refund_amount +" successfully refunded to you";
}
else
document.getElementById("msg").innerHTML="Sorry, You have no balance in your account!";
}
function reset_mon()
{
document.getElementById("mon").value="0.00";
}
function dispose(drink)
{
balance = parseFloat(localStorage.balance);
if(balance >= .75)
{
change = (parseFloat(localStorage.balance) - .75);
drink_qantity=parseInt(localStorage.getItem(drink));
//alert(drink_qantity);
if(drink_qantity == 1)
{
desable_button(drink)
document.getElementById("msg").innerHTML="Please enjoy the dring and take $" + change + " Change";
}
if(drink_qantity==2)
{
localStorage.setItem(drink, 1);
localStorage.setItem('balance', 0.0);
document.getElementById("msg").innerHTML="Please enjoy the dring and take $" + change + " Change";
}
}
else
{
document.getElementById("msg").innerHTML="Insfficient balance. Please add $" + (.75 - balance) + " more money.";
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Doller"> <input type="button" value="Quarter"> <input type="button" value="Dime"> <input type="button" value="Cent"> <input type="button" value="Refund"> <br><br>
<input type="text" id="mon">
<input type="button" value="Deposite">
<input type="button" value="Reset">
<br><br>
<input type="button" id="coke" value="Coke"> <input type="button" id="dcoke" value="Diet Coke"> <input type="button" id="mdew" value="Mountain Dew"> <input type="button" id="dpep" value="Dr. Pepper"> <input type="button" id="rbeer" value="Root Beer"> <input type="button" id="water" value="Water"> <br><br>
<span id="msg"></span>
</form>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.