一、父类

 public class Father{                            //父类

     protected static int stock1 = 10;            //库存
protected static int stock2 = 12;
protected static int stock3 = 15;
protected static int stock4 = 21;
protected static int stock5 = 9;
protected static int stock6 = 15; protected static float p1 = 1200; //商品价格
protected static float p2 = 89;
protected static float p3 = 688;
protected static float p4 = 5900;
protected static float p5 = 399;
protected static float p6 = 2588; protected static int integral1=10; //会员积分
protected static int integral2=200;
protected static int integral3=900;
protected static int integral4=3000; protected static int id1 = 1; //商品id
protected static int id2 = 2;
protected static int id3 = 3;
protected static int id4 = 4;
protected static int id5 = 5;
protected static int id6 = 6; int vipName; //会员编号
String name; //商品名
float price; //商品价格
int stock; //商品库存
int integral; //会员积分
int id; //商品id
protected static float sumprice = 0; public Father(String name,float price,int stock,int id){ //有参构造(商品)
this.name = name;
this.price = price;
this.stock = stock;
this.id = id;
}
public Father(int vipName,int integral){ //有参构造(会员)
this.vipName = vipName;
this.integral = integral;
} public Father() {
super();
} }

二、会员登陆

import java.util.Scanner;
public class Welcome extends Father{
Scanner land= new Scanner(System.in);
Father vip0 = new Father(0,integral1); //创建对象(会员)
Father vip1 = new Father(1,integral2);
Father vip2 = new Father(2,integral3);
Father vip3 = new Father(3,integral4);
public void vip(Scanner land){
System.out.println("您好 欢迎光临!\n");
System.out.println("请您先登陆!\n");
System.out.println("请输入会员编号:");
vipName=land.nextInt(); Father [] vip = new Father[4]; //创建对象数组(会员)
vip[0]=vip0;
vip[1]=vip1;
vip[2]=vip2;
vip[3]=vip3; //用户名正确
if(vipName==1||vipName==2||vipName==3||vipName==4||vipName==5||vipName==6){
System.out.println("请输入密码:");
String pasW = land.next();
//密码正确
if(pasW.equals("123")){
System.out.println("登陆成功!\n"+vipName+"号vip,欢迎您!");
System.out.println("您的当前积分为:"+vip[vipName].integral); }
//密码错误
else{
System.out.println("密码错误0.0,请重新输入!");
this.vip(land);
}
}
//用户名错误
else{
System.out.println("用户不存在0.0,请重新输入!");
this.vip(land);
}
}
public static void main(String[]args){
Scanner land= new Scanner(System.in);
Welcome landVip = new Welcome();
landVip.vip(land); } }

三、商品列表及购买

import java.util.Scanner;
public class CommodityList extends Welcome{
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= new Scanner(System.in); public void allCom(){ 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 void shopping(){
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 void main(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(); }
}
05-11 05:10