ps:注释比较清晰 看题↓
/**
完成一个电商系统的商品库存管理,包含两个类:java
商品类↓web
public class Goods { //商品编号 private int id; //商品名 private String name ; //商品类型 private String type ; //商品价格 private double price; //商品库存 private int sum ; //商品生产地 private String yieldly; //有参构造器 public Goods(int id, String name, String type, double price, int sum, String yieldly) { super(); this.id = id; this.name = name; this.type = type; this.price = price; this.sum = sum; this.yieldly = yieldly; } //set get public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } public String getYieldly() { return yieldly; } public void setYieldly(String yieldly) { this.yieldly = yieldly; } //输出商品信息 public void show() { System.out.println(id+"\t"+name+"\t"+type+"\t"+price+"\t"+sum+"\t"+yieldly); } }
管理类↓小程序
public class Manage { //声明一个Goods数组 private Goods [] list; //声明一个索引 private int index; //对象构造时初始一个数组 public Manage() { list = new Goods[100]; } //商品添加 public void add (Goods g) { list [index++]=g; } //查询指订价格范围的商品并显示 public void findGoodsByPrice(double max , double min) { for(int i =0; i<list.length; i++) { Goods g = list[i]; //判断g对象是否为空,而且是否在参数的范围内 if(g != null && g.getPrice()>=min && g.getPrice()<=max) { //显示当前学生的信息 g.show(); } } } //根据编号查询商品信息并显示 public void findGoodsById(int id) { int i = findByid(id); //判断i是否为-1 if(i != -1) { list[i].show(); } } //根据编号修改商品的单价和库存 public void updatePriceSum(int id,double price,int sum) { int i =findByid(id); //判断i是否为-1 if(i!=-1) { //修改索引为i的商品价格 list[i].setPrice(price); //修改索引为i的商品库存 list[i].setSum(sum); System.out.println("修改为功!"); list[i].show(); }else { System.out.println("位置找到此编号的商品"); } } //显示全部商品信息 public void showAll() { for(int i =0; i<list.length; i++) { Goods g = list[i]; //判断商品对象在数组中是否为空 if(g!=null) { g.show(); } } } //查询全部库存量低于指定数的商品信息 public void findGoodsBySmallSum(int sum) { for(int i =0; i<list.length; i++) { Goods g =list[i]; if(g!=null && g.getSum()<=sum) { g.show(); } } } // //根据提供的编号查询商品对象 public int findByid(int id) { for(int i=0; i<list.length; i++) { Goods g =list[i]; if(g!=null && g.getId() == id) { return i ; } } return -1; } }
测试类数组
public class Test { public static void main(String[] args) { //建立名为m的Manage对象 Manage m =new Manage(); //用名为m的对象去调用Manage中的add方法(方法中建立商品对象并传入相应的参数) m.add(new Goods(1,"奥利奥","饼干",6.0,100,"鄂州")); m.add(new Goods(2,"趣多多","饼干",5.0,100,"武汉")); m.add(new Goods(3,"红牛","饮料",8.0,100,"荆州")); m.add(new Goods(4,"卫龙","零食",3.0,100,"襄阳")); m.add(new Goods(5,"良品铺子","零食",10.0,100,"黄冈")); //用名为m的对象去调用Manage中的showAll方法(打印出目前状态下的全部商品信息) m.showAll(); System.out.println("--------------"); //用名为m的对象去调用Manage中的findGoodsByPrice方法(查询指订价格范围的商品并显示) m.findGoodsByPrice(7, 3); System.out.println("---------------------"); //用名为m的对象去调用Manage中的findGoodsById方法(根据编号查询商品信息并显示) m.findGoodsById(4); System.out.println("--------------"); //用名为m的对象去调用Manage中的updatePriceSum方法(根据编号修改商品的单价和库存) m.updatePriceSum(4, 0.5, 300); System.out.println("---------"); //用名为m的对象去调用Manage中的findGoodsBySmallSum方法(查询全部库存量低于指定数的商品信息) m.findGoodsBySmallSum(300); System.out.println("--------------"); } }
最后看看效果叭
ps:小白一枚,仅供参考!svg