package cn.jbit.market;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Market {
public Market(){
init();
}
Goods [] goods=new Goods[1000];// 超市商品
Person person=new Person();
Scanner input=new Scanner(System.in);
private void init() {
goods[0]=new Goods();
goods[0].name="火腿肠";
goods[0].id=101;
goods[0].dicount=100;
goods[0].price=2.0;
goods[0].count=1000;
goods[1]=new Goods();
goods[1].name="优乐美";
goods[1].id=102;
goods[1].dicount=100;
goods[1].price=4;
goods[1].count=20;
goods[2]=new Goods();
goods[2].name="老干妈";
goods[2].id=103;
goods[2].dicount=100;
goods[2].price=8.0;
goods[2].count=80;
goods[3]=new Goods();
goods[3].name="薯片";
goods[3].id=104;
goods[3].dicount=100;
goods[3].price=6.0;
goods[3].count=900;
goods[4]=new Goods();
goods[4].name="火腿肠";
goods[4].id=105;
goods[4].dicount=100;
goods[4].price=2.0;
goods[4].count=1000;
goods[5]=new Goods();
goods[5].name="ddd";
goods[5].id=106;
goods[5].dicount=100;
goods[5].price=2.0;
goods[5].count=1000;
goods[6]=new Goods();
goods[6].name="大米";
goods[6].id=107;
goods[6].dicount=100;
goods[6].price=2.0;
goods[6].count=1000;
goods[7]=new Goods();
goods[7].name="77腿肠";
goods[7].id=108;
goods[7].dicount=100;
goods[7].price=2.0;
goods[7].count=1000;
}
public void showGoods(){
p("商品编号\t商品名称\t 单价\t 折扣 \t 库存");
for (Goods gd : goods) {
if(gd!=null){
p(gd.id+"\t"+gd.name+"\t"+gd.price+"\t"+gd.dicount/10+"\t"+gd.count);
}
}
}
public void menu() {
p("欢迎来到xx超市");
while(true){
showGoods();
p("请选择要购买的商品");
int id=input.nextInt();
p("请输入要购买的数量");
int num=input.nextInt();
addShoppingCar(id,num);
p("是否继续购买y/n");
char c=input.next().charAt(0);
if(c=='n'){
settle();
break;
}
}
}
// 添加购物车
private void settle() {
p("欢迎光临xx超市");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
p("打印时间"+sdf.format(new Date()));
p("******************");
p("商品名称\t 单价\t 数量\t 折扣\t 小计");
double sum=0;// 总金额
int totalCount=0;
for (Goods g : person.goods) {
if(g!=null){
double subtotal=g.count*g.price*g.dicount/100;// 计算小计
sum+=subtotal;
totalCount+=g.count;
p(g.name+"\t"+g.price+"\t"+g.count+"\t"+g.dicount/10+"\t"+subtotal);
}
}
p("*****************************");
p("购物的总数量为"+totalCount);
p("优惠后的总计为"+sum);
p("请付款");
double money=input.nextDouble();
p("收款金额为"+money);
p("找零"+(money-sum));
int score=(int) (sum/3);// 三块钱一分
person.score+=score;
p("本次消费积分为"+score);
p("总积分为"+person.score);
}
private void addShoppingCar(int id,int num) {
for (Goods g : goods) {
// 要找到我要购买是商品
if(g!=null&&g.id==id){
// 库存数量大于购买数量
if(g.count>=num){
for (int i = 0; i < person.goods.length; i++) {
// 找到我购物车的空位
if(person.goods[i]!=null&&person.goods[i].id==id){
person.goods[i].count+=num;
g.count-=num;
p("购买成功");
return ;
}
}
for (int i = 0; i < person.goods.length; i++) {
// 找到我购物车的空位
if(person.goods[i]==null){
person.goods[i]=new Goods();
person.goods[i].id=g.id;
person.goods[i].name=g.name;
person.goods[i].price=g.price;
person.goods[i].dicount=g.dicount;
person.goods[i].count=num;
g.count-=num;
p("购买成功");
return ;
}
}
p("购物车满了");
}else{
p("库存不足");
return;
}
}
}
p("没有找到该商品");
}
public void p(Object obj) {
System.out.println(obj);
}
}
超市管理 java
最新推荐文章于 2023-01-07 00:29:05 发布