写留的商务超市管理系统~~~第二部分

紧跟着上一章~

随后我们实现goodbiz的Goods类和SalesmanGroup类,这个有点类似MVC里面的控制器,在底层进行增删改查的处理

这里就很快明白了我们为什么底层还要再放good和salesman类,因为他们是我的map里面保存的对象,只作为保存的对象而其实并不参与业务

我觉得只用java实现的弊端在于没有mysql之类的数据库在后端对数据进行存储,于是我就用了Map<String,Good/Salesman>来对每一对对象进行存储

整体的思路如下图:


/**
 * 商品信息库
 */
package com.pb.goodbiz;

import java.io.File;
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.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.pb.good.Good;

//@SuppressWarnings("serial")
public class Goods  {

	@SuppressWarnings("unused")
	private Good good = new Good();

	// 定义HashMap集合,用于存放商品信息。因为HashMap是健、值成对存放的
	// 所以使用HashMap做增、删、改、查比较方便
	public static Map<String, Good> goodMap = new HashMap<String, Good>();
	
	//从硬盘直接读取信息
	public Goods(){
		ObjectInputStream ois = null;
		File file = new File("d:/mydoc/goods.txt");
		try {
			if(!file.exists())
				file.createNewFile();
			ois = new ObjectInputStream(new FileInputStream(file));//就这么写!
			try {
				List list = (List) ois.readObject();
				goodMap = (Map<String,Good>) list.get(0);
				Good.count = (Integer) list.get(1);
				Good.alertnum = (Integer) list.get(2);
			} catch (ClassNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	// 初始化HashMap,使用反序列化,从硬盘读取信息。
	//其实用constructor直接new出来也可以
//	@SuppressWarnings("unchecked")
//	public static void initializeUser() {
//		ObjectInputStream ois = null;
//		try {
//			File file = new File("d:/mydoc/goods.txt");
//			if (!file.exists())
//				file.createNewFile();
//			ois = new ObjectInputStream(new FileInputStream(file));
//			try {
//				List list = (List) ois.readObject();
//				goodMap = (Map<String, Good>) list.get(0);
//				Good.count = (Integer) list.get(1);
//				Good.alertnum = (Integer) list.get(2);
//			} catch (ClassNotFoundException e) {
//				e.printStackTrace();
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

	// 增加商品
	public void addGood(Good good) {
		goodMap.put(good.getName(), good);
	}

	// 修改商品
	public void modifyGood(Good good) {
		goodMap.put(good.getName(), good);
	}

	// 删除商品
	public void deleteGood(String name) {
		goodMap.remove(name);
	}

	// 商品入库
	public void in(String name, int num) {
		goodMap.get(name).setStorage(goodMap.get(name).getStorage() + num);
	}

	// 商品出库
	public void out(Good good, int num) {
		good.setStorage(good.getStorage() - num);
	}

	// 设置警戒库存、检查商品库存是否在安全线以上
	public void checkStorage(int num) {
		Good.alertnum = num;// 因为alertnum是静态的,所以直接赋值
		// 遍历goodMap,如果库存小于警戒库存,增加备注信息,反之,删除备注信息。
		for (Entry<String, Good> e : goodMap.entrySet()) {
			if (e.getValue().getStorage() < Good.alertnum) {
				e.getValue().setNote("*该商品已不足" + Good.alertnum + "件");
			} else {
				e.getValue().setNote("");
			}
		}
	}

	// 查询指定商品
	public void queryGood(String name) {
		if (goodMap.get(name) != null) {
			System.out.println("商品ID" + "\t\t商品名称" + "\t\t商品价格" + "\t\t商品库存"
					+ "\t\t备注");
			System.out.println(goodMap.get(name));
		} else
			System.out.println("商品不存在");

	}

	// 查询全部商品
	public void queryAll() {
		System.out.println("商品ID" + "\t\t商品名称" + "\t\t商品价格" + "\t\t商品库存"
				+ "\t\t备注");
		for (Entry<String, Good> e : goodMap.entrySet()) {
			System.out.println(e.getValue().getId() + "\t"
					+ e.getValue().getName() + "\t\t" + e.getValue().getPrice()
					+ "\t\t" + e.getValue().getStorage() + "\t\t"
					+ e.getValue().getNote());
		}
	}

	// 查询当日买出商品
	public void dayQuery() {
		System.out.println("商品ID" + "\t\t商品名称" + "\t\t商品价格" + "\t\t商品库存"
				+ "\t\t销量" + "\t\t备注");
		for (Entry<String, Good> e : goodMap.entrySet()) {
			if (e.getValue().getSale() != 0) {
				System.out.println(e.getValue().getId() + "\t"
						+ e.getValue().getName() + "\t\t"
						+ e.getValue().getPrice() + "\t\t"
						+ e.getValue().getStorage() + "\t\t"
						+ e.getValue().getSale() + "\t\t"
						+ e.getValue().getNote());
			}
		}
	}

	// 模糊查找商品信息
	public boolean fuzzyQuery(String str) {
		int count = 0;
		Pattern p = Pattern.compile("\\w*" + str + "\\w*");
		System.out.println("商品ID" + "\t\t商品名称" + "\t\t商品价格" + "\t\t商品库存"
				+ "\t\t备注");
		for (Entry<String, Good> e : goodMap.entrySet()) {
			Matcher m = p.matcher(e.getValue().getName());
			if (m.matches()) {
				System.out.println(e.getValue().getId() + "\t"
						+ e.getValue().getName() + "\t\t"
						+ e.getValue().getPrice() + "\t\t"
						+ e.getValue().getStorage() + "\t\t"
						+ e.getValue().getSale() + "\t\t"
						+ e.getValue().getNote());
				count++;
			}
		}
		if (count == 0)
			return false;
		return true;
	}

	// 按价格升序查询
	// 因为HashMap没有排序的功能,所以把集合中的值取出来,放到ArrayList集合里
	// 使用Collections.sort()方法,对ArrayList进行升序排序
	@SuppressWarnings("unchecked")
	public void priceQuery() {
		List<Good> goodlist = new ArrayList<Good>();
		for (Entry<String, Good> e : goodMap.entrySet()) {
			goodlist.add(e.getValue());
			System.out.println(e.getValue().getPrice());
		}
		Collections.sort(goodlist);
		System.out.println("商品ID" + "\t\t商品名称" + "\t\t商品价格" + "\t\t商品库存"
				+ "\t\t备注");
		for (int i = 0; i < goodlist.size(); i++) {
			System.out.println(goodlist.get(i).getId() + "\t"
					+ goodlist.get(i).getName() + "\t\t"
					+ goodlist.get(i).getPrice() + "\t\t"
					+ goodlist.get(i).getStorage() + "\t\t"
					+ goodlist.get(i).getNote());
		}
	}

	// 保存商品信息
	// 使用序列化,把HashMap写到硬盘。
	@SuppressWarnings("unchecked")
	public void save() {
		File file = new File("d:/mydoc/goods.txt");
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			List list = new ArrayList();
			list.add(goodMap);
			list.add(Good.count);
			list.add(Good.alertnum);
			oos.writeObject(list);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (oos != null) {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 结账
	public double settlement(Good good, int sale) {
		return good.settlement(good, sale);
	}
}

之后同理对salesmanGroup也进行增删改查管理

/**
 * 售货员库
 */
package com.pb.salesmanBiz;

import java.io.File;
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.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.pb.salesman.Salesman;

//@SuppressWarnings("serial")
public class SalesmanGroup  {

	//定义HashMap集合,用于存放售货员信息。
	public static Map<String, Salesman> userMap = new HashMap<String, Salesman>();
	
	//直接new的时候读取硬盘信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SalesmanGroup(){
		ObjectInputStream ois = null;
		try {
			File file = new File("d:/mydoc/user.txt");
			if(!file.exists())
				file.createNewFile();
			ois = new ObjectInputStream(new FileInputStream(file));
			List list = (List) ois.readObject();
			userMap = (Map<String, Salesman>) list.get(0);
			Salesman.sum = (Integer) list.get(1);
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	// 初始化HashMap,使用反序列化,从硬盘读取信息。
//	@SuppressWarnings("unchecked")
//	public static void initializeSalesman() {
//		ObjectInputStream ois = null;
//		try {
//			File file = new File("d:/mydoc/user.txt");
//			if (!file.exists())
//				file.createNewFile();
//			ois = new ObjectInputStream(new FileInputStream(file));
//			try {
//				List list = (List) ois.readObject();
//				userMap = (Map<String, Salesman>) list.get(0);
//				Salesman.sum = (Integer) list.get(1);
//			} catch (ClassNotFoundException e) {
//				e.printStackTrace();
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

	// 增加售货员
	public void add(Salesman user) {
		userMap.put(user.getName(), user);
	}

	// 修改售货员
	public void modify(Salesman user) {
		userMap.put(user.getName(), user);
	}

	// 删除售货员
	public void delete(String name) {
		userMap.remove(name);
	}

	// 查询指定售货员
	public void query(String name) {
		if (userMap.get(name) != null) {
			System.out.println("用户名" + "\t密码" + "\t\t年龄");
			System.out.println(userMap.get(name));
		} else
			System.out.println("用户不存在");

	}

	// 查询全部售货员
	public void queryAll() {
		System.out.println("用户名" + "\t密码" + "\t\t年龄");
		for (Entry<String, Salesman> e : userMap.entrySet()) {
			System.out.println(e.getValue().getName() + "\t"
					+ e.getValue().getPsd() + "\t\t" + e.getValue().getAge());
		}
	}

	// 保存售货员信息,使用序列化,把HashMap写到硬盘。
	@SuppressWarnings("unchecked")
	public void save() {
		File file = new File("d:/mydoc/user.txt");
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			List list = new ArrayList();
			list.add(userMap);
			list.add(Salesman.sum);
			oos.writeObject(list);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (oos != null) {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

然后我们再几乎没有写代码都之间点一点完成底层对Good和Salesman的描述,完成类

/*
 * 定义商品信息
 */
package com.pb.good;

import java.io.Serializable;

import com.pb.number.ID;

@SuppressWarnings( { "serial", "unchecked" })
public class Good implements Serializable, Comparable {

	public static int count = 1;// 记录增加商品的总数量
	public static int alertnum = 10;// 警戒库存

	private String id;// 商品ID
	private String name;// 名称
	private double price;// 价格
	private int storage;// 库存
	private int sale;// 销售数量
	private String note;// 备注

	//无参购造方法(每使用new购造一次count加1,代表商品总数加1)
	public Good() {
		this.id = ID.returnID(count);
		count++;
	}

	//有参购造方法(每使用new购造一次count加1,代表商品总数加1,判断库存,如果小于
	//警戒库存,则添加备注信息)
	@SuppressWarnings("static-access")
	public Good(String name, double price, int storage) {
		this.id = ID.returnID(count);
		this.name = name;
		this.price = price;
		this.storage = storage;
		if (this.storage < this.alertnum)
			this.setNote("*该商品已不足" + this.alertnum + "件");
		this.note = "";
		count++;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getStorage() {
		return storage;
	}

	//set库存足,如果实际库存小于警戒库存,则添加备注信息
	@SuppressWarnings("static-access")
	public void setStorage(int storage) {
		this.storage = storage;
		if (this.storage < this.alertnum)
			this.setNote("*该商品已不足" + this.alertnum + "件");
		else
			this.setNote("");
	}

	public int getSale() {
		return sale;
	}

	public void setSale(int sale) {
		this.sale = sale;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}
	
	public String getId() {
		return id;
	}

//	// 返回购买商品的金额
//	public double cost(int sale) {
//		return sale * this.price;
//	}

	// 重写toString()方法
	@Override
	public String toString() {
		return this.getId() + "\t" + this.getName() + "\t\t" + this.getPrice()
				+ "\t\t" + this.getStorage() + "\t\t" + this.getNote();
	}

	// 重写compareTo()方法,用于ArrayList排序
	public int compareTo(Object obj) {
		Good good = (Good) obj;
		if (this.price == good.getPrice()) {
			return 0;
		} else {
			if (this.price > good.getPrice()) {
				return 1;
			} else {
				return -1;
			}
		}
	}
	
	// 返回购买商品的金额,如果库存小于购买数量,提式,如果不小于,返回该商品总价,并出库
	public double settlement(Good good, int sale) {
		this.sale += sale;
		if (good.getStorage() < sale) {
			System.out.println("库存不足!");
			return 0;
		} else {
			good.setStorage(good.getStorage() - sale);
			return good.getPrice() * sale;
		}
	}
}

/**
 * 定义售货员类
 */
package com.pb.salesman;

import java.io.Serializable;

import com.pb.number.ID;

@SuppressWarnings("serial")
public class Salesman implements Serializable {

	public static int sum=1;// 用于计录售货员的人数,所以用static

	@SuppressWarnings("unused")
	private String id;//售货员ID
	private String name;//用户名
	private String psd;//密码
	private int age;//年龄

	public Salesman() {
		this.id=ID.returnID(sum++);
	}

	public Salesman(String name, String psd, int age) {
		this.id=ID.returnID(sum);
		this.name = name;
		this.psd = psd;
		this.age = age;
		sum++;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPsd() {
		return psd;
	}

	public void setPsd(String psd) {
		this.psd = psd;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return this.getName() + "\t" + this.getPsd() + "\t\t" + this.getAge();
	}
}

现在程序的框架基本已经搭好啦~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值