题目:
将为披萨店创建一个交易处理程序。该程序允许用户选择不同的pizza,做一些基本的输入验证确保用户输入的值正确有效。本次实验使用标准JAVA IO(输入/输出)和Scanner类,并创建新的类。
代码实现:
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
public class pizza {
public static void main(String[] args) {
System.out.println("Welcome to the Java and a Slice Pizza Parlor.");
System.out.print("Please enter a name for our transaction records:");
//输入姓名
Scanner a=new Scanner(System.in);
String name=a.nextLine();
System.out.println(" ");
System.out.println("Thank you,"+name);
String Ifm="M";
String Ifv="V";
String Ifh="H";
String ifm="m";
String ifv="v";
String ifh="h";
//输入选择的pizza口味
System.out.println("Which type of pizza would you like?");
System.out.println("---------------------------------");
System.out.println("[M] Mega-Meaty");
System.out.println("[V] Very Vegetarian");
System.out.println("[H] Heaping Hawaiian");
System.out.println("---------------------------------");
System.out.print("Enter pizza choice:");
Scanner b=new Scanner(System.in);
String type=b.nextLine();
System.out.println(" ");
if(type.equals(Ifh)||type.equals(Ifv)||type.equals(Ifm)||type.equals(ifh)||type.equals(ifv)||type.equals(ifm)) {
if(type.equals(Ifm)||type.equals(ifm)){
String newtype="Mega-Meaty";
System.out.println("Price list for pizza type:"+newtype);
}
else if(type.equals(Ifv)||type.equals(ifv)){
String newtype="Very Vegetarian";
System.out.println("Price list for pizza type:"+newtype);
}
else if(type.equals(Ifh)||type.equals(ifh)){
String newtype="Heaping Hawaiian";
System.out.println("Price list for pizza type:"+newtype);
}
}
else {
System.out.println("I'm sorry, BadUser, but that is an invalid choice.");
System.out.println("Goodbye.");
System.exit(0);
}
System.out.println("---------------------------------");
double prize12=0;
double prize16=0;
if(type.equals(Ifm)||type.equals(ifm)) {
prize12=11.5;
prize16=15.5;
}
else if(type.equals(Ifv)||type.equals(ifv))
{
prize12=9.50;
prize16=13.50;
}
else if(type.equals(Ifh)||type.equals(ifh))
{
prize12=10.50;
prize16=14.50;
}
//输入尺寸大小
System.out.println("12-inch: $"+prize12);
System.out.println("16-inch: $"+prize16);
System.out.println("---------------------------------");
System.out.println(" ");
System.out.println("What size pizza would you like?");
System.out.print("Enter size in inches:");
Scanner c=new Scanner(System.in);
int size=c.nextInt();
if((size!=12&&size!=16))
{
System.out.println("I'm sorry, BadUser, but that is an invalid choice");
System.out.println("Goodbye.");
System.exit(0);
}
//输入数量
System.out.println(" ");
System.out.println("How many pizzas would you like?");
System.out.print("Enter number between 1 and 12:");
Scanner d=new Scanner(System.in);
int number=d.nextInt();
if((number<1)&&number>12)
{
System.out.println("I'm sorry, BadUser, but that is an invalid choice");
System.out.println("Goodbye.");
System.exit(0);
}
//是否需要加Cheezy Bread
System.out.println(" ");
System.out.println("Would you like to add Cheezy Bread to that, for a $3.00 charge?");
System.out.print("Please enter Y for yes or N for no:");
Scanner e=new Scanner(System.in);
String type1=e.nextLine();
String Ify="Y";
String ify="y";
String Ifn="N";
String ifn="n";
if((type1.equals(ify)||type1.equals(Ify)||type1.equals(ifn)||type1.equals(Ifn)))
{
;
}
else {
System.out.println("I'm sorry, BadUser, but that is an invalid choice.");
System.out.println("Goodbye.");
System.exit(0);
}
System.out.println(" ");
System.out.println("---------------------------------");
System.out.println(" Thank you,"+name+". Here is a record of your purchase.");
System.out.println("---------------------------------");
System.out.println(number+" "+size+"-inch Heaping Hawaiian pizzas.");
if((type1.equals(ify)||type1.equals(Ify)))
{
System.out.println("1 order Cheezy Bread.");
}
System.out.println("---------------------------------");
double totalprize=0;
if(size==12){
totalprize=prize12*number;
}
else{
totalprize=prize16*number;
}
if(type1.equals(ify)||type1.equals(Ify))
{
totalprize+=3.00;
}
System.out.println("Total cost:$"+new DecimalFormat("0.00").format(totalprize));
a.close();
b.close();
c.close();
d.close();
e.close();
System.out.println("---------------------------------");
System.out.println(" ");
System.out.println("---------------------------------");
//格式化输出日期
Date nowTime = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy年MMMMdd日 EEEE HH时mm分ss秒z",Locale.CHINA);
System.out.println(f.format(nowTime));
System.out.println("---------------------------------");
}
}
运行结果:
Welcome to the Java and a Slice Pizza Parlor.
Please enter a name for our transaction records:hh
Thank you,hh
Which type of pizza would you like?
---------------------------------
[M] Mega-Meaty
[V] Very Vegetarian
[H] Heaping Hawaiian
---------------------------------
Enter pizza choice:M
Price list for pizza type:Mega-Meaty
---------------------------------
12-inch: $11.5
16-inch: $15.5
---------------------------------
What size pizza would you like?
Enter size in inches:12
How many pizzas would you like?
Enter number between 1 and 12:3
Would you like to add Cheezy Bread to that, for a $3.00 charge?
Please enter Y for yes or N for no:Y
---------------------------------
Thank you,hh. Here is a record of your purchase.
---------------------------------
3 12-inch Heaping Hawaiian pizzas.
1 order Cheezy Bread.
---------------------------------
Total cost:$37.50
---------------------------------
---------------------------------
2021年七月10日 星期六 14时33分06秒CST
---------------------------------