LANGUAGE: JAVA Write an applet that simulates a soft drink vending machine. The
ID: 3828926 • Letter: L
Question
LANGUAGE: JAVA
Write an applet that simulates a soft drink vending machine. The simulated machine dispenses the following soft drinks: cola, lemon-lime soda, and bottled water. These drinks cost $0.75 each to purchase.
When the applet starts, the drink machine should have a supply of 10 of each of the drinks. The applet should have a text field where the user can enter the amount of money he or she is giving the machine. The user can then click on a button to select a drink to dispense. The applet should also display the amount of change it is giving back to the user. The applet should keep track of its inventory of drinks and inform the user if he or she has selected a drink that is out of stock. Be sure to handle operator errors such as selecting a drink with no money entered and selecting a drink with an inadequate amount of money entered.
NOTE: Your applet must be embedded in a webpage. The webpage should have an appropriate title and description of the applet. Feel free to add color and excitement to your webpage. Be sure to follow the instructions on “How to Create and Deploy an Applet” to add your webpage to your project. Export your project as usual for submission.
Be creative! Creativity earns bonus points!
Explanation / Answer
import java.util.*;
public class Automaton {
public static void main (String[] args) {
boolean exit;
exit = false;
String choice;
String coin;
String first ="You have choosen ";
String second =", please pay ";
String third = " amount and type select when done; ";
String coinsacc = "Coins accepted: 1,2,5,10 and 20: ";
String in = "Insert coin: ";
String water1 = "water";
String coca = "cola";
String juicy ="lemon-soda";
String thanks = "Thank you. No change.";
String thank = "Thank you. Your change is: ";
String back = " amount remaining.";
String not = "Not enough paid ";
String pence = " amount.";
String select = "select";
final int juice = 15;
final int water = 10;
final int cola = 12;
Scanner console = new Scanner(System.in);
while (!exit) {
System.out.println("Choose your drink ");
System.out.println("Water press w ");
System.out.println("Cola press c ");
System.out.println("lemon-soda press l ");
System.out.print("Your choice: ");
choice = console.next();
if (choice.equalsIgnoreCase("w")) {
System.out.println(first + water1 + second + water + third);
System.out.println(coinsacc);
int amount = 0;
while (amount < 100000000) {
System.out.println(in);
coin = console.next();
if (coin.equals("1")) {
amount += 1;
} else if (coin.equals("2")) {
amount += 2;
} else if (coin.equals("5")) {
amount += 5;
} else if (coin.equals("10")) {
amount += 10;
} else if (coin.equals("20")) {
amount += 20;
} else if (!coin.equals("1") && !coin.equals("2") && !coin.equals("5")
&& !coin.equals("10") && !coin.equals("20") &&
!coin.equalsIgnoreCase(select)) {
System.exit(0);
} else if (coin.equalsIgnoreCase(select) && amount == 10 ) {
System.out.println(thanks);
} else if (coin.equalsIgnoreCase(select) && amount > 10) {
System.out.println(thank + (amount - water) + amount);
} else if (coin.equalsIgnoreCase(select) && amount < 10) {
System.out.println(not + (water - amount) + back);
break ;
}
}
}
if (choice.equalsIgnoreCase("c")) {
System.out.println(first + coca + second + cola + third);
System.out.print(coinsacc);
int amount = 0;
while (amount < 100000000) {
System.out.println(in);
coin = console.next();
if (coin.equals("1")) {
amount += 1;
} else if (coin.equals("2")) {
amount += 2;
} else if (coin.equals("5")) {
amount += 5;
} else if (coin.equals("10")) {
amount += 10;
} else if (coin.equals("20")) {
amount += 20;
} else if (!coin.equals("1") && !coin.equals("2")
&& !coin.equals("5") && !coin.equals("10")
&& !coin.equals("20") && !coin.equalsIgnoreCase(select)) {
System.exit(0);
} else if (coin.equalsIgnoreCase(select) && amount < 12) {
System.out.println(not + (cola - amount) + back);
} else if (coin.equalsIgnoreCase(select) && amount == 12 ) {
System.out.println(thanks);
} else if (coin.equalsIgnoreCase(select) && amount > 12) {
System.out.println(thank + (amount - cola) + pence);
break ;
}
}
}
if (choice.equalsIgnoreCase("j")) {
System.out.println(first +chilled + second + lemon-soda + third);
System.out.print(coinsacc);
int amount = 0;
while (amount < 100000000) {
System.out.println(in);
coin = console.next();
if (coin.equals("1")) {
amount += 1;
} else if (coin.equals("2")) {
amount += 2;
} else if (coin.equals("5")) {
amount += 5;
} else if (coin.equals("10")) {
amount += 10;
} else if (coin.equals("20")) {
amount += 20;
} else if (!coin.equals("1") && !coin.equals("2")
&& !coin.equals("5") && !coin.equals("10")
&& !coin.equals("20") && !coin.equalsIgnoreCase(select)) {
System.exit(0);
} else if (coin.equalsIgnoreCase(select) && amount < 15) {
System.out.println(not + (juice - amount) + back);
} else if (coin.equalsIgnoreCase(select) && amount == 15 ) {
System.out.println(thanks);
} else if (coin.equalsIgnoreCase(select) && amount > 15) {
System.out.println(thank + (amount - juice) + amount);
break ;
}
}
}
else if (choice.equalsIgnoreCase("terminate")) {
System.out.print("Done");
exit = true;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.