1、package com.pb.client;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Scanner;import com.pb.server.Goods;import com.pb.util.Datas;import com.pb.util.SysConstants; /* * 客户端 */public class Client public static Scanner input=new Scanner(System.in); pu
2、blic static ClientBIZ clientBIZ=new ClientBIZ(); public static void main(String args) System.out.println(*欢迎使用青鸟超市管理系统*); System.out.println(1.登录n2.退出); System.out.println(*请选择1/2*); System.out.print(请选择:); int choose=input.nextInt(); while(true) if(choose=1) login(); break; else if(choose=2) System
3、.out.println(成功退出,谢谢使用); else System.out.println(您输入的有误请重新输入); /* * 登录视图 */public static void login()while(true)System.out.print(请输入用户名:);String username=input.next();System.out.print(请输入密码:);String password=input.next();Datas datas=clientBIZ.doLogin(username, password);if(SysConstants.SYS_USER.equa
4、ls(datas.getFlag() loginClitroller(); break;else if(SysConstants.SYS_ADMIN.equals(datas.getFlag()loginCashier();break;elseSystem.out.println(用户或密码错误,请重新登录);/* * 库管员登录界面 */public static void loginClitroller()while(true)System.out.println(*欢迎登录青鸟超市库存管理系统*);System.out.println(请选择要进行的操作:1.商品入库t2.商品出库t3.
5、新增商品t4.查询全部商品t5.按编号查询商品t6.退出);System.out.print(请选择:);int choose=input.nextInt();if(choose=1)System.out.println(请输入商品编号:);int goodsid=input.nextInt();System.out.println(请输入商品数量:);int num=input.nextInt();if(clientBIZ.inGoods(goodsid, num).getFlag().equals(SysConstants.SYS_SUCCESS)clientBIZ.inGoods(goo
6、dsid, num);System.out.println(入库成功);show(clientBIZ.inGoods(goodsid, num);elseSystem.out.println(入库失败。没有此编号的商品请选择3新增商品);else if(choose=2)System.out.println(请输入商品编号:);int goodsid=input.nextInt();System.out.println(请输入商品数量:);int num=input.nextInt();if(clientBIZ.outGoods(goodsid, num).getFlag().equals(S
7、ysConstants.SYS_SUCCESS)clientBIZ.outGoods(goodsid, num);System.out.println(出库成功);show(clientBIZ.findGoodAll();elseSystem.out.println(出库失败); else if(choose=3)System.out.println(请输入商品编号:);int goodsid=input.nextInt();if(clientBIZ.findGood(goodsid).getFlag().equals(SysConstants.SYS_SUCCESS)System.out.p
8、rintln(此商品已存在,请确认!);show(clientBIZ.findGoodAll();elseSystem.out.println(请输入商品的数量:);int num=input.nextInt();System.out.println(请输入商品的价格:);double price=input.nextDouble();System.out.println(请输入商品的单位:);String unilt=input.next();System.out.println(请输入商品的名称:);String goodsname=input.next();Goods goods=new
9、 Goods(goodsid,goodsname,price,unilt,num);clientBIZ.addGoods(goods);show(clientBIZ.findGoodAll();else if(choose=4)show(clientBIZ.findGoodAll();else if(choose=5) System.out.println(请输入商品编号:); int goodsid=input.nextInt(); if(SysConstants.SYS_SUCCESS.equals(clientBIZ.findGood(goodsid).getFlag() System.
10、out.println(查询成功); appointed(clientBIZ.findGood(goodsid); else System.out.println(查询失败); else if(choose=6)System.out.println(退出成功,谢谢使用库存管理系统);break;/* * 收银员登陆界面 */public static void loginCashier()while(true)System.out.println(*欢迎登录青鸟超市收银系统*);System.out.println(请选择进行的操作:1.扫描商品t2.修改商品数量t3.结账t4.退出);Sys
11、tem.out.print(请选择:);int choose=input.nextInt();if(choose=1)System.out.println(请输入商品编号:);int goodsid=input.nextInt();if(SysConstants.SYS_SUCCESS.equals(clientBIZ.doScan(goodsid).getFlag()System.out.println(扫描成功);scanner(clientBIZ.doScan(goodsid);elseSystem.out.println(扫描失败);else if(choose=2)if(client
12、BIZ.cheak().getFlag().equals(SysConstants.SYS_ERROR)System.out.println(您还没有有扫面商品);continue;elseSystem.out.println(请输入商品编号:);int goodsid=input.nextInt();if(arygood(goodsid)System.out.println(请输入商品的数量:);Datas datas=null;if(input.hasNextInt()int a=input.nextInt();if(a1)datas=clientBIZ.amendGoods(a, goo
13、dsid);if(datas.getFlag().equals(SysConstants.SYS_LACK)System.out.println(商品数量不足);perlook(clientBIZ.findGood(goodsid);continue;elseSystem.out.println(修改成功);show(clientBIZ.findGoodAll();break;elseSystem.out.println(数量必须大于零);continue;elseinput.nextInt();System.out.println(请输入正确的格式);continue; System.out
14、.println(对不起没有扫描此商品); else if(choose=3)cheakGoods();else if(choose=4)System.out.println(成功退出,谢谢使用);break;elseSystem.out.println(您输入的有误!); private static boolean arygood(int id) if(clientBIZ.cheak().getFlag().equals(SysConstants.SYS_SUCCESS) ArrayList list=clientBIZ.cheak().getBygoodsList(); for(int
15、i=0;ilist.size();i+) if(list.get(i).getGoodsid()=id) return true; return false; /结账public static void cheakGoods()double price=allcheak(clientBIZ.cheak();if(price!=0)System.out.println(总金额为:+price);System.out.println(请选择结账类型:1.会员结账t2.普通结账);System.out.println(请选择:);int choose=input.nextInt();if(choos
16、e=1)System.out.println(请输入会员卡号:);int VIPid=input.nextInt();if(VIPid=2012)System.out.println(会员卡正确,请输入你要支付的金额);int money=input.nextInt();if(moneyprice)System.out.println(金额不足,请确认);else if(money=price)cheakOut(money, price); elseSystem.out.println(您的操作有误!);elseSystem.out.println(会员卡号不正确!);cheakGoods()
17、;else if(choose=2) System.out.println(请输入金额:); int money=input.nextInt(); if(moneyprice) System.out.println(金额不足,请确认); else if(money=price) cheakOut(money, price); else System.out.println(您操作有误!); elseSystem.out.println(您输入的有误);elseSystem.out.println(您还没有扫描商品);public static void perlook(Datas datas)
18、System.out.println(-);System.out.println(商品编号tt商品名称tt商品价格tt商品单位tt商品数量);System.out.println(datas.getGoods().getGoodsid()+tt+datas.getGoods().getGoodsname()+tt+datas.getGoods().getPrice()+ tt+datas.getGoods().getUnilt()+tt+datas.getGoods().getNum();System.out.println(-); /付钱public static void cheakOut
19、(int money,double price)System.out.println(*青鸟超市*); System.out.println(收银员编号:1001tt小票号:+Remond(); scanner(clientBIZ.cheak(); System.out.println(总数量:+clientBIZ.cheak().getBygoodsList().size()+tt应收:+price); System.out.println(实收:+money); System.out.println(应找:+(money-price); System.out.println(此小票是你退货
20、及查看积分的唯一凭证,请妥善保管!); System.out.println(=);/随机生成的小票号public static String Remond()Date date=new Date();SimpleDateFormat sdf=new SimpleDateFormat(yyMMddhhmm);String dar=sdf.format(date)+(int)Math.random()*1000;return dar;/打印时间public static String date()Date dates=new Date();SimpleDateFormat sdf=new Sim
21、pleDateFormat(yy-MM-dd hh:mm:ss);String date=sdf.format(dates);return date;/总价格public static double allcheak(Datas datas)if(datas.getFlag().equals(SysConstants.SYS_SUCCESS)ArrayList list=datas.getBygoodsList();double sum=0;for(int i=0;ilist.size();i+)sum+=list.get(i).getNum()*list.get(i).getPrice();
22、return sum;elsereturn 0;/显示扫描信息public static void scanner(Datas datas)System.out.println(-);System.out.println(序tt商品名称tt商品价格tt商品单位tt商品数量tt金额);ArrayList goodlist=datas.getBygoodsList();for( int i=0;igoodlist.size();i+)System.out.println(i+tt+(+goodlist.get(i).getGoodsid()+)+goodlist.get(i).getGoodsna
23、me()+tt+ tt+goodlist.get(i).getPrice()+tt+goodlist.get(i).getUnilt()+tt+goodlist.get(i).getNum() +tt+goodlist.get(i).getNum()*goodlist.get(i).getPrice();System.out.println(-);/显示指定的商品信息public static void appointed(Datas datas)if(datas.getFlag().equals(SysConstants.SYS_SUCCESS)System.out.println(-);S
24、ystem.out.println(商品编号tt商品名称tt商品价格tt商品单位tt商品数量);Goods goods=datas.getGoods();System.out.println(goods.getGoodsid()+tt+goods.getGoodsname()+tt +goods.getPrice()+tt+goods.getUnilt()+tt+goods.getNum();System.out.println(-);/显示商品的全部信息信息public static void show(Datas datas)System.out.println(-); System.ou
25、t.println(商品编号tt商品名称tt商品价格tt商品单位tt商品数量); ArrayList list=datas.getGoodsList(); for(int i=0;ilist.size();i+) System.out.println(list.get(i).getGoodsid()+tt+list.get(i).getGoodsname()+tt +list.get(i).getPrice()+tt+list.get(i).getUnilt()+tt+list.get(i).getNum(); System.out.println(-);package com.pb.clie
26、nt;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import .Socket;import .UnknownHostException;import java.util.ArrayList;import com.pb.server.Controller;import com.pb.server.Goods;import com.pb.util.Datas;import com.pb.util.SysConstants;import com.pb.ut
27、il.Tools; /* * 客户端类 */public class ClientBIZ Socket socket=null; ObjectOutputStream oos=null; ObjectInputStream ois=null; /创建socket相关的对象 private void initSocket() try socket=new Socket(Tools.getValue(SysConstants.SYS_HOSTNAME), Integer.parseInt(Tools.getValue(SysConstants.SYS_LISTENERPORT);oos=new O
28、bjectOutputStream(socket.getOutputStream();ois=new ObjectInputStream(socket.getInputStream(); catch (NumberFormatException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-g
29、enerated catch blocke.printStackTrace(); private void closeSocket() try oos.close();ois.close(); socket.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); /* * 登录业务 */ public Datas doLogin(String username,String password) try /调用Socket访问服务器 initSocket(); /创建Datas对象
30、Datas datas=new Datas(); /为datas附上值 为了传递登录标示 datas.setFlag(SysConstants.SYS_LOGIN); /创建用户对象 Controller controller=new Controller(); controller.setUsername(username); controller.setPassword(password); datas.setController(controller); /向服务器发送信息 oos.writeObject(datas); /接收服务器的响应 datas=(Datas)ois.readOb
31、ject(); return datas; catch(Exception e) e.printStackTrace(); finally /关闭流 closeSocket(); return null; /商品入库 public Datas inGoods(int goodsid,int num) try /调用socket登录服务器 initSocket(); /创建datas对象 Datas datas=new Datas(); /给标示赋值 datas.setFlag(SysConstants.SYS_INGOOD); /创建商品对象 Goods goods=new Goods();
32、goods.setGoodsid(goodsid); goods.setNum(num); datas.setGoods(goods); /向服务器发送信息 oos.writeObject(datas); /获取服务器的信息 datas=(Datas)ois.readObject(); return datas; catch(Exception e) e.printStackTrace(); finally closeSocket(); return null; /商品出库 public Datas outGoods(int goodsid,int num) try /调用socket登录服务
33、器的方法 initSocket(); /创建datas对象 Datas datas=new Datas(); /创建Goods对象 Goods goods=new Goods(); /赋值 goods.setGoodsid(goodsid); goods.setNum(num); datas.setFlag(SysConstants.SYS_OUTGOOD); datas.setGoods(goods); /向服务器发送信息 oos.writeObject(datas); /接收服务器的响应 datas=(Datas)ois.readObject(); return datas; catch(
34、Exception e) e.printStackTrace(); finally closeSocket(); return null; /新增商品,增加的商品的属性 public Datas addGoods(Goods goods) try /调用Socket登录的方法 initSocket(); Datas datas=new Datas(); datas.setFlag(SysConstants.SYS_ADDGOOD); datas.setGoods(goods); /向服务器发送信息 oos.writeObject(datas); /接受响应 datas=(Datas) ois.readObject(); return datas; catch(Exception