书店管理系统
书店管理系统可以说是设计模式及设计思想的一个比较经典的例子。
本系列将分为多个部分讲述此输电管理系统。
书店管理系统将分为:用户、图书、进货、销售和库存五个模块,另外还有公共包、工具包和登录包,另外还有一个框架。
对于分层设计,都是表现层可以调用逻辑层,逻辑层调用数据层,数据层调用工具和公共包,方向不可打乱,必须严格按照这种模式。
本篇将做图书模块。
注:本篇需要使用到的框架在本系列二的用户模块:
链接:http://blog.csdn.net/x121850182/article/details/51362269
和用户模块相同的,图书模块分成了数据层、逻辑层、表现层和值对象层。
数据层:
接口:
package cn.hncu.book.dao.dao; import java.util.List; import cn.hncu.book.vo.BookModel; import cn.hncu.book.vo.BookQueryModel; public interface BookDAO { public boolean create(BookModel book); public boolean delete(String uuid); public boolean upDate(BookModel book); public BookModel getSingle(String uuid); public List<BookModel> getAll(); public List<BookModel> getByCondition(BookQueryModel bqm); }实现类:
package cn.hncu.book.dao.impl; import java.util.ArrayList; import java.util.List; import cn.hncu.book.dao.dao.BookDAO; import cn.hncu.book.vo.BookModel; import cn.hncu.book.vo.BookQueryModel; import cn.hncu.utils.FileIo; public class BookDAOImpl implements BookDAO { private static final String FILE_NAME="book.txt"; @Override public boolean create(BookModel book) { List<BookModel> list=FileIo.read(FILE_NAME); for (BookModel u:list){ if (u.getUuid().equals(book.getUuid())){ return false; } } list.add(book); FileIo.write(list, FILE_NAME); return true; } @Override public boolean delete(String uuid) { List<BookModel> list=FileIo.read(FILE_NAME); for (int i=0;i<list.size();i++){ if (list.get(i).getUuid().equals(uuid)){ list.remove(i); FileIo.write(list, FILE_NAME); return true; } } return false; } @Override public List<BookModel> getAll() { List<BookModel> list=FileIo.read(FILE_NAME); return list; } @Override public List<BookModel> getByCondition(BookQueryModel bqm) { List<BookModel> list=getAll(); List<BookModel> results=new ArrayList<BookModel>(); for (BookModel book:list){ //反逻辑,卫条件: 外层判断用户输入是否是查询条件;内层判断该对象是否符合查询条件 if (bqm.getUuid()!=null&&bqm.getUuid().trim().length()>0){ if (!bqm.getUuid().equals(book.getUuid())){ continue; } } if (bqm.getName()!=null&&bqm.getName().trim().length()>0){ if (book.getName().indexOf(bqm.getName())==-1){ continue; } } if (bqm.getInPrice()>0){ if (bqm.getInPrice()>book.getInPrice()){ continue; } } if (bqm.getHighInPrice()>0){ if (bqm.getHighInPrice()<book.getInPrice()){ continue; } } if (bqm.getSalePrice()>0){ if (bqm.getSalePrice()>book.getSalePrice()){ continue; } } if (bqm.getHighSalePrice()>0){ if (bqm.getHighSalePrice()<book.getSalePrice()){ continue; } } results.add(book); } return results; } @Override public BookModel getSingle(String uuid) { List<BookModel> list=FileIo.read(FILE_NAME); for (BookModel u:list){ if (u.getUuid().equals(uuid)){ return u; } } return null; } @Override public boolean upDate(BookModel book) { List<BookModel> list=FileIo.read(FILE_NAME); for (int i=0;i<list.size();i++){ if (list.get(i).getUuid().equals(book.getUuid())){ list.set(i, book); FileIo.write(list, FILE_NAME); return true; } } return false; } }工厂类:
package cn.hncu.book.dao.factory; import cn.hncu.book.dao.dao.BookDAO; import cn.hncu.book.dao.impl.BookDAOImpl; public class BookDAOFactory { public static BookDAO getBookDAO(){ return new BookDAOImpl(); } }
接口:
package cn.hncu.book.business.ebi; import java.util.List; import cn.hncu.book.vo.BookModel; import cn.hncu.book.vo.BookQueryModel; public interface BookEbi { public boolean create(BookModel book); public boolean delete(String uuid); public boolean upDate(BookModel book); public BookModel getSingle(String uuid); public List<BookModel> getAll(); public List<BookModel> getByCondition(BookQueryModel bqm); public BookModel getBookByName(String bookName); }实现类:
package cn.hncu.book.business.ebo; import java.util.List; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.dao.dao.BookDAO; import cn.hncu.book.dao.factory.BookDAOFactory; import cn.hncu.book.vo.BookModel; import cn.hncu.book.vo.BookQueryModel; import cn.hncu.common.UuidModelConstance; import cn.hncu.common.uuid.dao.factory.UuidDAOFactory; public class BookEbo implements BookEbi { BookDAO dao=BookDAOFactory.getBookDAO(); @Override public boolean create(BookModel book) { String uuid=UuidDAOFactory.getUuidDAO().getNextUuid(UuidModelConstance.BOOK); book.setUuid(uuid); return dao.create(book); } @Override public boolean delete(String uuid) { return dao.delete(uuid); } @Override public List<BookModel> getAll() { return dao.getAll(); } @Override public List<BookModel> getByCondition(BookQueryModel bqm) { return dao.getByCondition(bqm); } @Override public BookModel getSingle(String uuid) { return dao.getSingle(uuid); } @Override public boolean upDate(BookModel book) { return dao.upDate(book); } @Override public BookModel getBookByName(String bookName) { BookQueryModel bqm=new BookQueryModel(); bqm.setName(bookName); return getByCondition(bqm).get(0); } }工厂类:
package cn.hncu.book.business.factory; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.ebo.BookEbo; public class BookEbiFactory { public static BookEbi getBookEbi(){ return new BookEbo(); } }
package cn.hncu.book.vo; import java.io.Serializable; public class BookModel implements Serializable{ private String uuid; private String name; private double inPrice; private double salePrice; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getInPrice() { return inPrice; } public void setInPrice(double inPrice) { this.inPrice = inPrice; } public double getSalePrice() { return salePrice; } public void setSalePrice(double salePrice) { this.salePrice = salePrice; } @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; BookModel other = (BookModel) 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 + ", " + name + ", 进价=" + inPrice + ", 售价=" + salePrice; } }图书查询模型:
package cn.hncu.book.vo; public class BookQueryModel extends BookModel { private double highInPrice; private double highSalePrice; public double getHighInPrice() { return highInPrice; } public void setHighInPrice(double highInPrice) { this.highInPrice = highInPrice; } public double getHighSalePrice() { return highSalePrice; } public void setHighSalePrice(double highSalePrice) { this.highSalePrice = highSalePrice; } }
/* * BookListPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.book.ui; import java.util.List; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.factory.BookEbiFactory; import cn.hncu.book.vo.BookModel; /** * * @author __USER__ */ public class BookListPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form BookListPanel */ public BookListPanel(JFrame mainFrame) { this.mainFrame = mainFrame; this.setOpaque(false); initComponents(); myInit(); } public BookListPanel(JFrame mainFrame, List<BookModel> list) { this.mainFrame = mainFrame; initComponents(); bookList.setListData(list.toArray()); } private void myInit() { BookEbi ebi = BookEbiFactory.getBookEbi(); List<BookModel> list = ebi.getAll(); bookList.setListData(list.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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); bookList = new javax.swing.JList(); queryBtn = new javax.swing.JButton(); addBtn = new javax.swing.JButton(); deleteBtn = new javax.swing.JButton(); btnUpdate = new javax.swing.JButton(); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24)); jLabel1.setText("\u56fe\u4e66\u5217\u8868"); add(jLabel1); jLabel1.setBounds(350, 50, 150, 70); bookList.setModel(new javax.swing.AbstractListModel() { String[] strings = { "" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(bookList); add(jScrollPane1); jScrollPane1.setBounds(250, 120, 300, 170); queryBtn.setText("\u67e5\u8be2"); queryBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { queryBtnActionPerformed(evt); } }); add(queryBtn); queryBtn.setBounds(540, 340, 63, 29); addBtn.setText("\u6dfb\u52a0"); addBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addBtnActionPerformed(evt); } }); add(addBtn); addBtn.setBounds(200, 340, 63, 29); deleteBtn.setText("\u5220\u9664"); deleteBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteBtnActionPerformed(evt); } }); add(deleteBtn); deleteBtn.setBounds(320, 340, 63, 29); btnUpdate.setText("\u4fee\u6539"); btnUpdate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUpdateActionPerformed(evt); } }); add(btnUpdate); btnUpdate.setBounds(420, 340, 63, 29); }// </editor-fold> //GEN-END:initComponents private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) { BookModel book = (BookModel) bookList.getSelectedValue(); if (book == null) { JOptionPane.showMessageDialog(null, "请选择要修改的用户!"); return; } String uuid = book.getUuid(); mainFrame.setContentPane(new BookUpDatePanel(mainFrame, uuid)); mainFrame.validate(); } private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) { BookModel book = (BookModel) bookList.getSelectedValue(); if (book == null) { JOptionPane.showMessageDialog(null, "请选择要删除的用户!"); return; } String uuid = book.getUuid(); mainFrame.setContentPane(new BookDeletePanel(mainFrame, uuid)); mainFrame.validate(); } private void addBtnActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookAddPanel(mainFrame)); mainFrame.validate(); } private void queryBtnActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookQueryPanel(mainFrame)); mainFrame.validate(); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton addBtn; private javax.swing.JList bookList; private javax.swing.JButton btnUpdate; private javax.swing.JButton deleteBtn; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton queryBtn; // End of variables declaration//GEN-END:variables }添加图书:
/* * BookAddPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.book.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.factory.BookEbiFactory; import cn.hncu.book.vo.BookModel; /** * * @author __USER__ */ public class BookAddPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form BookAddPanel */ public BookAddPanel(JFrame mainFrame) { this.setOpaque(false); this.mainFrame = mainFrame; initComponents(); } //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabelName = new javax.swing.JLabel(); tfdName = new javax.swing.JTextField(); btnAdd = new javax.swing.JButton(); btnBack = new javax.swing.JButton(); jLabelName1 = new javax.swing.JLabel(); tfdSalePrice = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); tfdInPrice = new javax.swing.JTextField(); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24)); jLabel1.setText("\u6dfb\u52a0\u56fe\u4e66"); add(jLabel1); jLabel1.setBounds(350, 30, 140, 70); jLabelName.setText("\u4e66\u540d\uff1a"); add(jLabelName); jLabelName.setBounds(180, 110, 70, 30); add(tfdName); tfdName.setBounds(250, 110, 100, 30); btnAdd.setText("\u6dfb\u52a0"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); add(btnAdd); btnAdd.setBounds(290, 340, 63, 29); 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(440, 340, 63, 29); jLabelName1.setText("\u552e\u4ef7\uff1a"); add(jLabelName1); jLabelName1.setBounds(420, 210, 70, 30); add(tfdSalePrice); tfdSalePrice.setBounds(490, 210, 100, 30); jLabel5.setText("\u8fdb\u4ef7\uff1a"); add(jLabel5); jLabel5.setBounds(180, 210, 52, 20); add(tfdInPrice); tfdInPrice.setBounds(250, 210, 100, 30); }// </editor-fold> //GEN-END:initComponents private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { //收集参数 // String uuid = tfdUuid.getText(); String name = tfdName.getText(); double inPrice = Double.parseDouble(tfdInPrice.getText()); double salePrice = Double.parseDouble(tfdSalePrice.getText()); //组织参数 BookModel book = new BookModel(); book.setName(name); // book.setUuid(uuid); book.setInPrice(inPrice); book.setSalePrice(salePrice); //调用逻辑层 BookEbi ebi = BookEbiFactory.getBookEbi(); if (ebi.create(book)) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } else { JOptionPane.showMessageDialog(null, "该用户已存在!"); } } private void btnBackActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnBack; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabelName; private javax.swing.JLabel jLabelName1; private javax.swing.JTextField tfdInPrice; private javax.swing.JTextField tfdName; private javax.swing.JTextField tfdSalePrice; // End of variables declaration//GEN-END:variables }删除图书:
/* * BookDeletePanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.book.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.factory.BookEbiFactory; import cn.hncu.book.vo.BookModel; /** * * @author __USER__ */ public class BookDeletePanel extends javax.swing.JPanel { private JFrame mainFrame=null; private String uuid=null; /** Creates new form BookDeletePanel */ public BookDeletePanel(JFrame mainFrame,String uuid) { this.mainFrame=mainFrame; this.setOpaque(false); this.uuid=uuid; initComponents(); myInit(); } private void myInit() { BookModel book=BookEbiFactory.getBookEbi().getSingle(uuid); tfdUuid.setText(book.getUuid()); tfdUuid.setEditable(false); tfdName.setText(book.getName()); tfdName.setEditable(false); tfdInPrice.setText(""+book.getInPrice()); tfdInPrice.setEditable(false); tfdSalePrice.setText(""+book.getSalePrice()); tfdSalePrice.setEditable(false); } /** 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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabelName = new javax.swing.JLabel(); tfdName = new javax.swing.JTextField(); btnDelete = new javax.swing.JButton(); btnBack = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); tfdUuid = new javax.swing.JTextField(); jLabelName1 = new javax.swing.JLabel(); tfdSalePrice = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); tfdInPrice = new javax.swing.JTextField(); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24)); jLabel1.setText("\u5220\u9664\u56fe\u4e66"); add(jLabel1); jLabel1.setBounds(290, 30, 140, 70); jLabelName.setText("\u4e66\u540d\uff1a"); add(jLabelName); jLabelName.setBounds(360, 110, 70, 30); add(tfdName); tfdName.setBounds(430, 110, 100, 30); btnDelete.setText("\u5220\u9664"); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); add(btnDelete); btnDelete.setBounds(230, 340, 63, 29); 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(380, 340, 63, 29); jLabel4.setText("uuid\uff1a"); add(jLabel4); jLabel4.setBounds(120, 110, 52, 20); add(tfdUuid); tfdUuid.setBounds(190, 110, 100, 30); jLabelName1.setText("\u552e\u4ef7\uff1a"); add(jLabelName1); jLabelName1.setBounds(360, 210, 70, 30); add(tfdSalePrice); tfdSalePrice.setBounds(430, 210, 100, 30); jLabel5.setText("\u8fdb\u4ef7\uff1a"); add(jLabel5); jLabel5.setBounds(120, 210, 52, 20); add(tfdInPrice); tfdInPrice.setBounds(190, 210, 100, 30); }// </editor-fold> //GEN-END:initComponents private void btnBackActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { boolean flag = BookEbiFactory.getBookEbi().delete(uuid); if (flag) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } else { JOptionPane.showMessageDialog(null, "该用户已经不存在!"); } } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnBack; private javax.swing.JButton btnDelete; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabelName; private javax.swing.JLabel jLabelName1; private javax.swing.JTextField tfdInPrice; private javax.swing.JTextField tfdName; private javax.swing.JTextField tfdSalePrice; private javax.swing.JTextField tfdUuid; // End of variables declaration//GEN-END:variables }修改图书:
/* * BookUpDatePanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.book.ui; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.factory.BookEbiFactory; import cn.hncu.book.vo.BookModel; /** * * @author __USER__ */ public class BookUpDatePanel extends javax.swing.JPanel { private JFrame mainFrame = null; private String uuid = null; /** Creates new form BookUpDatePanel */ public BookUpDatePanel(JFrame mainFrame, String uuid) { this.mainFrame = mainFrame; this.uuid = uuid; this.setOpaque(false); initComponents(); myInit(); } private void myInit() { BookModel book = BookEbiFactory.getBookEbi().getSingle(uuid); tfdUuid.setText(uuid); tfdUuid.setEditable(false); tfdName.setText(book.getName()); tfdInPrice.setText("" + book.getInPrice()); tfdSalePrice.setText("" + book.getSalePrice()); } /** 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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabelName = new javax.swing.JLabel(); tfdName = new javax.swing.JTextField(); btnUpDate = new javax.swing.JButton(); btnBack = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); tfdUuid = new javax.swing.JTextField(); jLabelName1 = new javax.swing.JLabel(); tfdSalePrice = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); tfdInPrice = new javax.swing.JTextField(); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24)); jLabel1.setText("\u4fee\u6539\u56fe\u4e66"); add(jLabel1); jLabel1.setBounds(290, 30, 140, 70); jLabelName.setText("\u4e66\u540d\uff1a"); add(jLabelName); jLabelName.setBounds(360, 110, 70, 30); add(tfdName); tfdName.setBounds(430, 110, 100, 30); btnUpDate.setText("\u4fee\u6539"); btnUpDate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUpDateActionPerformed(evt); } }); add(btnUpDate); btnUpDate.setBounds(230, 340, 63, 29); 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(380, 340, 63, 29); jLabel4.setText("uuid\uff1a"); add(jLabel4); jLabel4.setBounds(120, 110, 52, 20); add(tfdUuid); tfdUuid.setBounds(190, 110, 100, 30); jLabelName1.setText("\u552e\u4ef7\uff1a"); add(jLabelName1); jLabelName1.setBounds(360, 210, 70, 30); add(tfdSalePrice); tfdSalePrice.setBounds(430, 210, 100, 30); jLabel5.setText("\u8fdb\u4ef7\uff1a"); add(jLabel5); jLabel5.setBounds(120, 210, 52, 20); add(tfdInPrice); tfdInPrice.setBounds(190, 210, 100, 30); }// </editor-fold> //GEN-END:initComponents protected void btnUpDateActionPerformed(ActionEvent evt) { //收集参数 String name = tfdName.getText(); double inPrice = Double.parseDouble(tfdInPrice.getText()); double salePrice = Double.parseDouble(tfdSalePrice.getText()); //组织参数 BookModel book = new BookModel(); book.setUuid(uuid); book.setName(name); book.setInPrice(inPrice); book.setSalePrice(salePrice); //调用逻辑层 BookEbi ebi = BookEbiFactory.getBookEbi(); if (ebi.upDate(book)) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } else { JOptionPane.showMessageDialog(null, "该用户已经不存在!"); } } private void btnBackActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnBack; private javax.swing.JButton btnUpDate; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabelName; private javax.swing.JLabel jLabelName1; private javax.swing.JTextField tfdInPrice; private javax.swing.JTextField tfdName; private javax.swing.JTextField tfdSalePrice; private javax.swing.JTextField tfdUuid; // End of variables declaration//GEN-END:variables }查询图书:
/* * BookQueryPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.book.ui; import java.util.List; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.book.business.ebi.BookEbi; import cn.hncu.book.business.factory.BookEbiFactory; import cn.hncu.book.vo.BookModel; import cn.hncu.book.vo.BookQueryModel; /** * * @author __USER__ */ public class BookQueryPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form BookQueryPanel */ public BookQueryPanel(JFrame mainFrame) { this.mainFrame = mainFrame; this.setOpaque(false); initComponents(); } /** 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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabelName = new javax.swing.JLabel(); tfdHighPrice = new javax.swing.JTextField(); btnQuery = new javax.swing.JButton(); btnBack = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); tfdLowPrice = new javax.swing.JTextField(); jLabelName1 = new javax.swing.JLabel(); tfdName = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); tfdUuid = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); tfdHighSalePrice = new javax.swing.JTextField(); tfdLowSalePrice = new javax.swing.JTextField(); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24)); jLabel1.setText("\u67e5\u8be2\u56fe\u4e66"); add(jLabel1); jLabel1.setBounds(280, 10, 140, 70); jLabelName.setText("\u6700\u9ad8\u8fdb\u4ef7\uff1a"); add(jLabelName); jLabelName.setBounds(320, 170, 100, 30); add(tfdHighPrice); tfdHighPrice.setBounds(420, 170, 100, 30); btnQuery.setText("\u67e5\u8be2"); btnQuery.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnQueryActionPerformed(evt); } }); add(btnQuery); btnQuery.setBounds(240, 360, 63, 30); 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(340, 360, 63, 30); jLabel4.setText("\u6700\u4f4e\u8fdb\u4ef7\uff1a"); add(jLabel4); jLabel4.setBounds(90, 170, 80, 20); add(tfdLowPrice); tfdLowPrice.setBounds(190, 170, 100, 30); jLabelName1.setText("\u4e66\u540d\uff1a"); add(jLabelName1); jLabelName1.setBounds(350, 100, 70, 30); add(tfdName); tfdName.setBounds(420, 100, 100, 30); jLabel5.setText("uuid\uff1a"); add(jLabel5); jLabel5.setBounds(120, 100, 52, 20); add(tfdUuid); tfdUuid.setBounds(190, 100, 100, 30); jLabel2.setText("\u6700\u9ad8\u552e\u4ef7\uff1a"); add(jLabel2); jLabel2.setBounds(320, 250, 100, 40); jLabel3.setText("\u6700\u4f4e\u552e\u4ef7\uff1a"); add(jLabel3); jLabel3.setBounds(90, 250, 90, 40); add(tfdHighSalePrice); tfdHighSalePrice.setBounds(420, 260, 100, 30); add(tfdLowSalePrice); tfdLowSalePrice.setBounds(190, 260, 100, 30); }// </editor-fold> //GEN-END:initComponents private void btnBackActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new BookListPanel(mainFrame)); mainFrame.validate(); } private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) { //收集参数 String uuid = tfdUuid.getText(); String name = tfdName.getText(); double inPrice = 0; if (tfdLowPrice.getText() != null && tfdLowPrice.getText().trim().length() > 0) { try { inPrice = Double.parseDouble(tfdLowPrice.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "最低进价格式输入错误"); return; } } double highInPrice = 0; if (tfdHighPrice.getText() != null && tfdHighPrice.getText().trim().length() > 0) { try { highInPrice = Double.parseDouble(tfdHighPrice.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "最高进价格式输入错误"); return; } } double salePrice = 0; if (tfdLowSalePrice.getText() != null && tfdLowSalePrice.getText().trim().length() > 0) { try { salePrice = Double.parseDouble(tfdLowSalePrice.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "最低售价格式输入错误"); return; } } double highSalePrice = 0; if (tfdHighSalePrice.getText() != null && tfdHighSalePrice.getText().trim().length() > 0) { try { highSalePrice = Double.parseDouble(tfdHighSalePrice.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "最高售价格式输入错误"); return; } } //组织参数 BookQueryModel bqm = new BookQueryModel(); bqm.setName(name); bqm.setUuid(uuid); bqm.setInPrice(inPrice); bqm.setHighInPrice(highInPrice); bqm.setSalePrice(salePrice); bqm.setHighSalePrice(highSalePrice); //调用逻辑层 BookEbi ebi = BookEbiFactory.getBookEbi(); List<BookModel> list = ebi.getByCondition(bqm); mainFrame.setContentPane(new BookListPanel(mainFrame, list)); mainFrame.validate(); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnBack; private javax.swing.JButton btnQuery; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabelName; private javax.swing.JLabel jLabelName1; private javax.swing.JTextField tfdHighPrice; private javax.swing.JTextField tfdHighSalePrice; private javax.swing.JTextField tfdLowPrice; private javax.swing.JTextField tfdLowSalePrice; private javax.swing.JTextField tfdName; private javax.swing.JTextField tfdUuid; // End of variables declaration//GEN-END:variables }