java写会员购物_java小程序-----用if for写会员登陆和商品列表

这篇博客展示了如何使用Java编程实现会员购物系统,包括商品列表的展示和分类。通过Scanner获取用户输入,根据输入选择显示所有商品或分类商品列表,并进行商品购买操作,更新商品库存和总价。
摘要由CSDN通过智能技术生成

importjava.util.Scanner;public class CommodityList extendsWelcome{

Father com1= new Father("格力空调 ",p1,stock1,id1); //创建对象(商品)

Father com2 = new Father("蓝色圆领polo衫(男)",p2,stock2,id2);

Father com3= new Father("Nice平底运动鞋透气型",p3,stock3,id3);

Father com4= new Father("华硕笔记本FL5900 ",p4,stock4,id4);

Father com5= new Father("adidas黑白户外双肩背包",p5,stock5,id5);

Father com6= new Father("chanel男士香水",p6,stock6,id6);

Father comAll= new Father(); //无参构造对象

Scanner sort= newScanner(System.in);public voidallCom(){

Object selected=sort.next();

Father [] com= new Father[6]; //商品对象数组

com[0] = com1; //创建对象(商品)

com[1] =com2;

com[2] =com3;

com[3] =com4;

com[4] =com5;

com[5] =com6;if(selected.equals("all")|selected.equals("0")|selected.equals("所有")){ //所有商品列表

for(int i=0;i<6;i++){ //遍历打印数组

System.out.print("name:"+com[i].name+" \t");

System.out.print("stock:"+com[i].stock+" \t");

System.out.print("price:"+com[i].price+" \t");

System.out.print("id:"+com[i].id+"\t");

System.out.println();

System.out.println();

}

}else if(selected.equals("assort")|selected.equals("1")|selected.equals("分类")){//分类商品列表

System.out.println('\u2605'+"数码电器"+'\u2605');

System.out.print("name:"+com[0].name+" \t");

System.out.print("stock:"+com[0].stock+" \t");

System.out.print("price:"+com[0].price+" \t");

System.out.print("id:"+com[0].id+"\t");

System.out.println();

System.out.println();

System.out.print("name:"+com[3].name+" \t");

System.out.print("stock:"+com[3].stock+" \t");

System.out.print("price:"+com[3].price+" \t");

System.out.print("id:"+com[3].id+"\t");

System.out.println();

System.out.println();

System.out.println('\u2605'+"男士专区"+'\u2605');

System.out.print("name:"+com[1].name+" \t");

System.out.print("stock:"+com[1].stock+" \t");

System.out.print("price:"+com[1].price+" \t");

System.out.print("id:"+com[1].id+"\t");

System.out.println();

System.out.println();

System.out.print("name:"+com[5].name+" \t");

System.out.print("stock:"+com[5].stock+" \t");

System.out.print("price:"+com[5].price+" \t");

System.out.print("id:"+com[5].id+"\t");

System.out.println();

System.out.println();

System.out.println('\u2605'+"户外运动"+'\u2605');

System.out.print("name:"+com[2].name+" \t");

System.out.print("stock:"+com[2].stock+" \t");

System.out.print("price:"+com[2].price+" \t");

System.out.print("id:"+com[2].id+"\t");

System.out.println();

System.out.println();

System.out.print("name:"+com[4].name+" \t");

System.out.print("stock:"+com[4].stock+" \t");

System.out.print("price:"+com[4].price+" \t");

System.out.print("id:"+com[4].id+"\t");

System.out.println();

System.out.println();

}else{

System.out.println("输入错误!\n请重新输入");this.allCom();

}

}public voidshopping(){

Father [] com= new Father[6]; //商品对象数组

com[0] = com1; //创建对象(商品)

com[1] =com2;

com[2] =com3;

com[3] =com4;

com[4] =com5;

com[5] =com6;//是否购买判断

System.out.println("是否购买商品(yes/no/是/否)");

String sure=sort.next();//购买

if(sure.equals("yes")|sure.equals("是")){

System.out.println("请输入您选中的商品id");int shopId =sort.nextInt();for(int i=0;shopId<=com.length;i++){

comAll.sumprice+=com[shopId-1].price; //商品总价格

--com[shopId-1].stock; //库存-1

System.out.println("继续添加?(yes/no)");

String cont=sort.next();//继续

if(cont.equals("yes")){

System.out.println("请输入您选中的商品id");

shopId=sort.nextInt();

}//不继续

else if(cont.equals("no")){

System.out.println("谢谢惠顾!您购买的商品总价格为:"+comAll.sumprice+"元!积分增加"+comAll.sumprice/100);break;

}//其他回答

else{

System.out.println("不好意思,没听清!");

CommodityList Com= new CommodityList(); //创建对象

Com.shopping();

}

}

}//不购买

else if(sure.equals("no")|sure.equals("否")){

System.out.println("欢迎下次光临!");

}//输入错误

else{

System.out.println("不好意思,没听清!请您再说一次0.0");

CommodityList Com= new CommodityList(); //创建对象

Com.shopping();

}

}public static voidmain(String[]args){

CommodityList Com= new CommodityList(); //创建对象

System.out.println("0 所有商品列表"); //提示信息

System.out.println("1 商品分类列表");

System.out.println("请选择商品显示列表(0/1/all/assort/“所有”/“分类”)");//提示输入信息

Com.allCom();

Com.shopping();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值