实现配送中心管理系统的基本功能:针对产品实现该配送中心的货物的初始化处理,完成对货物的进仓、出仓、报废等管理,实现货物的库存统计等操作。

基本要求:

  1. 货物的初始化处理
  2. 货物的进仓
  3. 货物的出仓
  4. 货物的报废
  5. 零状态和满仓预警处理

6、必须要用面向对象设计思想编程实现

高级要求:

1、界面友好;

2、对货物信息进行统计(例如某单位时间内流量排序等);

大二期末的实训,共有十五个课设可以选择,博主选择的是仓库管理系统。

这是整个系统的设计模块图(仓库有很多种,博主做的是快递超市仓库管理系统)

不多说,上代码

货物类Cargo:

public class Cargo{String name;int id;String type;String source;int picking;String state;int weight;String time;public Cargo(String name,int id,String type,String source,int picking,int weight) {this.name=name;this.id =id;this.type =type;this.source =source;this.picking=picking;this.state="正常";this.weight=weight;this.time= null;}public String getSource() {return source;}public String getName() {return name;}public int getId() {return id;}public String getType() {return type;}public int getPicking(){return picking;}public String getState(){return state;}public int getWeight(){return weight;}public String getTime() {return time;}public void setName(String name) {this.name = name;}public void setType(String type) {this.type = type;}public void setSource(String source) {this.source = source;}public void setPicking(int picking) {this.picking = picking;}public void setState(String state) { this.state = state; }public void setWeight(int weight) {this.weight = weight;}public void setTime(String time) {this.time = time;}@Overridepublic String toString() {return "Cargo{" +"name='" + name + '\'' +",, type='" + type + '\'' +", source='" + source + '\'' +", picking='" + picking + '\'' +", state='" + state + '\'' +", weight=" + weight +", time=" + time +'}';}}

仓库类Warehouse:

import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.HashMap;public class Warehouse{private HashMap cargos = new HashMap();private int capacity = 0;public int getInventory() {int inventory = 0;for(Cargo cargo : cargos.values()){inventory +=cargo.getWeight();}return inventory;}public String getNewTime(){SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();String str =sdf.format(date);return str;}public int getCapacity() {return capacity;}public void setCapacity(int capacity) {this.capacity = capacity;}publicboolean addCargo(Cargo cargo) {if(getInventory()+cargo.getWeight()<=capacity) {cargos.put(cargo.getId(), cargo);cargo.setTime(getNewTime());return true;} else {return false;}}public boolean getCargo(int id){return cargos.get(id) != null;}public boolean removeCargo(int id){return cargos.remove(id) != null;}public int getCargoPicking(int id){Cargo cargo = cargos.get(id);return cargo.getPicking();}public Cargo seekCargo(int id){return cargos.get(id);}public boolean stateChange(int id){Cargo cargo = cargos.get(id);if(cargo != null) {cargo.setState("已报废");return true;}else {return false;}}public WarehouseWarning warning(int capacity){if(getInventory()  0.9 * capacity) {return WarehouseWarning.FULL;}else{return WarehouseWarning.NONE;}}public Collection getCargos() {return cargos.values();}@Overridepublic String toString() { String str = ""; for (Cargo cargo : cargos.values()) { str += cargo.getName() + "\n"; } return str;}}

仓库类中用到的枚举WarehouseWarning

public enum WarehouseWarning {EMPTY,FULL,NONE}

以上为该系统的数据层

由于高级要求中有一条界面友好的要求,博主是用GUI来实现的表现层(另一个高级要求没有完成)

下面是GUI的主界面MainFrame:

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MainFrame extends JFrame {private JPanel cn;private Warehouse warehouse;public MainFrame(String str, Warehouse warehouse) { super(str); this.warehouse = warehouse; setSize(800, 600); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBackground(Color.white); Box box = Box.createVerticalBox(); setContentPane(box); JPanel an = new JPanel(); an.setSize(800, 200); JButton big = new JButton("容量设置"); big.setPreferredSize(new Dimension(120, 60)); JButton in = new JButton("进仓"); in.setPreferredSize(new Dimension(120, 60)); JButton out = new JButton("出仓"); out.setPreferredSize(new Dimension(120, 60)); JButton useless = new JButton("报废"); useless.setPreferredSize(new Dimension(120, 60)); JButton warning = new JButton("预警"); warning.setPreferredSize(new Dimension(150, 75)); warning.setBackground(Color.cyan); //warning.setEnabled(false); an.add(big); an.add(in); an.add(out); an.add(useless); an.add(warning); box.add(an); setVisible(true); JLabel x = new JLabel(); an.add(x); cn = new JPanel(); cn.setVisible(true); cn.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20)); JScrollPane scrollPane = new JScrollPane(cn); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); //cn.setPreferredSize(new Dimension(780, cn.getPreferredSize().height)); Dimension preferredSize = new Dimension(800, 600); cn.setPreferredSize(preferredSize); scrollPane.setPreferredSize(preferredSize); box.add(scrollPane); changeCargoButton(); big.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { new cabacityAdd("输入容量", warehouse);} }); in.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { new CargoAdd("进仓", warehouse, MainFrame.this);} }); out.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { new CargoRemove("出仓", warehouse, MainFrame.this);} }); useless.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { new Useless("报废", warehouse, MainFrame.this);} }); warning.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) { x.setText(warehouse.getInventory() + " / " + warehouse.getCapacity()); if (warehouse.warning(warehouse.getCapacity()) == WarehouseWarning.EMPTY) {warning.setBackground(Color.green);warning.setText("预警" + "\n" + "EMPLY(零状态)"); } else if (warehouse.warning(warehouse.getCapacity()) == WarehouseWarning.FULL) {warning.setBackground(Color.red);warning.setText("预警" + "\n" + "FULL(满状态)"); } else {warning.setBackground(Color.cyan);warning.setText("预警" + "\n" + "NONE(正常)"); }} });}void changeCargoButton() { cn.removeAll(); validate(); repaint(); for (Cargo cargo : warehouse.getCargos()) {JButton y1 = new JButton();y1.setPreferredSize(new Dimension(170, 80));y1.setBackground(Color.pink);y1.setText(" " + cargo.getId()+ "\n" + " " + cargo.getName() + "\n" + " " + cargo.getState());if (cargo.getState().equals("已报废")) { y1.setText(" " + cargo.getId() + "\n" + " " + cargo.getName() + "\n" + " " + cargo.getState()); y1.setBackground(Color.GRAY);}cn.add(y1);y1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {new CargoMessege(cargo); }});validate(); }} }

容量添加界面cabacityAdd:

import javax.swing.*;import java.awt.*;public class cabacityAdd extends JFrame {public cabacityAdd (String str,Warehouse warehouse) {super(str);setSize(210, 400);setLocationRelativeTo(null);setBackground(Color.white);JPanel c1 = new JPanel();JLabel a1 = new JLabel("容量大小");JTextField b1 = new JTextField(16);JButton jButton = new JButton("确定");c1.add(a1);c1.add(b1);c1.add(jButton);add(c1);setVisible(true);jButton.addActionListener((actionEvent -> {int s1 = Integer.parseInt(b1.getText());warehouse.setCapacity(s1);if(s1==0){new determine("容量不可为0");}else{new determine("容量设置成功");dispose();}}));}}

货物进仓界面CargoAdd:

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class CargoAdd extends JFrame {private MainFrame mainFrame;public CargoAdd(String str, Warehouse warehouse,MainFrame mainFrame) {super(str);this.mainFrame = mainFrame;setSize(210, 400);setLocationRelativeTo(null);setBackground(Color.white);JPanel c1 = new JPanel();JLabel a1 = new JLabel("名称");JLabel a2 = new JLabel("编号");JLabel a3 = new JLabel("类型");JLabel a4 = new JLabel("来源");JLabel a5 = new JLabel("出库码");JLabel a6 = new JLabel("重量");JTextField b1 = new JTextField(16);JTextField b2 = new JTextField(16);JTextField b3 = new JTextField(16);JTextField b4 = new JTextField(16);JTextField b5 = new JTextField(16);JTextField b6 = new JTextField(16);JButton jButton = new JButton("确定");c1.add(a1);c1.add(b1);c1.add(a2);c1.add(b2);c1.add(a3);c1.add(b3);c1.add(a4);c1.add(b4);c1.add(a5);c1.add(b5);c1.add(a6);c1.add(b6);c1.add(jButton);add(c1);setVisible(true);jButton.setActionCommand("myButton");jButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String s1 = b1.getText();int s2 = Integer.parseInt(b2.getText());String s3 = b3.getText();String s4 = b4.getText();int s5 = Integer.parseInt(b5.getText());int s6 = Integer.parseInt(b6.getText());Cargo cargo = new Cargo(s1,s2,s3,s4,s5,s6); // warehouse.addCargo(cargo);if(s1==null||s3==null||s4==null||s6==0){new determine("请输入正确的信息");}else if(warehouse.addCargo(cargo)==false) {new determine("入库失败(超出库存)");}else{//mainFrame.addCargoButton(cargo);mainFrame.changeCargoButton();new determine("入库成功");dispose();}}});}}

货物出库界面CargoRemove:

import javax.swing.*;import java.awt.*;public class CargoRemove extends JFrame {private MainFrame mainFrame;public CargoRemove (String str,Warehouse warehouse,MainFrame mainFrame) {super(str);setSize(210, 400);setLocationRelativeTo(null);setBackground(Color.white);this.mainFrame = mainFrame;JPanel c1 = new JPanel();JLabel a1 = new JLabel("出仓货物编号");JLabel a2 = new JLabel("出库码");JTextField b1 = new JTextField(16);JTextField b2 = new JTextField(16);JButton jButton2 = new JButton("确定");c1.add(a1);c1.add(b1);c1.add(a2);c1.add(b2);c1.add(jButton2);add(c1);setVisible(true);jButton2.addActionListener((actionEvent -> {int s1 = Integer.parseInt(b1.getText());int s2 = Integer.valueOf(Integer.parseInt(b2.getText()));if(warehouse.getCargo(s1)==false){new determine("编号不存在");}else{if(warehouse.getCargoPicking(s1)!=s2){new determine("出仓码错误");}else {warehouse.removeCargo(s1);new determine("出仓成功");mainFrame.changeCargoButton();dispose();}}}));}}

货物报废界面useless:

import javax.swing.*;import java.awt.*;public class Useless extends JFrame {private MainFrame mainFrame;public Useless (String str,Warehouse warehouse,MainFrame mainFrame) {super(str);setSize(210, 400);setLocationRelativeTo(null);setBackground(Color.white);this.mainFrame = mainFrame;JPanel c1 = new JPanel();JLabel a1 = new JLabel("货物报废");JTextField b1 = new JTextField(16);JButton jButton3 = new JButton("确定");c1.add(a1);c1.add(b1);c1.add(jButton3);add(c1);setVisible(true);jButton3.addActionListener((actionEvent -> {int s3 = Integer.parseInt(b1.getText());warehouse.stateChange(s3);if(warehouse.stateChange(s3)==false){new determine("编号不存在");}else{new determine("报废成功");mainFrame.changeCargoButton();}}));}}

货物详细信息弹窗界面CargoMessege:

import javax.swing.*;import java.awt.*;public class CargoMessege extends JDialog {public CargoMessege(Cargo cargo) {setVisible(true);setBounds(800, 300, 200, 300);Container container = getContentPane();container.setLayout(null);Panel jPanel = new Panel();JLabel jLabel1 = new JLabel("货物名字" +": " + cargo.getName());JLabel jLabel2 = new JLabel("编号" +": " + cargo.getId() + "");JLabel jLabel3 = new JLabel("类型" + ": " + cargo.getType() + "");JLabel jLabel4 = new JLabel("来源" + ": " + cargo.getSource() + " ");JLabel jLabel5 = new JLabel("取件码" + ": " + cargo.getPicking() + " ");JLabel jLabel6 = new JLabel("状态" + ": " + cargo.getState() + "");JLabel jLabel7 = new JLabel("重量" + ": " + cargo.getWeight() + " ");JLabel jLabel8 = new JLabel("入库时间" +cargo.getTime());jPanel.setSize(140, 300);jPanel.add(jLabel1);jPanel.add(jLabel2);jPanel.add(jLabel3);jPanel.add(jLabel4);jPanel.add(jLabel5);jPanel.add(jLabel6);jPanel.add(jLabel7);jPanel.add(jLabel8);container.add(jPanel);}}import javax.swing.*;import java.awt.*;public class CargoMessege extends JDialog {public CargoMessege(Cargo cargo) {setVisible(true);setBounds(800, 300, 200, 300);Container container = getContentPane();container.setLayout(null);Panel jPanel = new Panel();JLabel jLabel1 = new JLabel("货物名字" +": " + cargo.getName());JLabel jLabel2 = new JLabel("编号" +": " + cargo.getId() + "");JLabel jLabel3 = new JLabel("类型" + ": " + cargo.getType() + "");JLabel jLabel4 = new JLabel("来源" + ": " + cargo.getSource() + " ");JLabel jLabel5 = new JLabel("取件码" + ": " + cargo.getPicking() + " ");JLabel jLabel6 = new JLabel("状态" + ": " + cargo.getState() + "");JLabel jLabel7 = new JLabel("重量" + ": " + cargo.getWeight() + " ");JLabel jLabel8 = new JLabel("入库时间" +cargo.getTime());jPanel.setSize(140, 300);jPanel.add(jLabel1);jPanel.add(jLabel2);jPanel.add(jLabel3);jPanel.add(jLabel4);jPanel.add(jLabel5);jPanel.add(jLabel6);jPanel.add(jLabel7);jPanel.add(jLabel8);container.add(jPanel);}}

提示信息弹窗界面determine:

import javax.swing.*;import java.awt.*;public class determine extends JDialog {public determine(String str) {setVisible(true);setBounds(600, 400, 200, 100);Container container = getContentPane();container.setLayout(null);JLabel jLabel = new JLabel(str);jLabel.setSize(130, 30);container.add(jLabel);}}

最后是主函数Delivery(添加了一些测试用例)

public class Delivery {public static void main(String[] args) {Warehouse warehouse = new Warehouse();warehouse.setCapacity(1000);//Cargo cargo1 = new Cargo("康师傅",1,"食品","京东",114514,1);//Cargo cargo2 = new Cargo("洗发水",2,"生活用品","淘宝",114514,1);//Cargo cargo3 = new Cargo("洗手液",3,"生活用品","淘宝",114514,960);//Cargo cargo4 = new Cargo("餐巾纸",4,"生活用品","淘宝",114514,2);//Cargo cargo5 = new Cargo("面包",5,"食品","京东",114514,1);//Cargo cargo6 = new Cargo("面包",6,"食品","京东",114514,1);//Cargo cargo7 = new Cargo("康师傅",7,"食品","京东",114514,1);//Cargo cargo8 = new Cargo("洗发水",8,"生活用品","淘宝",114514,1);//Cargo cargo9 = new Cargo("洗手液",9,"生活用品","淘宝",114514,960);//Cargo cargo10 = new Cargo("餐巾纸",10,"生活用品","淘宝",114514,2);//Cargo cargo11 = new Cargo("面包",11,"食品","京东",114514,1);//Cargo cargo12 = new Cargo("面包",12,"食品","京东",114514,1);//Cargo cargo13 = new Cargo("面包",13,"食品","京东",114514,1);//Cargo cargo14 = new Cargo("面包",14,"食品","京东",114514,1);//Cargo cargo15 = new Cargo("康师傅",15,"食品","京东",114514,1);//Cargo cargo16 = new Cargo("洗发水",16,"生活用品","淘宝",114514,1);//Cargo cargo17 = new Cargo("洗手液",17,"生活用品","淘宝",114514,960);//Cargo cargo18 = new Cargo("餐巾纸",18,"生活用品","淘宝",114514,2);//Cargo cargo19 = new Cargo("面包",19,"食品","京东",114514,1);//Cargo cargo20 = new Cargo("面包",20,"食品","京东",114514,1);//Cargo cargo21= new Cargo("康师傅",21,"食品","京东",114514,1);//Cargo cargo22 = new Cargo("洗发水",22,"生活用品","淘宝",114514,1);//Cargo cargo23 = new Cargo("洗手液",23,"生活用品","淘宝",114514,960);////warehouse.addCargo(cargo1);//warehouse.addCargo(cargo2);//warehouse.addCargo(cargo3);//warehouse.addCargo(cargo4);//warehouse.addCargo(cargo5);//warehouse.addCargo(cargo6);//warehouse.addCargo(cargo7);//warehouse.addCargo(cargo8);//warehouse.addCargo(cargo9);//warehouse.addCargo(cargo10);//warehouse.addCargo(cargo11);//warehouse.addCargo(cargo12);//warehouse.addCargo(cargo13);//warehouse.addCargo(cargo14);//warehouse.addCargo(cargo15);//warehouse.addCargo(cargo16);//warehouse.addCargo(cargo17);//warehouse.addCargo(cargo18);//warehouse.addCargo(cargo19);//warehouse.addCargo(cargo20);//warehouse.addCargo(cargo21);//warehouse.addCargo(cargo22);//warehouse.addCargo(cargo23);MainFrame mainFrame = new MainFrame("仓库管理系统",warehouse);}}

以上就是全部代码,接下来看看效果

主界面:

添加超出容量的货物后可使用滚动条

仓库容量设置界面:

进仓出仓界面:

既然是快递超市仓库管理系统,当然需要出仓码才能出仓啦(doge)

点击预警按钮后三种预警界面(正常,零状态,满状态):

报废界面:

点击货物对应按钮后弹出的详细消息弹窗:

信息提示界面:

最后再来谈谈该系统没有完善的地方

首先,系统的预警系统需要手动点击触发,其实这里可以关联在进仓出仓按钮上,实现自动预警,这里纯粹是博主在设计的时候没有想到。

另外,在进仓26个货物之后,后面进仓的(第27个以及27以后)货物可能会无法在主界面显示,这个应该是代码问题但博主一直没有解决,如有大佬指正欢迎在讨论区留言。