简介
基于对象序列化技术,完成一个小型电商管理系统商品模块,要求包含商品管理相关功能,其中商品类包含以下属性:
id,商品名,类别名,单价,库存量,计量单位,上架时间,修改时间,销量,商品状态(1:正常销售/0:下架);
根据以上提示信息,要求实现如下功能:
- 实现控制台输入商品信息,并记录到文件中
- 列表查询所有商品数据
- 按照不同的信息(价格,库存量,上架时间,销量)对商品排序显示
- 查询出所有下架商品
- 按类别查询出所有的商品
- 修改商品状态(要求同步更新修改时间)
- 修改商品的单价和库存量(要求同步更新修改时间)
- 删除下架商品
代码
package com.xiaoruanzi.test;
import java.io.Serializable;
import java.util.Date;
public class Goods implements Serializable {
/**
* 生成一个随机序列号
*/
private static final long serialVersionUID = 3518876938647252805L;
private int id;
private String gname;
private String tname;
private double price;
private int number;
private int unit;
private Date input;
private Date modify;
private int sale;
private transient int state;
public Goods() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getUnit() {
return unit;
}
public void setUnit(int unit) {
this.unit = unit;
}
public Date getInput() {
return input;
}
public void setInput(Date input) {
this.input = input;
}
public Date getModify() {
return modify;
}
public void setModify(Date modify) {
this.modify = modify;
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
@Override
public String toString() {
return "Goods [id=" + id + ", gname=" + gname + ", tname=" + tname + ", price=" + price + ", number=" + number
+ ", unit=" + unit + ", input=" + input + ", modify=" + modify + ", sale=" + sale + ", state=" + state
+ "]";
}
public Goods(int id, String gname, String tname, double price, int number, int unit, Date input, Date modify,
int sale, int state) {
super();
this.id = id;
this.gname = gname;
this.tname = tname;
this.price = price;
this.number = number;
this.unit = unit;
this.input = input;
this.modify = modify;
this.sale = sale;
this.state = state;
}
}
package com.xiaoruanzi.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class GoodsManage {
public void add(Goods g) {
try {
FileOutputStream fos = new FileOutputStream("src/Goods.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(g);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<Goods> list() throws ClassNotFoundException, IOException {
FileInputStream fis = new FileInputStream("src/Goods.txt");
ObjectInputStream bis = new ObjectInputStream(fis);
ArrayList<Goods> g1 = new ArrayList<Goods>();
Object obj = bis.readObject();
Goods g = (Goods) obj;
g1.add(g);
bis.close();
return g1;
}
}
package com.xiaoruanzi.test;
import java.io.IOException;
import java.util.Date;
public class GoodsTest {
public static void main(String[] args) throws ClassNotFoundException, IOException {
Date now = new Date();
Goods g = new Goods(1, "2", "3", 4.5, 6, 7, now, now, 8, 9);
Goods g2 = new Goods(2, "2", "3", 4.5, 6, 7, now, now, 8, 9);
Goods g3 = new Goods(3, "2", "3", 4.5, 6, 7, now, now, 8, 9);
new GoodsManage().add(g);
System.out.println(new GoodsManage().list());
}
}
运行结果
[Goods [id=1, gname=2, tname=3, price=4.5, number=6, unit=7, input=Mon Aug 19 19:30:52 CST 2019, modify=Mon Aug 19 19:30:52 CST 2019, sale=8, state=0]]
总结
还有一些功能没有实现,有待日后完善