超市收银台
public class ChaoShiSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String pName1 = "苹果";
double pPricel = 5;
String pId1 = "1001";
String pName2 = "香蕉";
double pPrice2 = 7;
String pId2 = "1002";
String pName3 = "菠萝";
double pPrice3 = 12;
String pId3 = "1003";
double amount = 0;
int totalNum = 0; //商品总数量
double totalAmount = 0; //商品总金额
System.out.println("**********超市收银台*************");
while(true) {
while(true) {
System.out.println("请输入商品编号:");
String id = input.next();
String name = "";
double price = 0;
if(pId1.equals(id)) {
name = pName1;
price = pPricel;
}else if(pId2.equals(id)) {
name = pName2;
price = pPrice2;
}else if(pId3.equals(id)) {
name = pName3;
price = pPrice3;
}else {
System.out.println("没有此商品");
continue;
}
System.out.println("请输入商品数量:");
int num = input.nextInt();
amount = price * num;
System.out.println("您当前添加的商品是"+name+",单价"+price+"元,"+"金额为"+amount+"元");
totalAmount += amount;
totalNum += num;
System.out.println("1继续添加商品,2退出结算, 3清空商品 ");
int flag = 0;
flag = input.nextInt();
if(flag == 2) {
break;
}else if(flag == 3){
totalAmount = 0;
totalNum = 0;
System.out.println("--------------------------");
continue;
}else {
continue;
}
}
//结算
System.out.println("您购买了 " + totalNum + " 件商品,总金额为 " + totalAmount + " 元");
while(true) {
System.out.println("请输入付款金额:");
double money = input.nextDouble();
if(money >= totalAmount) {
System.out.println("您付款"+money+"元,找零"+ (money - totalAmount) +"元");
System.out.println("欢迎下次光临!");
break;
}else {
System.out.println("金额不够,请重新付款");
continue;
}
}
System.out.println("--------------------------");
}
}
}