Java基于socket网络编程的超市收银管理系统.docx
package entity;import java.io.Serializable;/* * 收银类 * */SuppressWarnings("serial")public class Cash implements Serializable private String name;/ 商品名private String unit;/ 商品单位private int amount;/ 商品数量private double price;/ 商品单价private double money;/ 商品金额public Cash(String name, String unit, int amount, double price, double money) this.name = name;this.unit = unit;this.amount = amount;this.price = price;this.money = money;public String getName() return name;public void setName(String name) this.name = name;public String getUnit() return unit;public void setUnit(String unit) this.unit = unit;public int getAmount() return amount;public void setAmount(int amount) this.amount = amount;public double getPrice() return price;public void setPrice(double price) this.price = price;public double getMoney() return money;public void setMoney(double money) this.money = money;package entity;import java.io.Serializable;/* * 商品类 * */SuppressWarnings("serial")public class ShangPin implements Serializable private String number;/ 商品编号private String spname;/ 商品名称private double price;/ 商品单价private String unit;/ 单位private int amount;/ 数量public String getNumber() return number;public void setNumber(String number) this.number = number;public String getSpname() return spname;public void setSpname(String spname) this.spname = spname;public double getPrice() return price;public void setPrice(double price) this.price = price;public String getUnit() return unit;public void setUnit(String unit) this.unit = unit;public int getAmount() return amount;public void setAmount(int amount) this.amount = amount;public ShangPin(String number, String spname, double price, String unit,int amount) this.number = number;this.spname = spname;this.price = price;this.unit = unit;this.amount = amount;package entity;/* * 用户类 */import java.io.Serializable;SuppressWarnings("serial")public class User implements Serializable private String username;/ 用户名称private String password;/ 用户密码public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(String password) this.password = password;public User(String username, String password) this.username = username;this.password = password;package entity;import java.io.Serializable;/* * 会员类 * */SuppressWarnings("serial")public class Vip implements Serializable private String vipId;/ 会员卡号private int score;/ 会员积分public Vip(String vipId,int score)this.vipId = vipId;this.score = score;public String getVipId() return vipId;public void setVipId(String vipId) this.vipId = vipId;public int getScore() return score;public void setScore(int score) this.score = score;package socket;/* * 客户端类 * */import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Scanner;import entity.Cash;import entity.ShangPin;public class Client static Scanner input = new Scanner(System.in);private static Object receive;private static String username;static int count = 0;private static Object send(Map<String, String> map) throws Exception,Exception / 发送方法Socket s = new Socket("127.0.0.1", 8800);OutputStream os = s.getOutputStream();ObjectOutputStream oos = new ObjectOutputStream(os);oos.writeObject(map);InputStream is = s.getInputStream();ObjectInputStream ois = new ObjectInputStream(is);receive = ois.readObject();/ 关闭流oos.close();os.close();is.close();ois.close();return receive;public static void main(String args) throws Exception System.out.println("*欢迎使用青鸟超市管理系统*");System.out.println("1.登陆");System.out.println("2.退出系统");System.out.println("*请选择数字1/2*");while (true) int choose = input.nextInt();if (choose = 1) do System.out.print("用户名称:");username = input.next();System.out.print("用户密码:");String password = input.next();Map<String, String> map = new HashMap<String, String>();map.put("账号", username);map.put("密码", password);map.put("send", "登录");send(map);/ 库存管理员界面if ("管理员".equals(receive) System.out.println("*欢迎登录青鸟超市库存管理系统*");admin(); else if ("收银员".equals(receive) System.out.println("*欢迎登录青鸟超市收银系统*");cash(); else System.out.println("用户名或者密码输入错误!登录失败!"); while (true); else if (choose = 2) System.out.println("您已经退出系统!");break; else System.out.println("输入错误!请重新选择数字1/2:");private static void admin() throws Exception / 管理员int choose;while (true) System.out.println("请选择进行的操作:1.商品入库 t 2.商品出库 t 3.新增商品 t 4.查询所有商品 t 5.按编号查询商品 t 6.退出");choose = input.nextInt();/ 选择库存操作switch (choose) case 1:/ 入库in();break;case 2:/ 出库out();break;case 3:/ 新增add();break;case 4:/ 打印所有商品checkall();break;case 5:/ 按编号查询check();break;case 6:/ 退出System.out.println("谢谢使用!");break;default:System.out.println("对不起,您的输入有误,请重新输入!");break;SuppressWarnings("unchecked")private static void in() throws Exception / 入库System.out.print("输入商品编号:");String number = input.next();Map<String, String> map = new HashMap<String, String>();map.put("编号", number);map.put("send", "查询编号");send(map);if ("有此商品".equals(receive) System.out.print("入库数量:");String amount = input.next();if (num(amount) = true) int a = Integer.parseInt(amount);if (a >= 0) map.put("入库数量", amount);map.put("send", "入库");Map<String, ShangPin> map1 = (Map<String, ShangPin>) send(map);System.out.println("商品编号tt" + "商品名称tt" + "商品单价tt"+ "单位tt" + "数量");for (ShangPin iterable_element : map1.values() System.out.println(iterable_element.getNumber()+ "tt" + iterable_element.getSpname()+ "tt" + iterable_element.getPrice() + "tt"+ iterable_element.getUnit() + "tt"+ iterable_element.getAmount(); else System.out.println("出库数量有误!"); else System.out.println("没有此商品,请选择编号3:新增商品!");SuppressWarnings("unchecked")private static void out() throws Exception / 出库System.out.print("输入商品编号:");String number = input.next();Map<String, String> map = new HashMap<String, String>();map.put("编号", number);map.put("send", "查询编号");send(map);if ("有此商品".equals(receive) System.out.print("出库数量:");String amount = input.next();if (num(amount) = true) int a = Integer.parseInt(amount);if (a >= 0) map.put("出库数量", amount);map.put("send", "出库");send(map);Map<String, ShangPin> map1 = (Map<String, ShangPin>) receive;if (a <= map1.get(number).getAmount() System.out.println("商品编号tt" + "商品名称tt"+ "商品单价ttt" + "单位ttt" + "数量");for (ShangPin sp : map1.values() System.out.println(sp.getNumber() + "tt"+ sp.getSpname() + "tt" + sp.getPrice()+ "tt" + sp.getUnit() + "tt"+ sp.getAmount(); else System.out.println("库存不足,请核实,出库失败!");System.out.println("商品编号tt" + "商品名称tt" + "商品单价tt"+ "单位tt" + "数量");System.out.println(map1.get(number).getNumber()+ "tt" + map1.get(number).getSpname()+ "tt" + map1.get(number).getPrice() + "tt"+ map1.get(number).getUnit() + "tt"+ map1.get(number).getAmount(); else System.out.println("出库数量有误!"); else System.out.println("没有此商品!");SuppressWarnings("unchecked")private static void add() throws Exception / 新增while (true) System.out.print("输入商品编号:");String number = input.next();Map<String, String> map = new HashMap<String, String>();map.put("编号", number);map.put("send", "查询编号");send(map);if ("无此商品".equals(receive) System.out.print("输入商品名称:");String spname = input.next();System.out.print("输入商品价格:");String price = input.next();if (price(price) = true) System.out.print("输入商品单位:");String unit = input.next();System.out.print("输入商品数量:");String amount = input.next();if (num(amount) = true) map.put("send", "新增");map.put("名称", spname);map.put("价格", price);map.put("单位", unit);map.put("数量", amount);send(map);System.out.println("新增商品成功!");Map<String, ShangPin> map2 = (Map<String, ShangPin>) receive;System.out.println("商品编号tt" + "商品名称tt"+ "商品单价ttt" + "单位ttt" + "数量");for (ShangPin sp : map2.values() System.out.println(sp.getNumber() + "tt"+ sp.getSpname() + "tt" + sp.getPrice()+ "tt" + sp.getUnit() + "tt"+ sp.getAmount();break; else System.out.println("此编号已存在");continue;SuppressWarnings("unchecked")private static void checkall() throws Exception / 查询全部Map<String, String> map = new HashMap<String, String>();map.put("send", "查询全部");send(map);Map<String, ShangPin> map1 = (Map<String, ShangPin>) receive;System.out.println("商品编号tt" + "商品名称tt" + "商品单价tt" + "单位tt"+ "数量");for (ShangPin sp : map1.values() System.out.println(sp.getNumber() + "tt" + sp.getSpname()+ "tt" + sp.getPrice() + "tt" + sp.getUnit() + "tt"+ sp.getAmount();SuppressWarnings("unchecked")private static void check() throws Exception / 查询号查询System.out.print("输入商品编号:");String number = input.next();Map<String, String> map = new HashMap<String, String>();map.put("编号", number);map.put("send", "查询编号");send(map);if ("有此商品".equals(receive) map.put("send", "查询号查询");send(map);Map<String, ShangPin> map1 = (Map<String, ShangPin>) receive;System.out.println("商品编号tt" + "商品名称tt" + "商品单价ttt"+ "单位ttt" + "数量");for (ShangPin sp : map1.values() System.out.println(sp.getNumber() + "tt" + sp.getSpname()+ "tt" + sp.getPrice() + "tt" + sp.getUnit()+ "tt" + sp.getAmount(); else System.out.println("对不起,没有此商品!");private static void cash() throws Exception / 收银int choose;while (true) System.out.println("请选择进行的操作:1.扫描商品 t 2.修改数量 t 3.结账 t 4.退出");choose = input.nextInt();/ 选择库存操作switch (choose) case 1:/ 扫描scan();break;case 2:/ 修改数量modify();break;case 3:/ 新增checkout();break;case 4:/ 退出System.out.println("谢谢使用!");break;default:System.out.println("对不起,您的输入有误,请重新输入!");break;SuppressWarnings("unchecked")public static void scan() throws Exception / 扫描商品System.out.print("输入需要扫描的商品编号:");String number = input.next();Map<String, String> map = new HashMap<String, String>();map.put("编号", number);map.put("send", "查询编号");send(map);if ("有此商品".equals(receive) System.out.println("扫描成功!");count+;String c = String.valueOf(count);map.put("send", "扫描");map.put("扫描次数", c);send(map);Map<String, Cash> map2 = (Map<String, Cash>) receive;System.out.println("商品名称tttt" + "数量tt" + "单价tt" + "金额");System.out.println("-");for (Cash cash : map2.values() System.out.println(cash.getName() + cash.getUnit() + "tt"+ cash.getAmount() + "tt" + cash.getPrice() + "tt"+ cash.getMoney();System.out.println("-"); else System.out.println("对不起,没有此商品,扫描失败");SuppressWarnings("unchecked")public static void modify() throws Exception / 修改购买数量Map<String, String> map = new HashMap<String, String>();if (count != 0) while (true) System.out.print("输入需要修改的商品编号:");String number = input.next();map.put("编号", number);map.put("send", "查询编号");send(map);if ("有此商品".equals(receive) map.put("编号", number);map.put("send", "检测是否扫描");send(map);if ("已被扫描".equals(receive) System.out.print("输入修改数量:");String amount = input.next();if (num(amount) = true) int a = Integer.parseInt(amount);if (a >= 0) map.put("数量", amount);map.put("send", "修改数量");send(map);Map<String, Cash> map2 = (Map<String, Cash>) receive;System.out.println("商品名称tttt" + "数量tt"+ "单价tt" + "金额");System.out.println("-");for (Cash cash : map2.values() System.out.println(cash.getName()+ cash.getUnit() + "tt"+ cash.getAmount() + "tt"+ cash.getPrice() + "tt"+ cash.getMoney();System.out.println("-"); else System.out.println("修改数量有误!");break;break; else System.out.println("没有扫描此商品。");continue; else System.out.println("对不起,没有此商品,扫描失败!");break; else System.out.println("您还没有扫描商品!");SuppressWarnings("unchecked")public static void checkout