import javax.swing.JOptionPane;
public class HandPhone2 {
public static int getModel() {
String input;
int modelNo;
do {
input = JOptionPane.showInputDialog(null,
"Enter Model No.: \n" +
"1 - Nokia N10 \n" +
"2 - Nokia S20 \n" +
"3 - Nokia K30 \n" +
"4 - Nokia E40 \n" +
"5 - Nokia D50 \n",
"Handphone",
JOptionPane.QUESTION_MESSAGE);
modelNo = Integer.parseInt(input);
if (modelNo < 1 || modelNo > 5) {
JOptionPane.showMessageDialog(null, "Model No. must be 1 to 5",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} while (modelNo < 1 || modelNo > 5);
return modelNo;
}
public static int getQuantity() {
String input;
int qty;
do {
input = JOptionPane.showInputDialog(null,
"Enter no. of handphones to purchase");
qty = Integer.parseInt(input);
if (qty <= 0) {
JOptionPane.showMessageDialog(null,
"Quantity must be more than 0",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} while (qty <= 0);
return qty;
}
public static void displayInfo(int modelNo, int qty) {
String modelName = "";
double price = 0, totPrice;
switch (modelNo) {
case 1:
modelName = "Nokia N10";
price = 1000;
break;
case 2:
modelName = "Nokia S20";
price = 888;
break;
case 3:
modelName = "Nokia K30";
price = 600;
break;
case 4:
modelName = "Nokia E40";
price = 400;
break;
case 5:
modelName = "Nokia D50";
price = 100;
break;
} //end switch
totPrice = qty * price;
JOptionPane.showMessageDialog(null, "Total purchase price for " +
qty + " " + modelName + " is $" +
totPrice);
}
public static void main(String[] args) {
int modelNo, qty;
modelNo = getModel();
qty = getQuantity();
displayInfo(modelNo, qty);
}
}
Thursday, October 26, 2006
Solution to Revision Test 2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment