《超市购物管理系统(附图).pdf》由会员分享,可在线阅读,更多相关《超市购物管理系统(附图).pdf(23页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、package com。mm。entity;import java。io.*;/商品类 SuppressWarnings(”serial)public class Goods implements Serializable private int No;/商品编号 private String name;/商品名称 private double price;/商品单价 private int amount;/商品数量 private String unit;/商品单位 public int getNo()return No;public void setNo(int no)No=no;publ
2、ic String getUnit()return unit;public void setUnit(String unit)this.unit=unit;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 getAmount()return amount;public void setA
3、mount(int amount)this。amount=amount;public Goods()public Goods(int No,String name,double price,String unit,int amount)this.No=No;this.name=name;this。price=price;this.amount=amount;this。unit=unit;=package com。mm。entity;import java.util。Scanner;import com.mm.manager。;/菜单类 public class Menu /商品库存管理菜单 p
4、ublic static final void goodsMenu()System。out。println(*欢迎登录青鸟超市管理系统*”);Scanner input=new Scanner(System.in);GoodsManger gm=new GoodsManger();boolean result=true;while(result)System.out。print(”请选择进行的操作:);System.out。print(1.商品入库);System.out。print(2。商品出库”);System.out.print(3.新增商品”);System。out.print(”4.
5、查询全部商品);System。out.print(”5.按序查询号查询);System.out.print(6.退出n”);switch(input.nextInt())case 1:gm。inStorage();break;case 2:gm。outStorage();break;case 3:gm.add();break;case 4:gm.check();break;case 5:gm.checkByNo();break;case 6:result=false;break;default:System。out.println(t 您的选择有误。”);/收银管理菜单 public stat
6、ic final void orderMenu()System。out.println(”*欢迎登录青鸟超市管理系统*);Scanner input=new Scanner(System。in);OrderManager om=new OrderManager();boolean result=true;while(result)System。out。print(请选择进行的操作:”);System。out.print(1.扫描商品”);System.out.print(2.修改数量);System。out.print(3.结账”);System.out.print(”4.退出n”);swit
7、ch(input。nextInt()case 1:om.scan();/执行扫描 break;case 2:om.modify();/修改数量 break;case 3:om.account();/结账 break;case 4:result=false;break;default:System。out。println(”t 您的选择有误。);=package com。mm。entity;/订单类 /public class Order private String name;/所购商品名称:(编号)+名称+单位 private int count;/所购商品数量 private double
8、 price;/单价 public String getName()return name;public void setName(String name)this.name=name;public int getCount()return count;public void setCount(int count)this。count=count;public double getPrice()return price;public void setPrice(double price)this.price=price;public Order()public Order(String nam
9、e,int count,double price)this.name=name;this。count=count;this.price=price;=package com.mm。entity;import java.io。Serializable;/*会员类:主要记录积分和会员号 /SuppressWarnings(”serial)public class Vip implements Serializable private int vipNo;private int score;public int getVipNo()return vipNo;public void setVipNo(
10、int vipNo)this。vipNo=vipNo;public int getScore()return score;public void setScore(int score)this.score=score;public Vip()public Vip(int vipNo,int score)this。vipNo=vipNo;this。score=score;=package com.mm。initialize;/通过这个类给本地初始化一个原始商品信息表(创建仓库)import java.io.FileOutputStream;import java.io。IOException;i
11、mport java。io.ObjectOutputStream;import java.util。ArrayList;import com。mm。entity。Goods;public class GoodsList SuppressWarnings(”unchecked)public static void main(String args)Goods goo0=new Goods(1001,脉动水蜜桃 ”,7.0,1.5L”,50);Goods goo1=new Goods(1002,”旺仔苹果 Q 糖”,2。5,”70g”,70);Goods goo2=new Goods(1003,吉
12、百芝麻油 ,9。5,”125ml”,20);Goods goo3=new Goods(1004,雀巢奶香咖啡”,1.5,13g,200);Goods goo4=new Goods(1005,”白玉内酯豆腐,1。0,”400g,15);Goods goo5=new Goods(1006,桃李熟切片 ”,6.5,”400g,10);Goods goo6=new Goods(1007,白玉黄豆芽 ”,2.4,350g”,20);Goods goo7=new Goods(1008,喜之郎果冻 ,10.5,”125g,50);ArrayList list=new ArrayList();try Obje
13、ctOutputStream oos=new ObjectOutputStream(new FileOutputStream(D:goods。txt”);list。add(goo0);list。add(goo1);list。add(goo2);list.add(goo3);list。add(goo4);list。add(goo5);list。add(goo6);list.add(goo7);oos。writeObject(list);oos。close();catch(IOException e)e.printStackTrace();=package com。mm.initialize;im
14、port java.io.FileNotFoundException;import java.io。FileOutputStream;import java.io。IOException;import java.io。ObjectOutputStream;import java.util。ArrayList;import com。mm。entity。Vip;/*用来创建一个存储会员信息的库 */public class VipList public static void main(String args)throws FileNotFoundException,IOException Vip
15、 v1=new Vip(2001,200);Vip v2=new Vip(2002,800);Vip v3=new Vip(2003,2000);Vip v4=new Vip(2004,2100);Vip v5=new Vip(2005,1200);Vip v6=new Vip(2006,3200);Vip v7=new Vip(2007,290);ArrayListVip list=new ArrayListVip();list.add(v1);list.add(v2);list.add(v3);list。add(v4);list.add(v5);list。add(v6);list。add(
16、v7);ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(”D:vip。txt”));oos.writeObject(list);oos.close();=package com。mm.client;import java。io。BufferedReader;import java.io.BufferedWriter;import java。io.IOException;import java。io.InputStreamReader;import java.io.OutputStreamWriter;impo
17、rt java。net.Socket;import java。net.UnknownHostException;import java.util.Scanner;import com。mm。entity。Menu;public class MrgClient public static String username;public static void main(String args)throws UnknownHostException,IOException Scanner input=new Scanner(System.in);Socket socket=new Socket(”l
18、ocalhost”,10001);BufferedReader br=new BufferedReader(new InputStreamReader(socket。getInputStream();BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));System.out。println(”请输入用户名:);username=input。next();System。out.println(”请输入密码:);String password=input。next();bw.wr
19、ite(username+”+”+password);bw.flush();socket.shutdownOutput();String reply=br.readLine();if(reply.equals(登录成功)System.out。println(reply);/进入库管员界面 Menu。goodsMenu();else if(reply.equals(load successed!”))System。out.println(reply);/进入收银员界面 Menu。orderMenu();else System.out.println(reply);bw。close();br。cl
20、ose();socket.close();=package com.mm。server;import java。io.IOException;import .ServerSocket;import java。net.Socket;public class MgrServer public static void main(String args)throws IOException ServerSocket ss=new ServerSocket(10001);Socket socket=null;/搞个死循环,让服务器一直监听端口 while(true)socket=ss.accept();
21、MgrThread st=new MgrThread(socket);/启动一次程序创建一个线程 st。start();=package com.mm.server;import java.io。;import java。net。Socket;public class MgrThread extends Thread private Socket socket;public MgrThread(Socket socket)this.socket=socket;public void run()try BufferedReader br=new BufferedReader(new InputS
22、treamReader(socket。getInputStream());BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream());String admin=br.readLine();System.out.println(有用户请求登录”);if(admin.equals(admin1+888888”)|admin.equals(”admin2+888888)|admin.equals(”admin3+888888))bw。write(登录成功”);bw.flush();else
23、 if(admin。equals(1001+888888”)|admin。equals(”1002+888888)|admin。equals(”1003+888888)bw。write(”load successed!);bw.flush();else bw。write(”用户名或者密码错误!);bw。flush();bw.close();br。close();socket.close();catch(IOException e)e。printStackTrace();=package com。mm.manager;import java.io。File;import java.io.File
24、InputStream;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.Scanner;import com.mm.entity。Goods;/*商品管理类,包括对商品的入库,出
25、库,新增,删除,按编号查询,全部商品查询等 author Administrator */SuppressWarnings(”serial)public class GoodsManger implements Serializable public static final File f=new File(D:goods.txt”);public static ArrayList getList()ObjectInputStream ois=null;try ois=new ObjectInputStream(new FileInputStream(f));if(ois。available(
26、)!=1)list=(ArrayListGoods)ois。readObject();/从本地读取商品库存信息列表 else ois.close();catch(ClassNotFoundException e)e。printStackTrace();catch(IOException e)e。printStackTrace();finally if(ois!=null)try ois。close();catch(IOException e)e.printStackTrace();return list;/查询全部商品 public void check()list=getList();/打印
27、菜单表头 System.out.println(”商品编号t”+”商品名称ttt+”商品单价tt+”单位tt”+数量);/打印菜单列表-遍历读来的 ArrayList 集合 for(Goods s:list)if(s。getName()。length()5)System.out。println(s.getNo()+t+s.getName()+ttt”+s.getPrice()+”tt”+s.getUnit()+”tt”+s。getAmount());else System。out.println(s.getNo()+”t”+s.getName()+”tt”+s。getPrice()+tt”+s
28、。getUnit()+”tt+s.getAmount());/商品入库(要传入编号)public synchronized void inStorage()list=getList();ObjectOutputStream oos=null;try oos=new ObjectOutputStream(new FileOutputStream(f);catch(FileNotFoundException e1)e1。printStackTrace();catch(IOException e1)e1。printStackTrace();/遍历 ArrayList 若送来的商品在库中存在则改变:原
29、有数量=原有数量+新增数量,否则提示选择新增商品 boolean contain=false;/作为原库中是否包含有此新增商品的标记,默认不包含 /包含就只改变数目 System.out.println(”请输入商品编号:”);Scanner input=new Scanner(System。in);int No=input。nextInt();for(int i=0;i list.size();i+)if(contain=(No=list。get(i).getNo()System。out.print(”入库数量:);int amount=input。nextInt();(list.get(i
30、))。setAmount(list。get(i)。getAmount()+amount);/更新原有数量=原有数量+新增数量 System。out.println(”入库成功!);try oos。writeObject(list);oos。flush();catch(IOException e)e.printStackTrace();break;if(!contain)System。out。println(”请选择编号 3:新增商品”);/商品出库(要传入编号)public synchronized void outStorage()list=getList();ObjectOutputStr
31、eam oos=null;try oos=new ObjectOutputStream(new FileOutputStream(f);catch(FileNotFoundException e1)e1.printStackTrace();catch(IOException e1)e1。printStackTrace();boolean contain=false;/作为原库中是否包含有此新增商品的标记,默认不包含 Scanner input=new Scanner(System.in);do System.out。println(”请输入商品编号:”);int No=input。nextIn
32、t();for(int i=0;i list.size();i+)Goods good=list。get(i);if(contain=(No=good。getNo())System.out.print(”出库数量:”);int amount=input.nextInt();if(good.getAmount()amount)System。out.println(库存不足,请核实,出库失败!”);System。out。println(商品编号tt”+”商品名称tt+”商品单价tt+单位tt”+数量”);System。out.println(good.getNo()+tt+good。getName
33、()+”tt +”tt+good。getPrice()+good.getUnit()+tt”+good。getAmount());break;else good.setAmount(good.getAmount()-amount);/更新原有数量=原有数量+新增数量 System.out.println(”入库成功!”);check();try oos。writeObject(list);oos.flush();catch(IOException e)e.printStackTrace();break;if(!contain)System.out.println(”没有此商品!);while(
34、!contain);/如果不存在继续输入编号 /新增商品 public synchronized void add()Scanner input=new Scanner(System。in);list=getList();ObjectOutputStream oos=null;try oos=new ObjectOutputStream(new FileOutputStream(f);catch(FileNotFoundException e)System.out。println(”找不到文件!”);catch(IOException e)e.printStackTrace();boolean
35、 contain=false;/作为原库中是否包含有此新增商品的标记,默认不包含 Goods good=null;do System。out。print(”输入商品编号:”);int No=input.nextInt();for(int i=0;i list.size();i+)good=list.get(i);if(contain=(No=(good。getNo())/查找原库存中是否有此编号 System。out.println(”此编号已存在!);break;/若原库中无此编号,则此编号有效,可以入库 if(!contain)System.out.print(输入商品名称:”);Stri
36、ng name=input.next();System.out.print(输入商品价格:”);double price=input.nextDouble();System.out。print(输入商品单位:);String unit=input.next();System。out。print(”输入商品数量:);int amount=input.nextInt();list.add(new Goods(No,name,price,unit,amount));/构造一个 Goods 对象加入库存表中 try oos。writeObject(list);oos.flush();catch(IOE
37、xception e)e。printStackTrace();while(contain);/按查询号查询商品 public void checkByNo()list=getList();boolean contain=false;/作为原库中是否包含有此新增的商品,默认不包含 Scanner input=new Scanner(System。in);System.out.print(”输入商品编号:”);int No=input.nextInt();for(int i=0;i list。size();i+)Goods good=list.get(i);if(contain=(No=good。
38、getNo()System.out.println(商品编号tt”+”商品名称tt+商品单价tt+”单位tt+”数量”);System.out.println(good.getNo()+”tt+good。getName()+”t +good.getPrice()+”tt+good.getUnit()+”tt+good.getAmount();break;if(!contain)System.out.println(”对不起,没有此商品!);=package com.mm。manager;import java.io.;import java。text。SimpleDateFormat;impo
39、rt java。util。;import com。mm。entity.;/订单管理 public class OrderManager public static double sum=0;/总金额 public static int number=0;/购物总数量 Scanner input=new Scanner(System.in);ArrayList();/扫描商品 public void scan()System。out.print(输入要扫描的商品编号:);int No=input。nextInt();Goods good=null;boolean contain=false;/作
40、为原库中是否包含有此商品编号,默认不包含 for(int i=0;i list。size();i+)good=list.get(i);if(contain=(No=good.getNo())String orderName=(+good.getNo()+)+good.getName()+good.getUnit();Order order=new Order(orderName,1,good。getPrice());System.out.println(扫描成功!”);orderMap.put(good。getNo(),order);break;if(!contain)System.out。p
41、rintln(对不起,没有此商品!”);/显示已扫描商品 SetInteger keySet=orderMap。keySet();System.out。println(”商品名称ttt 数量t 单价t 金额”);System。out.println(-);sum=0;for(int key:keySet)Order order=orderMap.get(key);System.out。println(order。getName()+t”+order.getCount()+”t”+order.getPrice()+”t”+order。getCount()*order.getPrice();sum
42、+=order.getCount()order。getPrice();/结算 System。out.println(-);/修改数量 public void modify()sum=0;SetInteger keySet=orderMap。keySet();System.out.print(”输入要修改的商品编号:);int No=input。nextInt();boolean contain=false;/作为原库中是否包含有此新增的商品,默认不包含 for(int i=0;i keySet=orderMap。keySet();int s=1;/序号 Date date=new Date()
43、;SimpleDateFormat sdf=new SimpleDateFormat(yyyyMMddHHmmss”);/小票编号格式 System.out.println(”ttt 青鸟超市”);System。out。println(收银员:”+com.mm。client。MrgClient。username+ttt小票号:”+sdf.format(date));System。out。println(序号t 商品名称ttt 数量t 单价t 金额);System。out.println(-);for(int key:keySet)Order order=orderMap.get(key);Sy
44、stem。out。println((s+)+t+order。getName()+t”+order。getCount()+”t”+order。getPrice()+t+order。getCount()order。getPrice());number+=order.getCount();System。out。println(”-”);System。out。println(”总数量:+number+”tt+”应收:+sum);/输入付款金额 public int inputMoney()int mon=0;String regex1=1-9d*;String regex2=-19 d;String
45、regex3=”+0,119d”;do String money=input.next();if(!(money.matches(regex1)money。matches(regex2)System.out。println(”请输入正确格式的金额!”);if(money.matches(regex2)&!(money.matches(regex1)))System。out。println(”请输入正确的金额!);if(money。matches(regex3)mon=Integer.parseInt(money);break;while(true);return mon;/结账 Suppres
46、sWarnings(”unchecked)public void account()System.out.println(总金额:”+sum);System.out.println(”1.普通结账 2.会员结账);boolean result=true;while(result)switch(input。nextInt())case 1:System.out。print(”请输入您要支付的金额:);int cash=inputMoney();if(cash=sum)ticket();System。out。println(”实收:+cash+tt”+”找零:+(cash-sum);Date d=
47、new Date();SimpleDateFormat format=new SimpleDateFormat(yyyyMMdd HH:mm:ss);System。out。println(format。format(d));System。out.println(”此小票是您退换货及核对积分的凭证,请妥善保存!”);System.out.println(”=);result=false;orderMap。clear();break;else System.out。println(金额不足!”);case 2:System.out.print(”请输入您的会员卡号:);int vipNum=inp
48、ut.nextInt();synchronized(list)try ObjectInputStream ois1=new ObjectInputStream(new FileInputStream(D:vip。txt”));ArrayList vilist=(ArrayListVip)ois1.readObject();ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(”D:vip。txt”);Vip v=null;boolean isCorrect=false;for(int i=0;ivilist.siz
49、e();i+)v=vilist。get(i);if(vipNum=v。getVipNo()isCorrect=true;System.out.print(”会员卡号正确,请输入您要支付的金额:);int vcash=inputMoney();if(vcash=sum)ticket();System.out。println(”实收:”+vcash+”tt+”找零:”+(vcash-sum);System.out。println(”本次累计积分成功tt 卡号:”+vipNum);System.out。println(消费前积分 +本次消费积分 =消费后积分);int newScore=v。getS
50、core()+(int)sum;System.out。println(v。getScore()+”+(int)sum+=+newScore);v.setScore(newScore);vilist.add(v);oos.writeObject(vilist);oos.close();Date vdate=new Date();SimpleDateFormat vformat=new SimpleDateFormat(yyyy/MM/dd HH:mm:ss”);System.out。println(vformat。format(vdate));System。out。println(此小票是您退换