文件存储型商品管理系统

io实现数据保存商品管理系统

一、简介

因为每次添加商品都无法保存下来,重新运行后又要重更新添加,于是需要一个容器来存储这些数据,显然数组已经无法满足这样的要求了,所以利用文件来存储相应的动态数组,每次在操作功能时将数组从文件中取出来访问。

二、具体思路

先创建一个实体类,方法类。将所有数据的改变都存储到数组中去,再通过封装两个方法,一个存储数组的方法,一个从文件中取出数组的方法,当涉及到数据的改变时,需要再在调用一下存储方法。在每个方法前都需要调用取出方法

三、具体代码

public class Goods {
	private int id;
	private String gname;
	private String  cname;
	private double price;
	private int kc;
	private String jl;
	Date saldate;
	Date update;
	private int snum;
	private int staus;
	public Goods() {
		super();
	}
	
	public Goods(int id, String gname, String cname, double price, int kc, String jl, Date saldate, Date update,
			int snum, int staus) {
		super();
		this.id = id;
		this.gname = gname;
		this.cname = cname;
		this.price = price;
		this.kc = kc;
		this.jl = jl;
		this.saldate = saldate;
		this.update = update;
		this.snum = snum;
		this.staus = staus;
	}
	public int getStaus() {
		return staus;
	}
	public void setStaus(int staus) {
		this.staus = staus;
	}
	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 getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getKc() {
		return kc;
	}
	public void setKc(int kc) {
		this.kc = kc;
	}
	public String getJl() {
		return jl;
	}
	public void setJl(String jl) {
		this.jl = jl;
	}
	public Date getSaldate() {
		return saldate;
	}
	public void setSaldate(Date saldate) {
		this.saldate = saldate;
	}
	public Date getUpdate() {
		return update;
	}
	public void setUpdate(Date update) {
		this.update = update;
	}
	public int getSnum() {
		return snum;
	}
	public void setSnum(int snum) {
		this.snum = snum;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cname == null) ? 0 : cname.hashCode());
		result = prime * result + ((gname == null) ? 0 : gname.hashCode());
		result = prime * result + id;
		result = prime * result + ((jl == null) ? 0 : jl.hashCode());
		result = prime * result + kc;
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((saldate == null) ? 0 : saldate.hashCode());
		result = prime * result + snum;
		result = prime * result + staus;
		result = prime * result + ((update == null) ? 0 : update.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Goods other = (Goods) obj;
		if (cname == null) {
			if (other.cname != null)
				return false;
		} else if (!cname.equals(other.cname))
			return false;
		if (gname == null) {
			if (other.gname != null)
				return false;
		} else if (!gname.equals(other.gname))
			return false;
		if (id != other.id)
			return false;
		if (jl == null) {
			if (other.jl != null)
				return false;
		} else if (!jl.equals(other.jl))
			return false;
		if (kc != other.kc)
			return false;
		if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
			return false;
		if (saldate == null) {
			if (other.saldate != null)
				return false;
		} else if (!saldate.equals(other.saldate))
			return false;
		if (snum != other.snum)
			return false;
		if (staus != other.staus)
			return false;
		if (update == null) {
			if (other.update != null)
				return false;
		} else if (!update.equals(other.update))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Goods [id=" + id + ", gname=" + gname + ", cname=" + cname + ", price=" + price + ", kc=" + kc + ", jl="
				+ jl + ", saldate=" + saldate + ", update=" + update + ", snum=" + snum + ", staus=" + staus + "]";
	}
	
}
public class Storger {
	static File f =new File("src/Goods.txt");
	
	public void storge(List<Goods> list){
		FileOutputStream fos =null; 
		ObjectOutputStream oos = null;
				try {
					fos=new FileOutputStream(f);
					oos = new ObjectOutputStream(fos);
					oos.writeObject(list);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					if(fos!=null)
						try {
							fos.close();
							if(oos!=null)oos.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
	}
	public List<Goods> read(List<Goods> list){
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(f);
			ois = new ObjectInputStream(fis);
			if(fis.available()>0){
				Object obj=ois.readObject();
				list = (ArrayList<Goods>) obj;
				return list;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}

具体方法类在前面已经实现过,代码就不在展示了。需要的可以参考之前的博客。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值