定义超市对象数组 还是一个一个输入
不过今天Java课 Scanner sc=new Scanner(new File("f:\\a.txt"));
语句可以实现在文本中导入数据 下次再尝试完善一下吧^_^
感觉和C++的也差不多
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class TickstingManagmentSystem {
public static void main(String[] args) {
//定义超市列表对象数组
Goods[] s =new Goods[6];
//超市商品列表
//牛奶
Goods milk = new Goods();
milk.name="牛奶";
milk.number=101;
milk.price=3.0;
milk.unit="袋";
s[0]=milk;
//饼干
Goods cookie =new Goods();
cookie.name="饼干";
cookie.number=102;
cookie.price=23.0;
cookie.unit="盒";
s[1]=cookie;
//苹果
Goods apple =new Goods();
apple.name="苹果";
apple.number=103;
apple.price=3.2;
apple.unit="Kg";
s[2]=apple;
//梨
Goods pea =new Goods();
pea.name="香梨";
pea.number=104;
pea.price=2.65;
pea.unit="Kg";
s[3]=pea;
//香蕉
Goods banana =new Goods();
banana.name="香蕉";
banana.number=105;
banana.price=6.8;
banana.unit="Kg";
s[4]=banana;
//石榴
Goods pomegranate =new Goods() ;
pomegranate.name="石榴";
pomegranate.number=106;
pomegranate.price=7.2;
pomegranate.unit="Kg";
s[5]=pomegranate;
double payPrice=0;
//操作首页
System.out.println(" 超市票务管理系统");
System.out.println("本超市商品清单如下:");
System.out.println("------------------------");
System.out.println("编号 "+"名称 "+"单价 "+"单位 ");
int i;
for(i=0;i<6;i++)
{
System.out.printf("%-1.0f %-2s %14.2f %2s ",s[i].number,s[i].name,s[i].price,s[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>106|m<101)
System.out.println(" 亲!本店没有该商品呢-_-|| ");
else {
System.out.println("您选择的商品是:名称:"+s[(int) (m-101)].name+" 编号:"+(int)s[(int) (m-101)].number+" 单价: "+s[(int) (m-101)].price+" 单位:"+s[(int) (m-101)].unit);
System.out.printf("请输入购买数量:",s[(int) (m-101)].amount);
s[(int) (m-101)].amount=sc.nextDouble();
s[(int) (m-101)].total=s[(int) (m-101)].price*s[(int) (m-101)].amount;
System.out.println("名称:"+s[(int) (m-101)].name+" 编号:"+(int)s[(int) (m-101)].number+" 单价:"+s[(int) (m-101)].price+" 单位:"+s[(int) (m-101)].unit+" 数量:"+s[(int) (m-101)].amount+" 价钱:"+(double)Math.round(s[(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<6;i++)
{
if(s[i].total!=0)
{
totalPrice=totalPrice+s[i].total;
totalAmount=totalAmount+s[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)//打印商品清单
{
double totalPrice=0;//商品总价
double totalAmount=0;//商品总数量
for(i=0;i<6;i++)
{
if(s[i].total!=0)
{
totalPrice=totalPrice+s[i].total;
totalAmount=totalAmount+s[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(s[i].total!=0)
{
System.out.println((int)s[i].number+" "+s[i].name+" "+s[i].amount+"*"+s[i].price+" "+(double)Math.round(s[i].total*100)/100);
System.out.println();
}
}
System.out.println("-------------------------");
System.out.println("支付金额:"+payPrice );
System.out.println("合计金额:"+totalPrice+" 总折扣:"+totalRedution);
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("您的满意是我们服务的宗旨!!!");
}
}
我果然还是太菜─━ _ ─━✧
嚯嚯嚯
差点忘记了 还有个class
public class Goods {
String name;//商品名称
double number;//商品编号
double price;//商品单价
String unit;//商品单位
double amount;//商品数量
double total;//商品合计
}