承接上面的代码
//付账(打印小票)方法
public static void printReceipt() {
// 付账:打印小票
// ---------票头-----------------------------------------
// 定义变量:品名 编号 售价 数量 金额
System.out.println(" 欢 迎 光 临");
System.out.println();
System.out
.println(" 品名 售价 数量 金额");
System.out
.println("-------------------------------------------------------------------");
// --------票体-----------------------------------------
// 货物超过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 + " " + slsNumber
+ " " + slsMoney);
System.out.println(rgName + "(" + rgID + ") "
+ rgPrice + " " + rgNumber + " "
+ rgMoney);
System.out.println(skName + "(" + skID + ") " + skPrice
+ " " + skNumber + " " + skMoney);
// -------票脚------------------------------------------
System.out
.println("-------------------------------------------------------------------");
// 项数
int totalItemsNumber = 3;
// 共计
int totalNumber = slsNumber + rgNumber + skNumber;
// 总计
// 加入打折逻辑:
/*
* 如果商品总价超过300元,打95折 如果商品总价超过600元,打9折 如果商品总价超过1000元,打8折
* 如果商品总价超过1000元,并且少林寺酥饼核桃购买超过30份, 则总价打8折后,立减20
*/
double beforeTotalMoney = slsMoney + rgMoney + skMoney;
double totalMoney = beforeTotalMoney;
if (300 <= beforeTotalMoney && beforeTotalMoney < 600) {
totalMoney = beforeTotalMoney * 0.95;
} else if (600 <= beforeTotalMoney && beforeTotalMoney < 1000) {
totalMoney = beforeTotalMoney * 0.9;
} else if (1000 <= beforeTotalMoney) {
totalMoney = beforeTotalMoney * 0.8;
}
if (1000 <= beforeTotalMoney && slsNumber >= 30) {
totalMoney -= 20;
}
// 将总计四舍五入,并强转为整数
totalMoney = Math.round(totalMoney);
System.out.println(totalItemsNumber + "项商品 共计:"
+ totalNumber + "件");
System.out.println("总计:" + (int) totalMoney);
System.out.println("凭此小票换取发票!");
//记录本次交易流水
flowData[flowNumber] = (int)totalMoney;
//每次记录流水后,流水号自增
flowNumber++;
}
//数据重置
public static void resetData() {
slsNumber = 0;
slsMoney = 0;
rgNumber = 0;
rgMoney = 0;
skNumber = 0;
skMoney = 0;
}
//定义老板的查账方法
public static void check() {
for(;;) {
System.out.println("老板欢迎您!您要干嘛呀?");
System.out.println("1:流水查询 2:总收入查询 3:退出查询");
//1流水查询 2总收入查询 3退出查询
int choose = enterNumber();
switch (choose) {
case 1:
checkFlow();
break;
case 2:
checkTotalMoney();
break;
case 3:
System.out.println("欢迎老板下次光临!");
return;
default:
System.out.println("您输入的内容有误,请重新输入!");
break;
}
}
}
//查询流水的方法
public static void checkFlow() {
if(flowNumber==0) {
System.out.println("老板,今天没开张!");
return;
}
for (int i = 0; i < flowNumber; i++) {
System.out.println(flowData);
}
}
//查询总账方法
public static void checkTotalMoney() {
int sum = 0;
for (int i = 0; i < flowNumber; i++) {
sum += flowData;
}
System.out.println("目前为止,今天的收入为:"+sum);
}
}
|