主类
import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class TickstingManagmentSystem {
public static void main(String[] args) throws FileNotFoundException {
//定义超市列表对象数组
Goods[] goods =new Goods[100];
//导入文件,文件位置自行修改
@SuppressWarnings("resource")
Scanner scanner = new Scanner(new File("D:\\code\\ceshi01\\src\\com\\msq\\Goods.txt"));
int goodsIndex=0;
while (scanner.hasNext()){
String str = scanner.next();
//分割字符串
String[] strings = str.split("\\|");
//填入对应的数值
//超市商品列表
goods[goodsIndex] = new Goods();
goods[goodsIndex].name = strings[0];
goods[goodsIndex].number = Double.valueOf(strings[1]);
goods[goodsIndex].price = Double.valueOf(strings[2]);
goods[goodsIndex].unit =strings[3];
goodsIndex++;
}
double payPrice=0;
//操作首页
System.out.println(" 超市票务管理系统");
System.out.println("本超市商品清单如下:");
System.out.println("----------------------------");
System.out.println("编号 "+"名称 "+"单价 "+"单位 ");
// i 商品index
int i;
for(i=0;i<goodsIndex;i++)
{
System.out.printf("%-1.0f %-3s %10.2f %-3s ",goods[i].number,goods[i].name,goods[i].price,goods[i].unit);
System.out.println();
}
System.out.println("----------------------------");
//操作类型
System.out.println("欢迎光临!!!小管 很高兴为您服务^_^");
System.out.println("祝您购物愉快哟(づ ̄3 ̄)づ╭❤~");
System.out.println("请选择您的操作:");
System.out.println(" 1.开始购物");
System.out.println(" 2.结账");
System.out.println(" 3.打印商品清单");
System.out.println(" 0.退出系统");
//输入操作编号
int n = 0;
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.printf("请输入操作编号:",n);
n=sc.nextInt();
while(n!=0)
{
if(n==1)//开始购物
{
double m = 0 ;//购买商品编号
System.out.printf("请输入你需要购买的商品编号(0退出):",m);
m=sc.nextDouble();
for(i=0;m!=0;i++)
{
if(m>goodsIndex+100|m<101)
System.out.println(" 亲!本店没有该商品呢-_-|| ");
else {
System.out.println("您选择的商品是:名称:"+goods[(int) (m-101)].name+" 编号:"+(int)goods[(int) (m-101)].number+" 单价: "+goods[(int) (m-101)].price+" 单位:"+goods[(int) (m-101)].unit);
System.out.printf("请输入购买数量:",goods[(int) (m-101)].amount);
goods[(int) (m-101)].amount=sc.nextDouble();
goods[(int) (m-101)].total=goods[(int) (m-101)].price*goods[(int) (m-101)].amount;
System.out.println("名称:"+goods[(int) (m-101)].name+" 编号:"+(int)goods[(int) (m-101)].number+" 单价:"+goods[(int) (m-101)].price+" 单位:"+goods[(int) (m-101)].unit+" 数量:"+goods[(int) (m-101)].amount+" 价钱:"+(double)Math.round(goods[(int) (m-101)].total*100)/100);
}
System.out.printf("请输入你需要购买的商品编号(0退出):",m);
m=sc.nextDouble();
}
}
if(n==2)//结账
{
System.out.println("------------万德隆超市------------");
double totalPrice=0;//商品总价
double totalAmount=0;//商品总数量
for(i=0;i<goodsIndex;i++)
{
if(goods[i].total!=0)
{
totalPrice=totalPrice+goods[i].total;
totalAmount=totalAmount+goods[i].amount;
}
}
//折扣
double totalRedution;
if(totalPrice>0&totalPrice<=100)
totalRedution=0;
else if(totalPrice>100&totalPrice<=200)
totalRedution=totalPrice*0.1;
else if(totalPrice>300&totalPrice<=300)
totalRedution=totalPrice*0.2;
else {
totalRedution=totalPrice*0.3;
}
if(totalPrice==0)
{
System.out.println("亲!您还没有购买任何商品奥,请先选择商品哈 ̄ω ̄||");
}
else {
//实付金额
double actualPrice;
actualPrice=totalPrice-totalRedution;
System.out.println("您一共购买了"+(int)totalAmount+"件商品");
//支付方式
System.out.println("支付方式有:1.支付宝 2.微信 3.刷卡 4.现金");
String way[]={"支付宝","微信","刷卡","现金"};
int p=0;
System.out.printf("请选择您的支付方式:",p);
p=sc.nextInt();
System.out.println("您的支付方式为"+"-----"+way[p-1]);
//支付金额
System.out.printf("您的付款金额为:",payPrice);
payPrice=sc.nextDouble();
if(payPrice<totalPrice)
{
System.out.printf("真是抱歉,您付的钱不够呢,请重新输入付款金额:",payPrice);
payPrice=sc.nextInt();
}
System.out.println("您的优惠金额为:"+(double)Math.round(totalRedution*100)/100+"元");
System.out.println("您的实付金额为:"+(double)Math.round(actualPrice*100)/100+"元");
//找零
double changePrice=payPrice-actualPrice;
System.out.println("您的找零金额为:"+(double)Math.round(changePrice*100)/100+"元");
System.out.println("-------------------------");
System.out.println("万德隆超市感谢您的光临^_^");
}
}
if(n==3)//打印商品清单
{
if(payPrice==0){
System.out.println("亲,需要先结账才能打印商品清单呢~");
}
else{
double totalPrice=0;//商品总价
double totalAmount=0;//商品总数量
for(i=0;i<goodsIndex;i++)
{
if(goods[i].total!=0)
{
totalPrice=totalPrice+goods[i].total;
totalAmount=totalAmount+goods[i].amount;
}
}
//折扣
double totalRedution;
if(totalPrice>0&totalPrice<=100)
totalRedution=0;
else if(totalPrice>100&totalPrice<=200)
totalRedution=totalPrice*0.1;
else if(totalPrice>300&totalPrice<=300)
totalRedution=totalPrice*0.2;
else {
totalRedution=totalPrice*0.4;
}
//实付金额
double actualPrice;
actualPrice=totalPrice-totalRedution;
//找零
double changePrice=payPrice-actualPrice;
//打印顶部
System.out.println("-------------------------");
System.out.println("本人确认以上交易,同意将其计入本卡账户");
System.out.println(" 万德隆南师店");
System.out.println("收银员:1094 "+"收银机号:007");
SimpleDateFormat df = new SimpleDateFormat("日期:yyyy-MM-dd 时间:HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date(n)));
//打印中部
System.out.println("-------------------------");
System.out.println("商品编号 "+"商品名称"+" 数量"+"*"+"单价"+" 小计");
for(i=0;i<6;i++)
{
if(goods[i].total!=0)
{
System.out.printf("%-1.0f %-3s %5.2f*%5.2f %5.2f ",goods[i].number,goods[i].name,goods[i].amount,goods[i].price,(double)Math.round(goods[i].total*100)/100);
//System.out.println((int)goods[i].number+" "+goods[i].name+" "+goods[i].amount+"*"+goods[i].price+" "+(double)Math.round(goods[i].total*100)/100);
System.out.println();
}
}
System.out.println("-------------------------");
System.out.println("支付金额:"+payPrice );
System.out.printf("合计金额:%5.2f 总折扣:%5.2f",totalPrice,totalRedution);
System.out.println();
System.out.println("实付金额:"+actualPrice);
System.out.println("找零:"+(double)Math.round(changePrice*100)/100+"元");
System.out.println("--------------------------");
System.out.println(" 请保管好您的购物小票");
System.out.println(" 购物小票是您退款换货的凭证");
System.out.println(" 请凭此票到购物台开取发票^_^");
}
}
System.out.println();
System.out.println("欢迎光临!!!小管 很高兴为您服务^_^");
System.out.println("祝您购物愉快哟(づ ̄3 ̄)づ╭❤~");
System.out.println("请选择您的操作:");
System.out.println(" 1.开始购物");
System.out.println(" 2.结账");
System.out.println(" 3.打印商品清单");
System.out.println(" 0.退出系统");
System.out.printf("请输入操作编号:",n);
n=sc.nextInt();
}
System.out.println("本次服务已结束^_^");
System.out.println(" 欢迎下次光临ヾ(o´∀`o)ノ ");
System.out.println("您的满意是我们服务的宗旨!!!");
}
}
商品类
public class Goods {
String name;//商品名称
double number;//商品编号
double price;//商品单价
String unit;//商品单位
double amount;//商品数量
double total;//商品合计
}
文本文件
Goods.txt
牛奶|101|3.0|袋
饼干|102|23.0|盒
苹果|103|3.2|Kg
香梨|104|2.65|Kg
香蕉|105|6.8|Kg
石榴|106|7.2|Kg
橘子|107|5.2|Kg
香肠|108|2.0|根