T1.完成理工超市系统的商品类及其子类的定义,实现“浏览商品”及“查看商品详情”功能
(1)商品类
public class Goods {
public String name;
public double price;
public int count;
public String desc;
public Goods(String name, double price, int count){
this.name=name;
this.price=price;
this.count=count;
}
@Override
public String toString() {
return this.name+"\t"+this.price+"\t"+this.count;
}
public void detail(){
System.out.println("商品名称"+this.name);
System.out.println("商品价格"+this.price);
System.out.println("商品库存"+this.count);
System.out.println("商品描述"+this.desc);
}
}
(2)浏览商品部分代码
private static String[] goods=new String[50];
private static double[] prices=new double[50];
private static int[] count=new int[50];
public static void goodslist(Scanner input){
System.out.println("商品列表");
System.out.println("==========================");
System.out.println("编号\t 商品名称\t 单价\t 数量\t");
for (int i = 0; i <3 ; i++) {
System.out.println((i+1)+"\t"+goodlist[i].name+"\t"+goodlist[i].price+"\t"+goodlist[i].count);
}
System.out.println("==================================");
}
public static void initial(){
Goods m17=new Goods("小米手机7",1999,10);
m17.desc="小米7将采用刘海屏设计,搭载Synaptics和Goodix的屏下指纹方案,有蓝色和黑色等颜色";
goodlist[0]=m17;
Goods m18=new Goods("小米手机8",2199,10);
m18.desc="搭载骁龙845处理器,拥有红外人脸识别、双频GPS等技术,具备AI双摄、光学变焦和光学防抖等功能";
goodlist[1]=m17;
Goods honor=new Goods("华为荣耀",2699,5);
honor.desc="搭载了华为海思自主研发的植桥920芯片。该芯片基于28nm工艺制造,采用8核big.little GTS架构";
goodlist[2]=honor;
}
}
(3)查看商品详情部分代码
case 4:
System.out.println("查看商品详情");
System.out.println("请输入需要查看的商品编号");
int goodNum= input.nextInt();
while(goodNum<1||goodNum>3){
System.out.println("当前输入的商品编号不合法,请重新输入");
goodNum= input.nextInt();
}
goodlist[goodNum-1].detail();
continue;
(4)运行截图