完成一个电商系统的商品库存管理,包含两个类:
- 商品类(编号,名称,类别,单价,库存量,生产地)
- 商品管理类
要求在管理类中完成如下功能:
a. 商品添加
b. 查询指定价格范围的商品并显示
c. 根据编号查询商品信息并显示
d. 根据编号修改商品的单价和库存
e. 显示所有商品信息
f. 查询所有库存量低于指定数的商品信息
Goods类:
package com.softeem.work.goods;
public class Goods {
int sno;
String name;
String type;
int price;
int store;
String birthplace;
public Goods(int sno, String name, String type, int price, int store, String birthplace) {
super();
this.sno = sno;
this.name = name;
this.type = type;
this.price = price;
this.store = store;
this.birthplace = birthplace;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
public String getBirthplace() {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}
public void details() {
System.out.println(sno + "\t" + name + "\t" + type + "\t" + price + "\t" + store + "\t" + birthplace + "\t");
}
}
GoodManager类:
package com.softeem.work.goods;
public class GoodsManager {
private Goods[] list;
private int index;
public GoodsManager() {
list = new Goods[100];
}
public void findBySno1(int sno) {
int i = findBySno(sno);
Goods g = list[i];
if (i == -1) {
System.out.println("未找到该商品");
} else {
g.details();
}
}
/**
* 根据编号显示某个商品的信息
*/
public int findBySno(int sno) {
for (int i = 0; i < list.length; i++) {
Goods g = list[i];
if (g != null && g.getSno() == sno) {
return i;
}
}
return -1;
}
/**
* 添加某个商品的信息
*/
public void add(Goods g) {
list[index++] = g;
}
/***
* 展示所有信息
*/
public void show() {
for (int i = 0; i < list.length; i++) {
Goods g = list[i];
if (g != null) {
g.details();
}
}
}
/**
* 根据指定价格的范围显示商品信息
*
* @param price
*/
public void assignShow(int price) {
for (int i = 0; i < list.length; i++) {
Goods g = list[i];
if (g != null && g.getPrice() >= price) {
g.details();
}
}
}
/**
* 根据编号修改商品价格和库存量
*
* @param sno
* @param price
* @param store
*/
public void change(int sno, int price, int store) {
int i = findBySno(sno);
if (i == -1) {
System.out.println("未找到该商品");
} else {
list[i].setPrice(price);
list[i].setStore(store);
}
}
/**
* 查询库存量低于指定数商品的信息
*
* @param store
*/
public void showLowStore(int store) {
for (int i = 0; i < list.length; i++) {
Goods g = list[i];
if (g != null && g.store < store) {
g.details();
}
}
}
}
Test类:
package com.softeem.work.goods;
public class Test {
public static void main(String[] args) {
GoodsManager gm = new GoodsManager();
gm.add(new Goods(10001, "王老吉", "饮品", 5, 10, "武汉"));
gm.add(new Goods(10002, "棒棒糖", "零食", 1, 100, "武汉"));
gm.add(new Goods(10003, "雪碧", "饮品", 3, 20, "武汉"));
gm.add(new Goods(10004, "可口可乐", "饮品", 3, 50, "武汉"));
gm.add(new Goods(10005, "老干妈", "干货", 10, 10, "武汉"));
gm.show();
System.out.println("根据编号显示商品的信息=================================");
gm.findBySno1(10003);
System.out.println("根据编号更改商品的价格、库存量====================================");
gm.change(10005, 20, 100);
gm.show();
System.out.println("根据商品价格指定范围显示商品信息===============================");
gm.assignShow(2);
System.out.println("根据提供低于商品价格显示信息=========================");
gm.showLowStore(30);
}
}
测试: