本帖最后由 冰雪丝阳 于 2015-12-8 21:55 编辑
import java.util.Scanner;
public class ShoppingZK
{
public static void main(String[] args)
{
//少林寺酥饼核桃香甜包子
String slsName = "少林寺酥饼核桃香甜包子";
String slsID = "090115";
double slsPrice = 15.50;
int slsNum = 0;
double slsMoney = 0;
int tslsNum = 0;//购买少林寺酥饼核桃香甜包子的总数量
//然光α移
String rgName = "然光α移";
String rgID = "090028";
double rgPrice = 16.00;
int rgNum = 0;
double rgMoney = 0;
int trgNum = 0;//购买然光α移的总数量
//尚康杂粮牡丹饼
String skName = "尚康杂粮牡丹饼";
String skID = "090027";
double skPrice = 14.50;
int skNum = 0;
double skMoney = 0;
int tskNum = 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++ )//用户的购买次数或向购物车里添加的物品数量没有限制,通过switch中的”break wai;“语句跳出for循环。
{
//消费者购买清单
if (sum != 0 && sfk == 0 )
{
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(" 品名 编号 售价 数量 金额");
System.out.println(" 1 "+slsName+" "+slsID+" "+slsPrice+" "+tslsNum+" "+slsMoney );
System.out.println(" 2 "+rgName+" "+rgID+" "+rgPrice+" "+trgNum+" "+rgMoney );
System.out.println(" 3 "+skName+" "+skID+" "+skPrice+" "+tskNum+" "+skMoney );
System.out.println(" --------------------------------------------------------");
System.out.println();
System.out.println();
}
//使用户操作输入数据后,是否可重新或继续购买的提示
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();
System.out.print("请输入数字选项:");
flag = sc.nextInt();
System.out.println();
if (flag == 1&&sum != 0)
{
System.out.println(" 继续购物 ");
System.out.println();
}
else if( sum == 0 && flag == 1)
{
System.out.println(" 开始购物 ");
System.out.println();
}
//使用户操作输入数据后是否可重新购买
switch (flag)
{
case 1:
System.out.print("请输入要购买的”少林寺酥饼核桃香甜包子“的数量:");
slsNum = sc.nextInt();
tslsNum = slsNum + tslsNum;
slsMoney = tslsNum*slsPrice;
System.out.print("请输入要购买的”然光α移“的数量:");
rgNum = sc.nextInt();
trgNum = rgNum + trgNum;
rgMoney = trgNum*rgPrice;
System.out.print("请输入要购买的”尚康杂粮牡丹饼“的数量:");
skNum = sc.nextInt();
tskNum = skNum + tskNum;
skMoney = tskNum*skPrice;
System.out.println();
sum = sum + slsNum + rgNum + skNum;
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 = 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;
//满足:slsName.length()>7
// 截取
// slsName = slsName.substring(0,7)+"...";
//不满足:slsName.length()>7
// 保持原样
System.out.println(" "+slsName+" "+slsID+" "+slsPrice+" "+tslsNum+" "+slsMoney );
System.out.println(" "+rgName+" "+rgID+" "+rgPrice+" "+trgNum+" "+rgMoney );
System.out.println(" "+skName+" "+skID+" "+skPrice+" "+tskNum+" "+skMoney );
System.out.println("------------------------------------------------------------------");
//-------票脚------------------------------------------
//项数
int totalNum = 3;
//总计
amount = tslsNum*slsPrice + trgNum*rgPrice + tskNum*skPrice;
double amount1 = 0;
/*
如果商品总价超过300元,打95折
如果商品总价超过600元,打9折
如果商品总价超过1000元,打8折
如果商品总价超过1000元,并且少林寺酥饼核桃购买超过30份, 则总价打8折后立减20
使用if语句else多层条件判断完成前三个条件判断
使用一个单独的if完成最后立减20的逻辑
*/
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 amount1 = amount>=1000&&slsNum>=30 ? amount*0.9 : amount;
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;
tslsNum = 0;
rgNum = 0;
trgNum = 0;
skNum = 0;
tskNum = 0;
sum = 0;
sfk = 0;
System.out.println(" 凭小票可兑换发票!");
break;
}
case 3:
System.out.println(" -------- << 您已退出购物!>> --------");
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(" ---- << 您的输入有误,请重新输入!>> ----");
// System.out.println();
if (num > 2)
{
System.out.println(" 您输入错误超过三次,已退出购物系统!");
break wai;
}
num++;
continue;
}
}
}
}
|
|