import java.util.Scanner;
public class ShoppingCM
{
public static void main(String[] args)
{
//少林寺酥饼核桃香甜包子
String slsName = "少林寺酥饼核桃香甜包子";
String slsID = "090115";
double slsPrice = 15.50;
int slsNum = 0;
double slsMoney = 0;
//然光α移
String rgName = "然光α移";
String rgID = "090028";
double rgPrice = 16.00;
int rgNum = 0;
double rgMoney = 0;
//尚康杂粮牡丹饼
String skName = "尚康杂粮牡丹饼";
String skID = "090027";
double skPrice = 14.50;
int skNum = 0;
double skMoney = 0;
//公用变量
double amount = 0;//打折前的总金额(原价)
int num = 1;//输入错误的次数
int sum = 0; //共计,购买物品的总数量
int sfk = 0;//实付款
int flag = 0;//用户输入的switch选择条件变量
//打印货品报价单
System.out.println(" << 货物售价单 >>");
System.out.println(" -----------------------------------------");
System.out.println(" 品名 售价");
System.out.println();
System.out.println(" "+slsName+" "+slsPrice);
System.out.println(" "+rgName+" "+rgPrice);
System.out.println(" "+skName+" "+skPrice);
System.out.println(" -----------------------------------------");
System.out.println();
//创建Scanner对象,用于用户输入数量。
Scanner sc = new Scanner(System.in);
wai:for (int i = 1; ;i++ )
{
System.out.println(" 第"+i+"位消费者");
System.out.println();
int times =0;//用户购买的次数
nei:for ( ; ; )//用户的购买次数或向购物车里添加的物品数量没有限制,通过switch中的”break wai;“语句跳出for循环。
{
//消费者购买清单
if (times !=0)
System.out.println(" 第"+i+"位消费者");
System.out.println();
if (sum != 0 && sfk == 0&× != 3)
{
slsName = slsName.length()>7 ? slsName.substring(0,7)+"..." : slsName;
rgName = rgName.length()>7 ? rgName.substring(0,7)+"..." : rgName;
skName = skName.length()>7 ? skName.substring(0,7)+"..." : skName;
System.out.println(" -------- << 您购物车里的物品清单 >> --------------");
System.out.println();
System.out.println(" 品名 编号 售价 数量 金额\t");
System.out.println(" 1 "+slsName+" "+slsID+" "+slsPrice+" "+slsNum+" "+slsMoney);
System.out.println(" 2 "+rgName+" "+rgID+" "+rgPrice+" "+rgNum+" "+rgMoney);
System.out.println(" 3 "+skName+" "+skID+" "+skPrice+" "+skNum+" "+skMoney);
System.out.println(" --------------------------------------------------------");
System.out.println();
System.out.println();
}
//使用户操作输入数据后,是否可重新或继续购买的提示
if (times != 3)
{
System.out.println(" -------------- << 购物引导 >>------------");
System.out.println();
if (sum != 0 && sfk == 0)
{
System.out.println(" 1、重新购买!");
amount = 0;
}
else
{
System.out.println(" 1、开始购物! ");
}
System.out.println(" 2、结账并打印小票!");
System.out.println(" 3、我不想买了!,退出账号!");
System.out.println(" 4、退出购物系统!");
System.out.println();
System.out.print("请输入数字选项:");
flag = sc.nextInt();
System.out.println();
if( sum == 0 && flag == 1)
{
System.out.println(" << 开始购物 >> ");
System.out.println();
}
else if ( sum != 0& flag == 1)
{
System.out.println(" << 重新购物 >> ");
System.out.println();
}
}
//使用户操作输入数据后是否可重新购买
switch (flag)
{
case 1:
if (times != 3)
{
System.out.println(" ----- << 您有两次重新购买的机会,购买次数满三次,将强制付款!>> -----");
System.out.println(" ----- << 您还有"+ (2-times)+"次重新购买的机会 >> -----");
System.out.println();
}
System.out.print("请输入要购买的”少林寺酥饼核桃香甜包子“的数量:");
slsNum = sc.nextInt();
slsMoney = slsNum*slsPrice;
System.out.print("请输入要购买的”然光α移“的数量:");
rgNum = sc.nextInt();
rgMoney = rgNum*rgPrice;
System.out.print("请输入要购买的”尚康杂粮牡丹饼“的数量:");
skNum = sc.nextInt();
skMoney = skNum*skPrice;
System.out.println();
sum = sum + slsNum + rgNum + skNum;
times++;
if (times == 3)
{
System.out.println(" --- << 您已使用完所有3次购买机会,请付款! >> ---");
System.out.println();
flag = 2;
break;
}
break;
case 2:
//---------票头-----------------------------------------
if (sum == 0)
{
System.out.println(" 您还没有购买任何东西,无需打印小票!");
System.out.println();
break;
}
else
{
System.out.println(" 欢 迎 光 临 ");
System.out.println(" --------------------------------------------------------------");
System.out.println();
System.out.println(" 品名 编号 售价 数量 金额");
//定义变量:品名 编号 售价 数量 金额
//--------票体-----------------------------------------
//分别判断三种货品的名称
/*if ( slsName.length()>7)
{
slsName.substring(0,7)+"...";
}*/
//满足:slsName.length()>7
// 截取
// slsName = slsName.substring(0,7)+"...";
//不满足:slsName.length()>7
// 保持原样
slsName = slsName.length()>7 ? slsName.substring(0,7)+"..." : slsName;
rgName = rgName.length()>7 ? rgName.substring(0,7)+"..." : rgName;
skName = skName.length()>7 ? skName.substring(0,7)+"..." : skName;
System.out.println(" "+slsName+" "+slsID+" "+slsPrice+" "+slsNum+" "+slsMoney );
System.out.println(" "+rgName+" "+rgID+" "+rgPrice+" "+rgNum+" "+rgMoney );
System.out.println(" "+skName+" "+skID+" "+skPrice+" "+skNum+" "+skMoney );
System.out.println(" --------------------------------------------------------------");
//-------票脚------------------------------------------
int totalNum = 3;//项数
amount = slsNum*slsPrice + rgNum*rgPrice + skNum*skPrice;//原价
double amount1 = 0;//需付款
if (amount<300)
{
amount1 = amount;
}
if (amount>=300&&amount<600)
{
amount1 = amount*0.95;
}
if (amount>=600&&amount<1000)
{
amount1 = amount*0.9;
}
if (amount>=1000&&slsNum<30)
{
amount1 = amount*0.8;
}
if (amount>=1000&&slsNum>=30)
{
amount1 = amount*0.8 - 20;
}
double zhekou = amount - amount1;//折扣
amount1 = Math.round(amount1);//对实付款四舍五入
zhekou = Math.round(zhekou);//对折扣四舍五入
System.out.print(" 项数:"+totalNum);
System.out.println(" 共计:"+sum+"件");
if (amount != 0)
{
System.out.print(" 原价:"+amount+"¥");
System.out.println();
System.out.print(" 需付:"+(int)amount1+"¥");//将double类型的amount1强制转换为int型
System.out.println(" 折扣:"+(int)zhekou+"¥");//将double类型的zhekou强制转换为int型
System.out.print(" 请付款:");
sfk = sc.nextInt();
System.out.print(" 实付款:"+sfk+"¥");
System.out.println(" 找零:"+(sfk-amount1)+"¥");
}
//付完款后所有值初始化为0
amount = 0;
slsNum = 0;
rgNum = 0;
skNum = 0;
sum = 0;
sfk = 0;
times = 0;
System.out.println(" 凭小票可兑换发票!");
System.out.println();
System.out.println("<<---------------------------------------------------------------------------->>");
continue wai;
}
case 3:
System.out.println(" -------- << 您的账号已退出!>> --------");
System.out.println(" -------- << 欢迎下次光临!>> ---------");
System.out.println();
System.out.println("<<---------------------------------------------------------------------------->>");
break nei;
case 4:
System.out.println(" -------- << 您已退出购物系统!>> --------");
break wai;
default:
if (num != 3)
{
System.out.println(" ---- << 您的输入有误,请重新输入!>> ----");
System.out.println();
}
else
{
System.out.println(" ---- << 您的输入有误,即将推出购物系统!>> ----");
System.out.println();
System.out.println(" 您输入错误超过三次,已退出购物系统!");
break nei;
}
num++;
continue;
}
}
}
}
}
|
|