首页 > 程序开发 > 软件开发 > Java >

Java单机版的书店管理系统开发教程

2017-11-02

Java单机版的书店管理系统开发教程。

Java单机版的书店管理系统开发教程

展示图片:

\

部分的代码模块:

工具类:StringComparison类:

package cn.hncu.bookStore.util;

/**
 * 工具类
 * 字符串比较
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class StringComparison {

    /**
     * @param str1---需要比较的字符串
     * @param str2---被比较的字符串
     * @return--- 返回0 , 2个字符串相同,
 
     *            返回-1,str1和str2不相同,

     *            返回1,str1是str2的子串(忽略str1两端空格)

     *            返回2,说明str1为null或者str1全部为空格

     */
    public static int stringComp(String str1,String str2){
        if(str1==null||!(str1.trim().length()>0)){
            return 2;
        }

        //字符串完全相同
        if(str1!=null&&str1.trim().length()>0){
            if(str1.equals(str2)){
                return 0;
            }
        }

        //模糊匹配(忽略两端空格)
        if(str1!=null&&str1.trim().length()>0){
            if(str2.indexOf(str1.trim())!=-1){
                return 1;
            }
        }
        return -1;
    }
}

注意一个问题:

要使用对象流读取文件:

这个类必须实现Serializable接口!!!!!

值对象模块:

进货值对象:InMainModel:

package cn.hncu.bookStore.in.vo;

import java.io.Serializable;

/**
 * 进货管理值对象封装
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InMainModel implements Serializable, Comparable{
    //进货单编号
    private String uuid;
    //进货时间
    private long inDate;
    //进货人员编号
    private String inUserId;

    /* 
       如果某个字段是外键,同时又需要在当前表中相应表现层显示出用户能看得懂
     的信息,则需要补一个专用于信息显示(给用户看)的字段。
     */
    private String inUserName;

    public String getInUserName() {
        return inUserName;
    }

    public void setInUserName(String inUserName) {
        this.inUserName = inUserName;
    }

    /**
     * 
     * @return ---返回进货单编号(String型)
     */
    public String getUuid() {
        return uuid;
    }

    /**
     * 
     * @param uuid---设置进货单编号(String型)
     */
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    /**
     * 
     * @return---返回进货时间(long型)
     */
    public long getInDate() {
        return inDate;
    }

    /**
     * 
     * @param inDate---设置进货时间(long型)
     */
    public void setInDate(long inDate) {
        this.inDate = inDate;
    }

    /**
     * 
     * @return---返回进货人员编号(String型)
     */
    public String getInUserId() {
        return inUserId;
    }

    /**
     * 
     * @param inUserId---设置进货人员编号(String型)
     */
    public void setInUserId(String inUserId) {
        this.inUserId = inUserId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        InMainModel other = (InMainModel) obj;
        if (uuid == null) {
            if (other.uuid != null)
                return false;
        } else if (!uuid.equals(other.uuid))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return uuid + ", " + inDate
                + ", " + inUserId;
    }

    @Override
    public int compareTo(InMainModel o) {
        return Integer.parseInt(uuid)-Integer.parseInt(o.uuid);
    }



}

进货明细值对象类:InDetailModel

package cn.hncu.bookStore.in.vo;

import java.io.Serializable;

/**
 * 进货明细管理的值对象封装
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InDetailModel implements Serializable{
    //进货明细编号
    private String uuid;
    //进货单编号
    private String inId;
    //图书编号
    private String bookId;
    //进货数量
    private int sumNum;
    //进货总金额
    private double sumMoney;

    //为把书名显示给用户看,给bookUuid添加一个bookName属性
    private String bookName;



    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getUuid() {
        return uuid;
    }
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    public String getInId() {
        return inId;
    }
    public void setInId(String inId) {
        this.inId = inId;
    }
    public String getBookId() {
        return bookId;
    }
    public void setBookId(String bookId) {
        this.bookId = bookId;
    }
    public int getSumNum() {
        return sumNum;
    }
    public void setSumNum(int sumNum) {
        this.sumNum = sumNum;
    }
    public double getSumMoney() {
        return sumMoney;
    }
    public void setSumMoney(double sumMoney) {
        this.sumMoney = sumMoney;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        InDetailModel other = (InDetailModel) obj;
        if (uuid == null) {
            if (other.uuid != null)
                return false;
        } else if (!uuid.equals(other.uuid))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return uuid +", "+bookName + ", " + sumNum
                + "本, " + sumMoney + "元";
    }





}

进货查询值对象类:InMainQueryModel

package cn.hncu.bookStore.in.vo;

/**
 * 进货查询值对象封装
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InMainQueryModel extends InMainModel {

    //需要查询的最大时间
    private long inDate2;

    /**
     * 
     * @return---需要查询的最大时间值
     */
    public long getInDate2() {
        return inDate2;
    }

    /**
     * 
     * @param inDate2---设置需要查询的最大时间
     */
    public void setInDate2(long inDate2) {
        this.inDate2 = inDate2;
    }

}

进货明细查询值对象类:InDetailQueryModel

package cn.hncu.bookStore.in.vo;

/**
 * 进货明细查询值对象封装
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InDetailQueryModel extends InDetailModel {

    //需要查询的最大进货数量
    private int sumNum2;
    //需要查询的最大进货金额
    private double sumMoney2;

    /**
     * 
     * @return---返回需要查询的最大进货数量
     */
    public int getSumNum2() {
        return sumNum2;
    }
    /**
     * 
     * @param sumNum2---设置需要查询的最大进货数量
     */
    public void setSumNum2(int sumNum2) {
        this.sumNum2 = sumNum2;
    }

    /**
     * 
     * @return---返回需要查询的最大进货金额
     */
    public double getSumMoney2() {
        return sumMoney2;
    }

    /**
     * 
     * @param sumMoney2---设置需要查询的最大进货金额
     */
    public void setSumMoney2(double sumMoney2) {
        this.sumMoney2 = sumMoney2;
    }

}

进货模块的数据层:

进货管理数据层接口:InMainDao

package cn.hncu.bookStore.in.dao.dao;

import java.util.List;

import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;

/**
 * 进货管理数据层接口
 * 
 * @author 陈浩翔
 * 
 * @version 1.0
 */
public interface InMainDao {

    /**
     * 创建一个进货数据对象
     * @param inMain---需要创建的进货数据对象
     * @return---true 表示创建成功,false 表示创建失败
     */
    public boolean create(InMainModel inMain);

    /**
     * 删除一个进货数据对象
     * @param uuid---进货单的编号-唯一的
     * @return---true表示删除成功, false表示删除失败
     */
    public boolean delete(String uuid);

    /**
     * 修改一个进货数据的对象
     * @param inMain---需要修改的进货管理对象
     * @return---true表示修改成功,false表示修改失败
     */
    public boolean update(InMainModel inMain);

    /**
     * 根据进货单编号,得到进货对象的其他信息资料
     * @param uuid---进货单编号
     * @return---该对象的其他其他信息资料
     */
    public InMainModel getSingle(String uuid);

    /**
     * 
     * @return---所有的进货管理对象信息
     */
    public List getAll();

    /**
     * 查找进货管理对象
     * @param imqm---查找条件!
     * @return---满足查找条件的所有INMainModel对象
     */
    public List getbyCondition(InMainQueryModel imqm);
}

进货明细管理数据层接口:InDetailDao

package cn.hncu.bookStore.in.dao.dao;

import java.util.List;

import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InDetailQueryModel;

/**
 * 进货明细管理数据层接口
 * @author 陈浩翔
 *
 * @version 1.0
 */
public interface InDetailDao {
    /**
     * 创建一个进货明细数据对象
     * @param inMain---需要创建的进货明细数据对象
     * @return---true 表示创建成功,false 表示创建失败
     */
    public boolean create(InDetailModel inDetail);

    /**
     * 删除一个进货明细数据对象
     * @param uuid---进货明细单的编号-唯一的
     * @return---true表示删除成功, false表示删除失败
     */
    public boolean delete(String uuid);

    /**
     * 修改一个进货明细数据的对象
     * @param inMain---需要修改的进货管理明细对象
     * @return---true表示修改成功,false表示修改失败
     */
    public boolean update(InDetailModel inDetail);

    /**
     * 根据进货明细单编号,得到进货明细对象的其他信息资料
     * @param uuid---进货明细单编号
     * @return---该对象的其他其他信息资料
     */
    public InDetailModel getSingle(String uuid);

    /**
     * 
     * @return---所有的进货明细管理对象信息
     */
    public List getAll();

    /**
     * 查找进货明细管理对象
     * @param imqm---查找条件!
     * @return---满足查找条件的所有InDetailModel对象
     */
    public List getbyCondition(InDetailQueryModel idqm);
}

进货明细的实现类:InDetailDaoSerImpl

package cn.hncu.bookStore.in.dao.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hncu.bookStore.in.dao.dao.InDetailDao;
import cn.hncu.bookStore.in.dao.factory.InDetailDaoFactory;
import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InDetailQueryModel;
import cn.hncu.bookStore.util.FileIoUtil;
import cn.hncu.bookStore.util.StringComparison;

/**
 * 进货明细的实现类
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InDetailDaoSerImpl implements InDetailDao{
    //进货明细数据的存储的文件名
    private final String FILE_NAME = "InDetail.txt";

    @Override
    public boolean create(InDetailModel inDetail) {
        List lists = FileIoUtil.readFormFile(FILE_NAME);

        for(InDetailModel model: lists){
            //已经存在这个Uuid,不能添加
            if(model.getUuid().equals(inDetail.getUuid())){
                return false;
            }
        }

        lists.add(inDetail);
        FileIoUtil.write2file(lists, FILE_NAME);
        return true;
    }

    @Override
    public boolean delete(String uuid) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        for(InDetailModel model: list){
            //存在,就删除
            if(model.getUuid().equals(uuid)){
                list.remove(model);
                FileIoUtil.write2file(list, FILE_NAME);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(InDetailModel inDetail) {
        List list  = FileIoUtil.readFormFile(FILE_NAME);
        for(int i=0;i list = FileIoUtil.readFormFile(FILE_NAME);
        for(InDetailModel model : list){
            //找到了,就返回
            if(model.getUuid().equals(uuid)){
                return model;
            }
        }
        return null;
    }

    @Override
    public List getAll() {
        return FileIoUtil.readFormFile(FILE_NAME);
    }

    @Override
    public List getbyCondition(InDetailQueryModel idqm) {
        List lists = getAll();
        List resulits = new ArrayList();
        for(InDetailModel model:lists){

            if(StringComparison.stringComp(idqm.getUuid(), model.getUuid())==-1){
                continue;
            }

            if(StringComparison.stringComp(idqm.getInId(), model.getInId())==-1){
                continue;
            }

            if(StringComparison.stringComp(idqm.getBookId(), model.getBookId())==-1){
                continue;
            }

            if(idqm.getSumNum()>0){
                if(model.getSumNum()0){
                if(model.getSumNum()>idqm.getSumNum2()){
                    continue;
                }
            }

            if(idqm.getSumMoney()>0){
                if(model.getSumMoney()0){
                if(model.getSumMoney()>idqm.getSumMoney2()){
                    continue;
                }
            }

            resulits.add(model);
        }
        return resulits;
    }

}

进货管理的实现类:InMainDaoSerImpl

package cn.hncu.bookStore.in.dao.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hncu.bookStore.in.dao.dao.InMainDao;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;
import cn.hncu.bookStore.util.FileIoUtil;
import cn.hncu.bookStore.util.StringComparison;

/**
 * 进货管理的实现类
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InMainDaoSerImpl implements InMainDao {

    private final String FILE_NAME = "InMain.txt";
    @Override
    public boolean create(InMainModel inMain) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        for(InMainModel model:list){
            if(model.getUuid().equals(inMain.getUuid())){//存在,则不能添加
                return false;
            }
        }
        list.add(inMain);
        FileIoUtil.write2file(list, FILE_NAME);
        return true;
    }

    @Override
    public boolean delete(String uuid) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        for(InMainModel model:list){
            if(model.getUuid().equals(uuid)){//存在,则删除
                list.remove(model);
                FileIoUtil.write2file(list, FILE_NAME);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(InMainModel inMain) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        for(int i=0;i list = FileIoUtil.readFormFile(FILE_NAME);
        for(InMainModel model:list){
            if(model.getUuid().equals(uuid)){//存在,则返回
                return model;
            }
        }
        return null;
    }

    @Override
    public List getAll() {
        return FileIoUtil.readFormFile(FILE_NAME);
    }

    @Override
    public List getbyCondition(InMainQueryModel imqm) {
        List lists = FileIoUtil.readFormFile(FILE_NAME);
        List results = new ArrayList();
        for(InMainModel model : lists){

            if(StringComparison.stringComp(imqm.getUuid(), model.getUuid())==-1){
                continue;
            }

            if(StringComparison.stringComp(imqm.getInUserId(), model.getInUserId())==-1){
                continue;
            }

            if(imqm.getInDate()>0){
                if(model.getInDate()0){
                if(model.getInDate()>imqm.getInDate2()){
                    continue;
                }
            }
            results.add(model);
        }
        return results;
    }

}

new 一个进货明细的实现类:InDetailDaoFactory

package cn.hncu.bookStore.in.dao.factory;

import cn.hncu.bookStore.in.dao.dao.InDetailDao;
import cn.hncu.bookStore.in.dao.impl.InDetailDaoSerImpl;

/**
 * 工厂方法----new 一个进货明细的实现类
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InDetailDaoFactory {

    public static InDetailDao getInDetailDao(){
        return new InDetailDaoSerImpl();
    }
}

new 一个进货实现类:InMainDaoFactory

package cn.hncu.bookStore.in.dao.factory;

import cn.hncu.bookStore.in.dao.dao.InMainDao;
import cn.hncu.bookStore.in.dao.impl.InMainDaoSerImpl;

/**
 * 工厂方法---new 一个进货实现类
 * @author 陈浩翔
 * @version 1.0
 */
public class InMainDaoFactory {

    public static InMainDao getInMainDao(){
        return new InMainDaoSerImpl();
    }
}

进货模块的逻辑层:

逻辑层的接口:InMainEbi

package cn.hncu.bookStore.in.business.ebi;

import java.util.List;
import java.util.Map;

import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InDetailQueryModel;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;

/**
 * 
 * @author 陈浩翔
 * @version 1.0
 */
public interface InMainEbi {

    /**
     * 创建一个进货模块数据-
     * @param inMain---进货数据
     * @param inDetails---进货明细数据
     * @return---返回true表示创建成功,false表示创建失败
     */
    public abstract boolean create(InMainModel inMain,List inDetails);

    /**
     * 
     * @return---返回所有的进货模块(包括进货明细)的集合
     */
    public abstract Map> getAll();

    /**
     * 
     * @param imqm---进货管理需要查找的条件
     * @param idqm---进货明细需要查找的条件
     * @return---满足条件的所有进货数据的Map集合
     */
    public abstract Map> getByCondition(InMainQueryModel imqm,InDetailQueryModel idqm);

}

逻辑层接口的实现类:InMainEbo

package cn.hncu.bookStore.in.business.ebo;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.common.UuidModelConstance;
import cn.hncu.bookStore.common.uuid.dao.dao.UuidDao;
import cn.hncu.bookStore.common.uuid.dao.factory.UuidDaoFactory;
import cn.hncu.bookStore.in.business.ebi.InMainEbi;
import cn.hncu.bookStore.in.dao.dao.InDetailDao;
import cn.hncu.bookStore.in.dao.dao.InMainDao;
import cn.hncu.bookStore.in.dao.factory.InDetailDaoFactory;
import cn.hncu.bookStore.in.dao.factory.InMainDaoFactory;
import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InDetailQueryModel;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.in.vo.InMainQueryModel;

public class InMainEbo implements InMainEbi{
    //注入dao

    InMainDao inMainDao = InMainDaoFactory.getInMainDao();
    InDetailDao inDetailDao = InDetailDaoFactory.getInDetailDao();
    UuidDao uuidDao = UuidDaoFactory.getUuidDao();
    BookEbi bookEbi = BookEbiFactory.getBookEbi();

    @Override
    public boolean create(InMainModel inMain, List inDetails) {
        //////////1存储inMain信息///////////
        //补全inMain中的数据
        //需要:inUuid,inDate,inUserUuid   已组织:inUserUuid
        //还缺(需补):inUuid,inDate
        String inUuid = uuidDao.getNextUuid(UuidModelConstance.IN_MAIN);
        inMain.setUuid(inUuid);
        inMain.setInDate(System.currentTimeMillis());
        inMainDao.create(inMain);

         //////////2存储inDetail信息///////////
        for(InDetailModel model:inDetails){
            //补全每一个inDetail中的数据
            //需要:inDetailUuid,inMainUuid,bookUuid,sumNum,sumMoney   已组织:bookUuid,sumNum
            //还缺(需补):inDetailUuid,inMainUuid,sumMoney
            model.setUuid(uuidDao.getNextUuid(UuidModelConstance.IN_DETAIL));
            model.setInId(inUuid);

            double sumMoney = model.getSumNum() * bookEbi.getSingle(model.getBookId()).getInPrice();
            model.setSumMoney(sumMoney);
            inDetailDao.create(model);
        }
        return true;
    }

    @Override
    public Map> getAll() {
        Map> map = new TreeMap>();

        List inMains = inMainDao.getAll();

        for(InMainModel inMain: inMains ){
            //查询条件值对象的创建
            InDetailQueryModel idqm = new InDetailQueryModel();
            String inUuid = inMain.getUuid();
            idqm.setInId(inUuid);

            List details = inDetailDao.getbyCondition(idqm);

            map.put(inMain, details);
        }

        return map;
    }

    @Override
    public Map> getByCondition(
            InMainQueryModel imqm, InDetailQueryModel idqm) {
        // TODO Auto-generated method stub
        return null;
    }

}

具体实现类的工厂方法:InMainEbiFactory

package cn.hncu.bookStore.in.business.factory;

import cn.hncu.bookStore.in.business.ebi.InMainEbi;
import cn.hncu.bookStore.in.business.ebo.InMainEbo;

/**
 * 工厂方法
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InMainEbiFactory {
    public static InMainEbi getInMainEbi(){
        return new InMainEbo();
    }
}

进货模块的表现层:

List主界面:InListPanel

/*
 * InListPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.in.ui;

import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;

import cn.hncu.bookStore.in.business.factory.InMainEbiFactory;
import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InMainModel;

/**
 * 
 * @author 陈浩翔
 *
 * @version 1.0
 */
public class InListPanel extends javax.swing.JPanel {

    private JFrame mainFrame = null;
    private Map> map;

    /** Creates new form InListPanel */
    public InListPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
        map = InMainEbiFactory.getInMainEbi().getAll();
        myInitData();
    }

    private void myInitData() {
        jListInMain.setListData(map.keySet().toArray());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnToAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListInMain = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListInDetail = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微软雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u8fdb\u8d27\u5217\u8868");
        add(jLabel1);
        jLabel1.setBounds(260, 0, 230, 80);

        btnToAdd.setFont(new java.awt.Font("Dialog", 1, 24));
        btnToAdd.setForeground(new java.awt.Color(0, 204, 204));
        btnToAdd.setText("\u8f6c\u5230\u6dfb\u52a0");
        btnToAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToAddActionPerformed(evt);
            }
        });
        add(btnToAdd);
        btnToAdd.setBounds(100, 460, 160, 70);

        jListInMain.setFont(new java.awt.Font("宋体", 1, 18));
        jListInMain.setForeground(new java.awt.Color(102, 102, 0));
        jListInMain.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jListInMain
                .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
                    public void valueChanged(
                            javax.swing.event.ListSelectionEvent evt) {
                        jListInMainValueChanged(evt);
                    }
                });
        jScrollPane1.setViewportView(jListInMain);

        add(jScrollPane1);
        jScrollPane1.setBounds(50, 110, 320, 300);

        jListInDetail.setFont(new java.awt.Font("宋体", 1, 18));
        jListInDetail.setForeground(new java.awt.Color(102, 102, 0));
        jListInDetail.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane2.setViewportView(jListInDetail);

        add(jScrollPane2);
        jScrollPane2.setBounds(400, 110, 370, 300);

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel2.setForeground(new java.awt.Color(51, 0, 204));
        jLabel2.setText("\u8fdb\u8d27\u660e\u7ec6\u5217\u8868\uff1a");
        add(jLabel2);
        jLabel2.setBounds(480, 80, 110, 30);
    }// 
    //GEN-END:initComponents

    protected void jListInMainValueChanged(ListSelectionEvent evt) {
        InMainModel inMain = (InMainModel) jListInMain.getSelectedValue();
        List details = map.get(inMain);
        //System.out.println(map);
        //System.out.println(inMain);
        jListInDetail.setListData(details.toArray());
    }

    protected void btnToAddActionPerformed(ActionEvent evt) {
        mainFrame.setContentPane(new InAddPanel(mainFrame));
        mainFrame.validate();
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnToAdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jListInDetail;
    private javax.swing.JList jListInMain;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}

添加进货界面类:InAddPanel

/*
 * InAddPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.in.ui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.plaf.ListUI;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.in.business.factory.InMainEbiFactory;
import cn.hncu.bookStore.in.vo.InDetailModel;
import cn.hncu.bookStore.in.vo.InMainModel;
import cn.hncu.bookStore.user.business.factory.UserEbiFactory;
import cn.hncu.bookStore.user.vo.UserModel;

/**
 * 
 * @author 陈浩翔
 * 
 * @version 1.0
 */
public class InAddPanel extends javax.swing.JPanel {

    private JFrame mainFrame = null;

    private List details = new ArrayList();

    /**
     * Creates new form InAddPanel
     * 
     * @param mainFrame
     */
    public InAddPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
        jtxArea.setEditable(false);
        myInitData();
    }

    private void myInitData() {
        // 1完成combBook组合框中数据的初始化
        // 访问book模块,把其中的图书获取出来,并把书名填写到combBook组合框中
        // 我们是UI层,只能访问(对方的)逻辑层
        List books = BookEbiFactory.getBookEbi().getAll();
        for (BookModel book : books) {
            combBook.addItem(book.getUuid() + "," + book.getName());
            // 若想更精确地控制每本书显示的信息,可以修改Book的toString()方法
        }

        // 2完成combUser组合框中数据的初始化
        /*
         * 模块之间的交接太深(细节太多了,功能可实现,但从开发技术或设计思想来讲不可取) 
          List users =UserEbiFactory.getUserEbi().getAll(); for(UserModel user: users){
          if(user.getType()==UserTypeEnum.IN.getType()){
          combUser.addItem(user.getName()); } }
         */

        List users = UserEbiFactory.getUserEbi().getAllIn();
        for (UserModel user : users) {
            combUser.addItem(user.getName());
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfdInNum = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        combUser = new javax.swing.JComboBox();
        combBook = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListDetails = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        btnAddDetail = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxArea = new javax.swing.JTextArea();
        btnBack = new javax.swing.JButton();
        btnMainAdd = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微软雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u6dfb\u52a0\u8fdb\u8d27");
        add(jLabel1);
        jLabel1.setBounds(270, 0, 210, 60);

        jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel4.setText("\u56fe\u4e66\u540d\u79f0:");
        add(jLabel4);
        jLabel4.setBounds(50, 100, 90, 30);

        tfdInNum.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInNum.setAutoscrolls(false);
        add(tfdInNum);
        tfdInNum.setBounds(140, 170, 150, 30);

        jLabel5.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel5.setForeground(new java.awt.Color(255, 51, 51));
        jLabel5.setText("  \u8fdb\u8d27\u660e\u7ec6\u5217\u8868\uff1a");
        add(jLabel5);
        jLabel5.setBounds(10, 220, 140, 30);

        jLabel6.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel6.setText("\u8fdb\u8d27\u4eba\uff1a");
        add(jLabel6);
        jLabel6.setBounds(480, 420, 80, 30);

        combUser.setFont(new java.awt.Font("Dialog", 1, 14));
        combUser.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "请选择..." }));
        add(combUser);
        combUser.setBounds(550, 420, 150, 30);

        combBook.setFont(new java.awt.Font("Dialog", 1, 14));
        combBook.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "请选择..." }));
        add(combBook);
        combBook.setBounds(140, 100, 150, 30);

        jListDetails.setFont(new java.awt.Font("Dialog", 1, 14));
        jListDetails.setForeground(new java.awt.Color(255, 51, 51));
        jListDetails.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane1.setViewportView(jListDetails);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 262, 410, 210);

        jLabel7.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel7.setText("\u8fdb\u8d27\u6570\u91cf\uff1a");
        add(jLabel7);
        jLabel7.setBounds(50, 170, 90, 30);

        btnAddDetail.setFont(new java.awt.Font("Dialog", 1, 14));
        btnAddDetail.setForeground(new java.awt.Color(0, 204, 204));
        btnAddDetail.setText("\u6dfb\u52a0\u5230\u660e\u7ec6");
        btnAddDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddDetailActionPerformed(evt);
            }
        });
        add(btnAddDetail);
        btnAddDetail.setBounds(480, 280, 120, 60);

        jtxArea.setColumns(20);
        jtxArea.setFont(new java.awt.Font("Dialog", 1, 14));
        jtxArea.setForeground(new java.awt.Color(0, 153, 51));
        jtxArea.setRows(5);
        jtxArea.setText("\u64cd\u4f5c\u8bf4\u660e\uff1a\n1\u3001\u9009\u62e9\u56fe\u4e66\u540d\u79f0\uff0c\u586b\u5199\u8fdb\u8d27\u6570\u91cf\u3002\n2\u3001\u518d\u70b9\u51fb\u4e0b\u65b9\u7684\u6dfb\u52a0\u5230\u660e\u7ec6\u6309\u94ae\uff0c\n      \u6dfb\u52a0\u6570\u636e\u5230\u8fdb\u8d27\u660e\u7ec6\u5217\u8868\uff0c\n      \u5982\u679c\u8fd8\u6709\u56fe\u4e66\uff0c\u91cd\u590d\u524d\u9762\u7684\u64cd\u4f5c\u3002\n3\u3001\u6700\u540e\u70b9\u51fb\u6dfb\u52a0\u6309\u94ae\u3002");
        jScrollPane2.setViewportView(jtxArea);

        add(jScrollPane2);
        jScrollPane2.setBounds(440, 100, 340, 140);

        btnBack.setFont(new java.awt.Font("Dialog", 1, 24));
        btnBack.setForeground(new java.awt.Color(0, 204, 204));
        btnBack.setText("\u8fd4\u56de");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack);
        btnBack.setBounds(530, 510, 120, 60);

        btnMainAdd.setFont(new java.awt.Font("Dialog", 1, 24));
        btnMainAdd.setForeground(new java.awt.Color(0, 204, 204));
        btnMainAdd.setText("\u6dfb\u52a0");
        btnMainAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMainAddActionPerformed(evt);
            }
        });
        add(btnMainAdd);
        btnMainAdd.setBounds(120, 510, 120, 60);
    }// 
    //GEN-END:initComponents

    private void btnMainAddActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集参数 
        String inUserName = combUser.getSelectedItem().toString();
        if(combUser.getSelectedIndex()==0){
            JOptionPane.showMessageDialog(mainFrame, "请选择用户类型!");
            return ;
        }
        //System.out.println(inUserName);

        String inUserUuid = UserEbiFactory.getUserEbi().getUserByName(inUserName).getUuid();

        //2组织参数
        InMainModel inMain = new InMainModel();
        inMain.setInUserId(inUserUuid);
        inMain.setInUserName(inUserName);//设置:专为外键显示而补的数据

        //3调用逻辑层
        //System.out.println(inMain);
        //System.out.println(details);
        InMainEbiFactory.getInMainEbi().create(inMain, details);

        //4返回到结果页面
        mainFrame.setContentPane( new InListPanel(mainFrame));
        mainFrame.validate();

    }

    private void btnAddDetailActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集参数
        String str = combBook.getSelectedItem().toString();

        //防范未选择图书
        if (combBook.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(mainFrame, "请选择图书!");
            return;
        }
        String strs[] = str.split(",");
        String bookUuid = strs[0];
        String bookName = strs[1];
        //      System.out.println(bookName);
        //      System.out.println(bookUuid);

        int sumNum = 0;
        try {
            sumNum = Integer.parseInt(tfdInNum.getText());
            if (sumNum < 0) {
                throw new NumberFormatException();
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(mainFrame, "进货数量格式错误!");
            return;
        }

        //2组织
        InDetailModel detail = new InDetailModel();
        detail.setBookId(bookUuid);
        detail.setSumNum(sumNum);
        //为显示给用户看,补的数据
        detail.setBookName(bookName);

        //3添加到List当中---真正的明细数据是details,界面上的只是回显信息
        details.add(detail);
        echoDetails();
    }

    private void echoDetails() {
        String echoStrs[] = new String[details.size()];

        for (int i = 0; i < details.size(); i++) {
            InDetailModel detail = details.get(i);
            echoStrs[i] = "《" + detail.getBookName() + "》,"
                    + detail.getSumNum() + "本";
        }
        jListDetails.setListData(echoStrs);
    }

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void back() {
        mainFrame.setContentPane(new InListPanel(mainFrame));
        mainFrame.validate();
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnAddDetail;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnMainAdd;
    private javax.swing.JComboBox combBook;
    private javax.swing.JComboBox combUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jListDetails;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jtxArea;
    private javax.swing.JTextField tfdInNum;
    // End of variables declaration//GEN-END:variables

}
相关文章
最新文章
热点推荐