import javax.swing.*;
public class VendingMachine {
public static int readChoice() {
String inputString;
int choice;
do {
inputString = JOptionPane.showInputDialog(null,
"Enter your choice of drink:\n"
+ "1. Coke (60 cents)\n"
+ "2. Pepsi (60 cents)\n"
+ "3. Soya Bean (50 cents)\n"
+ "4. Green Tea (70 cents)\n"
+ "5. Ice Coffee (80 cents)\n",
"SP Vendimatic Vending Machine",
JOptionPane.QUESTION_MESSAGE);
choice = Integer.parseInt(inputString);
if (choice < 1 || choice > 5) {
JOptionPane.showMessageDialog(null,
"Invalid input ! Please enter in the range from 1 to 5.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} while (choice < 1 || choice > 5);
return choice;
}
public static int computeAndDisplayPrice(int choice) {
int price = 0;
switch (choice) {
case 1:
case 2:
price = 60;
break;
case 3:
price = 50;
break;
case 4:
price = 70;
break;
case 5:
price = 80;
}
JOptionPane.showMessageDialog(null,
"Price is " + price + " cents");
return price;
}
public static int readPayment(int price) {
String inputString;
int payment;
do {
inputString = JOptionPane.showInputDialog(null,
"Please enter your payment ("
+ price + " - 100 cents):\n",
"SP Vendimatic Vending Machine",
JOptionPane.QUESTION_MESSAGE);
payment = Integer.parseInt(inputString);
if (payment < price) {
JOptionPane.showMessageDialog(null,
"Invalid input ! Please enter a minimum payment of " +
price + " cents.",
"Error",
JOptionPane.ERROR_MESSAGE);
} else if (payment > 100) {
JOptionPane.showMessageDialog(null,
"Invalid input ! Please do not enter more than 100 cents.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} while (payment < price || payment > 100);
return payment;
}
public static int computeAndDisplayChange(int payment, int price) {
int change;
change = payment - price;
JOptionPane.showMessageDialog(null, "Change is " + change + " cents.");
return change;
}
public static void dispenseChange(int change) {
int num50cents, num20cents, num10cents;
int balance;
num50cents = change / 50;
balance = change % 50;
num20cents = balance / 20;
balance = balance % 20;
num10cents = balance / 10;
String s = "Dispensing...\n"
+ num50cents + " x 50c coin\n"
+ num20cents + " x 20c coin\n"
+ num10cents + " x 10c coin";
JOptionPane.showMessageDialog(null, s );
}
public static void main(String[] args) {
int choice;
int price;
int payment;
int change;
String inputString;
char cont;
do {
choice = readChoice();
price = computeAndDisplayPrice(choice);
payment = readPayment(price);
change = computeAndDisplayChange(payment, price);
if (change > 0) {
dispenseChange(change);
}
do {
inputString = JOptionPane.showInputDialog(null,
"Do you want another drink (Y/N)?");
cont = inputString.charAt(0);
if (! (cont == 'Y' || cont == 'N' || cont == 'y' || cont == 'n')) {
JOptionPane.showMessageDialog(null,
"Invalid input ! Please enter Y or N.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
while (! (cont == 'Y' || cont == 'N' || cont == 'y' || cont == 'n'));
}
while (cont == 'Y' || cont == 'y');
JOptionPane.showMessageDialog(null,
"VendingMachine class program terminated.");
} //end main
} //end class
Friday, November 03, 2006
Solution for Mock Test (Available only until 10 Nov 2006)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment