Java-Single-player version of bookstore management system (practice design modules and ideas_Series 1): //www.VeVB.COM/article/91004.htm
introduce
Tip: There is a directory at the above point that can quickly locate the classes you need to see.
Today, a small modification to the previous code has been made to make the code more perfect.
As for the user's unique identification code uuid, it will be modified to be generated internally in the program in the future.
The current uuid is still set by the user.
Today, for this program, we added part of the presentation layer of the user interface and added public class enumeration.
Below is a posting of all the codes for the program I have written: I will gradually finish writing this program, please rest assured! (The functions that need to be implemented can be found in the series of bookstore management systems. I have classified the articles for this series, so that everyone can find them)
This series of blogs will never be interrupted.
The code is now layered:
The picture after the program is running:
I posted the code in the order from the top to the bottom of the directory:
Please note! This code order is not the order I write the code!
If you want to refer to my writing, please do not follow the order of the code I posted.
You should first write public classes and tool classes.
Again: Data layer class -> Logical layer class -> Presentation layer class
Some pictures after the program is running:
UserTypeEnum class:
cn.hncu.bookStore.common;
UserTypeEnum class:
package cn.hncu.bookStore.common;/** * Function: Enumeration of user types! <br/> * Defined in a public module. <br/> * Variables: <br/> * ADMIN(1,"Super Administrator"),<br/> * BOOK(2,"Librarian"),<br/> * IN(3,"Purchase Administrator"),<br/> * OUT(4,"Sales Administrator"),<br/> * STOCK(5,"Inventory Administrator");<br/> * @author chx * @version 1.0 */public enum UserTypeEnum { ADMIN(1,"Super Administrator"), BOOK(2,"Librarian"), IN(3,"Purchase Administrator"), OUT(4,"Sales Administrator"), STOCK(5,"Inventory Administrator"); private final int type; private final String name; /** * Initialize the enumeration variable name* @param type---Integer number corresponding to the enumeration variable* @param name---String type name corresponding to the enumeration variable*/ private UserTypeEnum(int type, String name) { this.type=type; this.name=name; } /** * Get the number of the current enumeration variable* @return---type-number*/ public int getType() { return type; } /** * Get the Chinese name of the current enumeration variable* @return---name-Chinese name*/ public String getName() { return name; } /** * Get the Chinese name of the enumeration variable corresponding to the number based on the int number* @param type---The int parameter required to be passed* @return ---If there is an enumeration variable corresponding to such a number, the Chinese name of the enumeration variable is returned. * <br/>--If there is no enumeration variable corresponding to such a number, an exception message will be thrown. */ public static String getNameByType(int type){ for(UserTypeEnum userType:UserTypeEnum.values()){ if(userType.getType()==type){ return userType.getName(); } } throw new IllegalArgumentException("There is no corresponding user type in the enumeration:"+type); } /** * Get the int type of the enumeration variable corresponding to the name based on the Chinese name of the enumeration variable * @param name---String type name that needs to be passed in * @return ---If there is an enumeration variable corresponding to such a name, return the type-int corresponding to this enumeration variable * <br/> ---If there is no enumeration variable corresponding to such a name, throw an exception message */ public static int getTypeByName(String name){ for(UserTypeEnum userType:UserTypeEnum.values()){ if(userType.getName().equals(name)){ return userType.getType(); } } throw new IllegalArgumentException("There is no corresponding user type in the enumeration:"+name); }}UserEbi interface:
cn.hncu.bookStore.user.business.ebi;
UserEbi interface:
package cn.hncu.bookStore.user.business.ebi;import java.util.List;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;/** * Interface of the logic layer* * @author chx * @version 1.0 */public interface UserEbi { /** * Function: Create a user* * @param userModel---User data to be created* @return---true means creation successful, false means creation failed*/ public boolean create(UserModel user); /** * Function: Delete a user based on the user's unique identification code uuid * * @param uuid---User's unique identification code, each user will not be the same * @return---true means the deletion is successful, false means the deletion failed*/ public boolean delete(String uuid); /** * Function: Modify the user's data information* * @param user---User data parameter name that needs to be modified* @return Return true- indicates that the modification is successful, return false- indicates that the modification has failed*/ public boolean update(UserModel user); /** * Function: Get all user data* * @return---A UserModel collection, that is, the user's data*/ public List<UserModel> getAll(); /** * Function: Search according to certain search conditions, * <br/> * Return user data that meets the search conditions. * * @param uqm---encapsulated search conditions* @return----User data set that meets the search conditions*/ public List<UserModel> getbyCondition(UserQueryModel uqm); /** * Function: Get a certain user data* * @param uuid---User unique identification code* @return ---Return the user data found according to this unique identification code*/ public UserModel getSingle(String uuid);}UserEbo class:
cn.hncu.bookStore.user.business.ebo;
UserEbo class:
package cn.hncu.bookStore.user.business.ebo;import java.util.List;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.dao.dao.UserDao;import cn.hncu.bookStore.user.dao.factory.UserDaoFactory;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;public class UserEbo implements UserEbi{ private UserDao dao = UserDaoFactory.getUserDao(); @Override public boolean create(UserModel user) { return dao.create(user); } @Override public boolean delete(String uuid) { return dao.delete(uuid); } @Override public boolean update(UserModel user) { return dao.update(user); } @Override public List<UserModel> getAll() { return dao.getAll(); } @Override public List<UserModel> getbyCondition(UserQueryModel uqm) { // TODO Auto-generated method stub return null; } @Override public UserModel getSingle(String uuid) { return dao.getSingle(uuid); }}UserEbiFactory class:
cn.hncu.bookStore.user.business.factory;
UserEbiFactory class:
package cn.hncu.bookStore.user.business.factory;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.business.ebo.UserEbo;public class UserEbiFactory { public static UserEbi getUserEbi(){ return new UserEbo(); }}UserDao interface:
cn.hncu.bookStore.user.dao.dao;
UserDao interface:
package cn.hncu.bookStore.user.dao.dao;import java.util.List;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;/** * * @author Chen Haoxiang* * @version 1.0 * Data layer interface of user module*/public interface UserDao { /** * Function: Create a user* * @param userModel---user data to be created* @return--true means creation successful, false means creation failed*/ public boolean create(UserModel user); /** * Function: Delete a user based on the user's unique identification code uuid * * @param uuid---User's unique identification code, each user will not be the same * @return---true means the deletion is successful, false means the deletion failed*/ public boolean delete(String uuid); /** * Function: Modify the user's data information* * @param user---User data parameter name that needs to be modified* @return Return true- means the modification is successful, return false- means the modification failed*/ public boolean update(UserModel user); /** * Function: Get all user data* * @return---A UserModel collection, that is, the user's data*/ public List<UserModel> getAll(); /** * Function: Search according to certain search conditions, * <br/> * Return user data that meets the search conditions. * * @param uqm---encapsulated search conditions* @return----User data set that meets the search conditions*/ public List<UserModel> getbyCondition(UserQueryModel uqm); /** * Function: Get a certain user data* * @param uuid---User unique identification code* @return ---Return the user data found according to this unique identification code*/ public UserModel getSingle(String uuid);}UserDaoFactory class:
cn.hncu.bookStore.user.dao.factory;
UserDaoFactory class:
package cn.hncu.bookStore.user.dao.factory;import cn.hncu.bookStore.user.dao.dao.UserDao;import cn.hncu.bookStore.user.dao.impl.UserDaoSerImpl;/** * Factory method<br/> * new instance of dao* @author Chen Haoxiang* * @version 1.0 * */public class UserDaoFactory { public static UserDao getUserDao(){ return new UserDaoSerImpl(); }}UserDaoSerImpl class:
cn.hncu.bookStore.user.dao.impl;
UserDaoSerImpl class:
package cn.hncu.bookStore.user.dao.impl;import java.util.ArrayList;import java.util.List;import cn.hncu.bookStore.user.dao.dao.UserDao;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.user.vo.UserQueryModel;import cn.hncu.bookStore.util.FileIoUtil;/** * <br/> * Specific implementation class for user data processing---Implementing the UserDao interface* * @author Chen Haoxiang* * @version 1.0 */public class UserDaoSerImpl implements UserDao { private static final String FILE_NAME = "User.txt"; @Override public boolean create(UserModel user) { // 1 First deserialize (read) the existing data List<UserModel> list = FileIoUtil.readFormFile(FILE_NAME); // 2 Determine whether the user already exists, and then decide whether to create for (UserModel userModel : list) { // If the uuids of the two users are equal, the user is the same if (userModel.getUuid().equals(user.getUuid())) { return false;// The user already exists, return false } } // 3 If the user does not exist, create list.add(user); FileIoUtil.write2file(list, FILE_NAME); return true;// Create successfully, return true } @Override public boolean delete(String uuid) { // 1 First deserialize (read) the existing data List<UserModel> list = FileIoUtil.readFormFile(FILE_NAME); // 2 Determine whether the user already exists, and then decide whether to delete// for(int i=0;i<list.size();i++){ // if(list.get(i).getUuid().equals(uuid)){ // list.remove(i); // FileIoUtil.write2file(list, FILE_NAME); // return true; // } // } for (UserModel userModel : list) { // If the uuids of 2 users are equal, the user is the same if (userModel.getUuid().equals(uuid)) { list.remove(userModel); FileIoUtil.write2file(list, FILE_NAME); // Delete successfully, return true return true; } } // 3 The user does not exist// Delete failed, return false return false; } @Override public boolean update(UserModel user) { // 1 Deserialize (read) the existing data first List<UserModel> list = FileIoUtil.readFormFile(FILE_NAME); // 2 Determine whether the user already exists, and then decide whether to create for (int i = 0; i < list.size(); i++) { // uuid cannot be changed. Find the user data through uuid, and modify it will be OK if (list.get(i).getUuid().equals(user.getUuid())) { // Modify the found user to user list.set(i, user); FileIoUtil.write2file(list, FILE_NAME); // Find the user and return true return true; } } // 3 If the user does not exist, the modification fails to return false; } @Override public List<UserModel> getAll() { return FileIoUtil.readFormFile(FILE_NAME); } @Override public List<UserModel> getbyCondition(UserQueryModel uqm) { // TODO Auto-generated method stub return null; } @Override public UserModel getSingle(String uuid) { // 1 Deserialize (read) the existing data first List<UserModel> list = FileIoUtil.readFormFile(FILE_NAME); // 2 Determine whether the user already exists, and if it exists, return the user for (int i = 0; i < list.size(); i++) { if (list.get(i).getUuid().equals(uuid)) { return list.get(i); } } // 3 If the user does not exist, return null return null; }}AddPanel class:
cn.hncu.bookStore.user.ui;
AddPanel class:
/* * AddPanel.java * * Created on __DATE__, __TIME__ */package cn.hncu.bookStore.user.ui;import javax.swing.JFrame;import javax.swing.JOptionPane;import cn.hncu.bookStore.common.UserTypeEnum;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.business.factory.UserEbiFactory;import cn.hncu.bookStore.user.vo.UserModel;import cn.hncu.bookStore.util.FileIoUtil;/** * * @author Chen Haoxiang*/public class AddPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form AddPanel */ public AddPanel(JFrame mainFrame) { this.mainFrame = mainFrame; initComponents(); myInitData(); } private void myInitData() { for (UserTypeEnum type : UserTypeEnum.values()) { combType.addItem(type.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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); tfdName = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); tfdUuid = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); tfdPwd2 = new javax.swing.JPasswordField(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); combType = new javax.swing.JComboBox(); tfdPwd = new javax.swing.JPasswordField(); btnAdd = new javax.swing.JButton(); btnBack = new javax.swing.JButton(); setMinimumSize(new java.awt.Dimension(800, 600)); setLayout(null); jLabel1.setFont(new java.awt.Font("Microsoft Yahei", 1, 48)); jLabel1.setForeground(new java.awt.Color(204, 0, 0)); jLabel1.setText("/u6dfb/u52a0/u7528/u6237"); add(jLabel1); jLabel1.setBounds(270, 30, 230, 80); jLabel2.setFont(new java.awt.Font("Microsoft Yahei", 0, 18)); jLabel2.setText("/u7528/u6237/u7c7b/u578b:"); add(jLabel2); jLabel2.setBounds(40, 310, 90, 30); tfdName.setFont(new java.awt.Font("Dialog", 1, 18)); tfdName.setAutoscrolls(false); add(tfdName); tfdName.setBounds(420, 160, 120, 30); jLabel3.setFont(new java.awt.Font("Microsoft Yahei", 0, 18)); jLabel3.setText("uuid:"); add(jLabel3); jLabel3.setBounds(70, 160, 50, 30); tfdUuid.setFont(new java.awt.Font("Dialog", 0, 11)); add(tfdUuid); tfdUuid.setBounds(140, 160, 110, 30); jLabel4.setFont(new java.awt.Font("Microsoft Yahei", 0, 18)); jLabel4.setText("/u59d3/u540d:"); add(jLabel4); jLabel4.setBounds(360, 160, 50, 30); add(tfdPwd2); tfdPwd2.setBounds(420, 240, 170, 30); jLabel5.setFont(new java.awt.Font("Microsoft Yahei", 0, 18)); jLabel5.setText("/u5bc6/u7801:"); add(jLabel5); jLabel5.setBounds(70, 240, 50, 30); jLabel6.setFont(new java.awt.Font("Microsoft Yahei", 0, 18)); jLabel6.setText("/u786e/u8ba4/u5bc6/u7801:"); add(jLabel6); jLabel6.setBounds(330, 240, 90, 30); combType.setFont(new java.awt.Font("Dialog", 1, 18)); combType.setForeground(new java.awt.Color(51, 51, 255)); combType.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "Please select..." })); add(combType); combType.setBounds(140, 310, 160, 30); tfdPwd.setFont(new java.awt.Font("安一", 1, 18)); add(tfdPwd); tfdPwd.setBounds(140, 240, 160, 30); btnAdd.setFont(new java.awt.Font("Dialog", 1, 24)); btnAdd.setForeground(new java.awt.Color(0, 204, 204)); 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(140, 430, 120, 60); 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(470, 430, 120, 60); }// </editor-fold> //GEN-END:initComponents private void back() { mainFrame.setContentPane(new ListPanel(mainFrame)); mainFrame.validate(); } /** *Listen back button* @param Click listening for the return button*/ private void btnBackActionPerformed(java.awt.event.ActionEvent evt) { back(); } private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { //1 Collect parameters String uuid = tfdUuid.getText(); String name = tfdName.getText(); String pwd = new String(tfdPwd.getPassword()); String pwd2 = new String(tfdPwd2.getPassword()); if (!pwd.equals(pwd2)) { JOptionPane.showMessageDialog(null, "The password inputs are inconsistent the two times, please re-enter! "); return; } int type = 0; try { type = UserTypeEnum.getTypeByName(combType.getSelectedItem() .toString()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Please specify the user type!"); return; } //2 Organize parameters UserModel user = new UserModel(); user.setName(name); user.setPwd(pwd); user.setType(type); user.setUuid(uuid); //3 Call the logic layer UserEbi ebi = UserEbiFactory.getUserEbi(); //4Direct to different pages according to the result returned by the call if (ebi.create(user)) { back(); } else { JOptionPane.showMessageDialog(null, "This user already exists!"); } } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnAdd; private javax.swing.JButton btnBack; private javax.swing.JComboBox combType; 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 jLabel6; private javax.swing.JTextField tfdName; private javax.swing.JPasswordField tfdPwd; private javax.swing.JPasswordField tfdPwd2; private javax.swing.JTextField tfdUuid; // End of variables declaration//GEN-END:variables}ListPanel class:
cn.hncu.bookStore.user.ui;
ListPanel class:
/* * ListPanel.java * * Created on __DATE__, __TIME__ */package cn.hncu.bookStore.user.ui;import java.util.List;import javax.swing.JFrame;import cn.hncu.bookStore.user.business.ebi.UserEbi;import cn.hncu.bookStore.user.business.factory.UserEbiFactory;import cn.hncu.bookStore.user.vo.UserModel;/** * Presentation layer-user list panel* * @author Chen Haoxiang* @version 1.0 */public class ListPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form ListPanel */ public ListPanel(JFrame mainFrame) { this.mainFrame = mainFrame; initComponents(); myInitData(); } /** * Read all users and add them to the list*/ private void myInitData() { UserEbi user = UserEbiFactory.getUserEbi(); List<UserModel> list = user.getAll(); userLists.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() { jScrollPane1 = new javax.swing.JScrollPane(); userLists = new javax.swing.JList(); jLabel1 = new javax.swing.JLabel(); btnToAdd = new javax.swing.JButton(); setMinimumSize(new java.awt.Dimension(800, 600)); setLayout(null); userLists.setModel(new javax.swing.AbstractListModel() { String[] strings = { "" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(userLists); add(jScrollPane1); jScrollPane1.setBounds(150, 150, 480, 230); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 48)); jLabel1.setForeground(new java.awt.Color(204, 0, 51)); jLabel1.setText("User"); jLabel1.setText("User"); jLabel1.setForeground(new java.awt.Color(204, 0, 51)); jLabel1.setText("User"); List"); add(jLabel1); jLabel1.setBounds(270, 30, 260, 80); btnToAdd.setFont(new java.awt.Font("Dialog", 1, 18)); btnToAdd.setText("/u6dfb/u52a0/u7528/u6237"); btnToAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnToAddActionPerformed(evt); } }); add(btnToAdd); btnToAdd.setBounds(60, 420, 150, 50); }// </editor-fold> //GEN-END:initComponents private void btnToAddActionPerformed(java.awt.event.ActionEvent evt) { mainFrame.setContentPane(new AddPanel(mainFrame)); mainFrame.validate(); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton btnToAdd; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JList userLists; // End of variables declaration//GEN-END:variables}UserModel class:
cn.hncu.bookStore.user.vo;
UserModel class:
User value object module:
package cn.hncu.bookStore.user.vo;import java.io.Serializable;import cn.hncu.bookStore.common.UserTypeEnum;/** * @author Chen Haoxiang* @version 1.0 * * <br/> * Value object used to save user information<br/> * 1. Serializable<br/> * 2. Privatize all variable members and supplement setter-getters method<br/> * 3. Write equals and hashCode method---Use the primary key (uuid) unique identification code<br/> * 4. toString method<br/> * 5. Empty parameter construction method<br/> */public class UserModel implements Serializable{ private String uuid;//User unique identification code private String name;//User name private int type;//User type private String pwd;//User password public UserModel() { } /** * Function: Get uuid-user unique identification code* * @return Return uuid-user unique identification code*/ public String getUuid() { return uuid; } /** * Function: Set uuid-user unique identification code* @param uuid-user unique identification code-String type parameter*/ public void setUuid(String uuid) { this.uuid = uuid; } /** * Function: Get the user's username* @return---name-username*/ public String getName() { return name; } /** * Function: Set the user's username* * @param name--username set by the user, String type parameter*/ public void setName(String name) { this.name = name; } /** * Function: Get the user's type: * 1― represents admin, and all operations can be performed* 2― represents the person who can operate the book module* 3― represents the person who can operate the purchase module* 4― represents the person who can operate the sales module* 5― represents the person who can operate the inventory module* @return User's type*/ public int getType() { return type; } /** * Function: Set the user's type: * 1 - denoted as admin, all operations can be performed* 2 - denoted as person who can operate book modules* 3 - denoted as person who can operate purchase modules* 4 - denoted as person who can operate sales modules* 5 - denoted as person who can operate inventory modules* @param type--user's type-int type parameter*/ public void setType(int type) { this.type = type; } /** * Function: Get the user's password* @return String type, user's password*/ public String getPwd() { return pwd; } /** * Function: Set the user's password* @param pwd--String type parameter*/ public void setPwd(String pwd) { this.pwd = pwd; } @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; UserModel other = (UserModel) 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 + "," + UserTypeEnum.getNameByType(type); }}UserQueryModel class:
cn.hncu.bookStore.user.vo;
UserQueryModel class:
Although there is no code, it cannot be ignored! This is what you need when looking for users.
I wrote the reason in the series.
package cn.hncu.bookStore.user.vo;/** * * @author Chen Haoxiang* * @version 1.0 */public class UserQueryModel extends UserModel{}FileIoUtil class:
cn.hncu.bookStore.util;
FileIoUtil class:
package cn.hncu.bookStore.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import javax.swing.JOptionPane;/** * User public data read and write class* @author Chen Haoxiang* * @version 1.0 */public class FileIoUtil { public FileIoUtil() { } /** * Read all data from the database and return it* * @param fileName: (the file name corresponding to the data table) * @return Records of all tables! */ @SuppressWarnings("unchecked")//Press warning public static<E> List<E> readFormFile(String fileName){ List<E> list = new ArrayList<E>(); final File file = new File(fileName); ObjectInputStream in =null; if(!file.exists()){ //JOptionPane.showMessageDialog(null, "The data table does not exist!"); return list; } try { in = new ObjectInputStream(new FileInputStream(fileName)); try { list = (List<E>) in.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(in!=null){ try { in.close(); } catch (IOException e) { throw new RuntimeException("Database close failed"); } } } return list; } /** * Write a list collection into the data file fileName * * @param list (the data collection that needs to be stored) * @param fileName (the file name to which file is written) */ public static<E> void write2file(List<E> list, String fileName){ ObjectOutputStream out = null; try { out = new ObjectOutputStream(new FileOutputStream(fileName)); out.writeObject(list); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(out!=null){ try { out.close(); } catch (IOException e) { throw new RuntimeException("Database close failed!"); } } } } } } } }} BookStore class: (including main method)
cn.hncu.bookStore;
BookStore class:
The main method of the user module is in this class:
/* * BookStore.java * * Created on __DATE__, __TIME__ */package cn.hncu.bookStore;import cn.hncu.bookStore.user.ui.ListPanel;/** * * @author Chen Haoxiang*/public class BookStore extends javax.swing.JFrame { /** Creates new form BookStore */ public BookStore() { initComponents(); this.setContentPane(new ListPanel(this)); this.setResizable(false);//This.setDefaultCloseOperation(EXIT_ON_CLOSE); } /** 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() { menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem(); editMenu = new javax.swing.JMenuItem(); cutMenuItem = new javax.swing.JMenuItem(); cutMenuItem = new javax.swing.JMenuItem(); editMenu = new javax.swing.JMenu(); cutMenuItem = new javax.swing.JMenuItem(); copyMenuItem = new javax.swing.JMenuItem(); pasteMenuItem = new javax.swing.JMenuItem(); deleteMenuItem = new javax.swing.JMenuItem(); helpMenu = new javax.swing.JMenuItem(); contentsMenuItem = new javax.swing.JMenuItem(); aboutMenuItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new java.awt.Dimension(800, 600)); fileMenu.setText("File"); openMenuItem.setText("Open"); fileMenu.add(openMenuItem); saveMenuItem.setText("Save"); fileMenu.add(saveMenuItem); saveAsMenuItem.setText("Save As..."); fileMenu.add(saveAsMenuItem); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); editMenu.setText("Edit"); cutMenuItem.setText("Cut"); editMenu.add(cutMenuItem); copyMenuItem.setText("Copy"); editMenu.add(copyMenuItem); pasteMenuItem.setText("Paste"); editMenu.add(pasteMenuItem); deleteMenuItem.setText("Delete"); editMenu.add(deleteMenuItem); menuBar.add(editMenu); helpMenu.setText("Help"); contentsMenuItem.setText("Contents"); helpMenu.add(contentsMenuItem); aboutMenuItem.setText("About"); helpMenu.add(aboutMenuItem); menuBar.add(helpMenu); setJMenuBar(menuBar); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 279, Short.MAX_VALUE)); pack(); }// </editor-fold> //GEN-END:initComponents private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new BookStore().setVisible(true); } }); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenuItem contentsMenuItem; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem cutMenuItem; private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JMenu helpMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem openMenuItem; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; // End of variables declaration//GEN-END:variables} That’s all for today, to be continued. . .
There is a small bug in the current addition, which is that when adding a user, you don’t enter anything.
Only select the user type, and you can also create it! I'll fix it next time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.