主要類:

1 //Book類 2 //每一本書為一個對象 3 public class Book 4 { 5 //數據 6 private String bookName ; 7 private String writer ; 8 private String bookRack ; 9 private String introduction ; 10 11 //------------------------------------------------------------------- 12 public Book(String bookName , String writer , String bookRack ,String introduction) 13 { 14 this.bookName = bookName ; 15 this.writer = writer ; 16 this.bookRack = bookRack ; 17 this.introduction = introduction ; 18 } 19 20 //------------------------------------------------------------------- 21 public String display() 22 { 23 String output = "書名:"+bookName+ 24 "\n作者:"+((writer.equals("")==true)?("無內容說明"):(writer))+ 25 "\n書架:"+((bookRack.equals("")==true)?("無內容說明"):(bookRack))+"\n介紹:"+ 26 ((introduction.equals("")==true)?("無內容說明"):(introduction)) ; 27 return output ; 28 } 29 30 //------------------------------------------------------------------- 31 public String getBookName() 32 { 33 return bookName ; 34 } 35 public String getWriterName() 36 { 37 return writer ; 38 } 39 }

1 //BookList類 2 //對象數組類 3 public class BookList 4 { 5 //數據 6 private Book[] book ;//Book對象 7 // private List<Book> book; 8 private int nElems ;//記錄書的數目 9 private int max ;//記錄對象數組的大小 10 private int doubleNum ;//當插入的書本數目超過對象數組的大小時,使用doubleNum使得對象數組的大小翻倍 11 12 //方法--------------------------------------------------------------- 13 public BookList() 14 { 15 max = 10 ; 16 doubleNum = 2 ; 17 book = new Book[max] ; 18 // book = new List<Book>; 19 nElems = 0 ; 20 } 21 //------------------------------------------------------------------- 22 //插入 23 public void insert(String bookName , String writer , String bookRack ,String introduction) 24 { 25 book[nElems] = new Book(bookName,writer,bookRack,introduction); 26 // Book temp = new Book(bookNmae,writer,bookRack,introduction); 27 // book.Add(temp); 28 nElems++ ; 29 if(nElems>=max) 30 { 31 max = max * doubleNum ; 32 Book[] bookTemporary = new Book[max] ;//創建一個臨時對象數組 33 for(int i = 0 ; i<nElems ; i++) 34 { 35 bookTemporary[i] = book[i] ; 36 } 37 book = bookTemporary ; 38 } 39 } 40 //------------------------------------------------------------------- 41 //根據書名查找 42 public String find(String bookName) 43 { 44 int i ; 45 for(i = 0 ; i<nElems ; i++) 46 { 47 if( book[i].getBookName().equals(bookName)) 48 break ; 49 } 50 if(i==nElems) 51 return "沒有此書" ; 52 else 53 return display(book[i]) ; 54 } 55 //------------------------------------------------------------------------ 56 //顯示 57 public String display(Book book) 58 { 59 return book.display(); 60 } 61 //------------------------------------------------------------------------ 62 //根據書名刪除 63 public String delete(String bookName) 64 { 65 int bookIndex = 0 ;//記錄下標 66 int i ; 67 for( i=0 ; i<nElems ; i++)//尋找目標下標 68 { 69 if(bookName.equals(book[i].getBookName())) 70 { 71 bookIndex = i ; 72 break ; 73 } 74 } 75 if(i==nElems) 76 return "該書不存在!" ; 77 else 78 { 79 for(int j=bookIndex ; i<nElems ; i++) 80 book[i] = book[i+1] ; 81 nElems-- ; 82 return "該書已經刪除!" ; 83 } 84 85 } 86 }
窗口類:
測試的窗口類:

1 //整個程序測試的入口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class BookManage extends JFrame 6 { 7 //數據 8 User userFrame ; 9 ManagerEntry managerEntryFrame ; 10 //組件 11 JButton jb1 ; 12 JButton jb2 ; 13 //構造方法 14 public BookManage() 15 { 16 userFrame = new User() ; 17 managerEntryFrame = new ManagerEntry() ; 18 19 jb1 = new JButton("找書") ; 20 jb2 = new JButton("管理員") ; 21 22 //添加組件 23 add(jb1) ; 24 add(jb2) ; 25 26 //添加監聽器 27 jb1.addActionListener(new ActionListener() 28 { 29 public void actionPerformed(ActionEvent e) 30 { 31 userFrame.setTitle("用戶查找窗口") ; 32 userFrame.setLocationRelativeTo(null) ; 33 userFrame.setBounds(100,100,400,400) ; 34 userFrame.setVisible(true) ; 35 } 36 }); 37 38 jb2.addActionListener(new ActionListener() 39 { 40 public void actionPerformed(ActionEvent e) 41 { 42 managerEntryFrame.setLayout(new FlowLayout()) ; 43 managerEntryFrame.setTitle("圖書管理程序") ; 44 managerEntryFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 45 managerEntryFrame.setBounds(700,300,300,120) ; 46 managerEntryFrame.setVisible(true) ; 47 } 48 }); 49 } 50 51 //main方法,程序入口 52 public static void main(String[] agrs) 53 { 54 BookManage bookManageFrame = new BookManage() ; 55 bookManageFrame.setLayout(new GridLayout(2,1,0,5)) ; 56 bookManageFrame.setTitle("圖書管理程序") ; 57 //bookManageFrame.setLocationRelativeTo(null) ; 58 bookManageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 59 //bookManageFrame.setSize(200,100) ; 60 bookManageFrame.setBounds(700,300,200,100) ; 61 bookManageFrame.setVisible(true) ; 62 } 63 }
其他窗口的實現:

1 //user窗口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class User extends JFrame implements BookListInterface 6 { //組件 7 JPanel jp1; 8 JLabel jlb ; 9 JTextField jtf ; 10 JPanel jp2; 11 JTextArea jta ; 12 13 //構造方法 14 public User() 15 { 16 //JButton jbt = new JButton("daf"); 17 // setLayout(new BorderLayout()) ; 18 19 jp1 = new JPanel() ; 20 jlb = new JLabel("書名:") ; 21 jtf = new JTextField(15) ; 22 jp1.add(jlb) ; 23 jp1.add(jtf) ; 24 25 jp2 = new JPanel() ; 26 jp2.setLayout(new BorderLayout()) ; 27 jta = new JTextArea() ; 28 jp2.add(jta) ; 29 30 //添加組件 31 add(jp1,BorderLayout.NORTH) ; 32 add(jp2,BorderLayout.CENTER) ; 33 34 //添加監聽器 35 jtf.addActionListener(new ActionListener() 36 { 37 public void actionPerformed(ActionEvent e) 38 { 39 String bookName = jtf.getText() ; 40 String output = bookList.find(bookName) ; 41 jta.setText(output) ; 42 jtf.setText("") ; 43 } 44 }); 45 } 46 47 //main方法,程序測試 48 public static void main(String[] agrs) 49 { 50 User userFrame = new User() ; 51 // userFrame.setLayout(new BorderLayout()) ; 52 userFrame.setTitle("用戶查找窗口") ; 53 userFrame.setLocationRelativeTo(null) ; 54 // userFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 55 // userFrame.setSize(400,400) ; 56 userFrame.setBounds(100,100,400,400) ; 57 userFrame.setVisible(true) ; 58 } 59 }

1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 public class ManagerEntry extends JFrame 5 { 6 //數據 7 private static String keyWord ;//生命靜態 8 Manager managerFrame ; 9 KeyWordReset keyWordResetFrame ; 10 11 12 JLabel jlb ; 13 JTextField jtf ; 14 JPanel jpl ; 15 JButton jbt ; 16 //方法 17 //-------------------------------------------------- 18 public ManagerEntry() 19 { 20 keyWord = "keen" ; 21 managerFrame = new Manager(); 22 keyWordResetFrame = new KeyWordReset() ; 23 24 jlb = new JLabel("輸入密碼:(keen)") ; 25 jtf = new JTextField(10) ; 26 jpl = new JPanel() ; 27 jbt = new JButton("修改密碼") ; 28 29 jpl.add(jlb) ; 30 jpl.add(jtf) ; 31 32 add(jpl) ; 33 add(jbt) ; 34 35 //添加監聽器 36 jtf.addActionListener(new ActionListener() 37 { 38 public void actionPerformed(ActionEvent e) 39 { 40 if(keyWord.equals(jtf.getText())) 41 { 42 managerFrame.setLayout(new GridLayout(3,1,0,5)) ; 43 managerFrame.setTitle("管理員操作窗口") ; 44 managerFrame.setLocationRelativeTo(null) ; 45 managerFrame.setBounds(250,100,190,190) ; 46 managerFrame.setVisible(true) ; 47 48 jtf.setText("") ; 49 setVisible(false) ; 50 } 51 else 52 { 53 jtf.setText("密碼有誤") ; 54 } 55 } 56 }); 57 58 jbt.addActionListener(new ActionListener() 59 { 60 public void actionPerformed(ActionEvent e) 61 { 62 keyWordResetFrame.setLayout(new FlowLayout()) ; 63 keyWordResetFrame.setTitle("密碼修改") ; 64 keyWordResetFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 65 keyWordResetFrame.setBounds(700,300,310,130) ; 66 keyWordResetFrame.setVisible(true) ; 67 } 68 }); 69 } 70 //------------------------------------------------------------------- 71 public static void setKey(String key) 72 { 73 keyWord = key ; 74 } 75 //------------------------------------------------------------------- 76 //main方法,程序入口 77 public static void main(String[] agrs) 78 { 79 ManagerEntry managerEntryFrame = new ManagerEntry() ; 80 managerEntryFrame.setLayout(new FlowLayout()) ; 81 managerEntryFrame.setTitle("圖書管理程序") ; 82 managerEntryFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 83 managerEntryFrame.setBounds(700,300,300,120) ; 84 managerEntryFrame.setVisible(true) ; 85 } 86 }

1 import javax.swing.*; 2 import java.awt.event.*; 3 import java.awt.*; 4 public class KeyWordReset extends JFrame 5 { 6 //數據 7 private String keyWord ; 8 9 JLabel jlb1 ; 10 JTextField jtf1 ; 11 JLabel jlb2 ; 12 JTextField jtf2 ; 13 JPanel jpl ; 14 JButton jbt ; 15 //方法 16 //-------------------------------------------------- 17 public KeyWordReset() 18 { 19 // managerEntryFrame = new ManagerEntry() ; 20 jlb1 = new JLabel("輸入新密碼:") ; 21 jlb2 = new JLabel("確認新密碼:") ; 22 jtf1 = new JTextField(10) ; 23 jtf2 = new JTextField(10) ; 24 jpl = new JPanel(new GridLayout(2,2,0,5)) ; 25 jbt = new JButton("確定修改") ; 26 27 jpl.add(jlb1) ; 28 jpl.add(jtf1) ; 29 jpl.add(jlb2) ; 30 jpl.add(jtf2) ; 31 32 add(jpl) ; 33 add(jbt) ; 34 35 //添加監聽器 36 jbt.addActionListener(new ActionListener() 37 { 38 public void actionPerformed(ActionEvent e) 39 { 40 String key1 = jtf1.getText() ; 41 String key2 = jtf2.getText() ; 42 if(key1.equals(key2)) 43 { 44 ManagerEntry.setKey(key1) ; 45 jtf1.setText("") ; 46 jtf2.setText("") ; 47 setVisible(false); 48 } 49 else 50 { 51 JOptionPane.showMessageDialog(null,"兩次的密碼不相同!") ; 52 jtf1.setText("") ; 53 jtf2.setText("") ; 54 } 55 } 56 }); 57 } 58 //------------------------------------------------------------------- 59 public void setKey(String keyWord) 60 { 61 this.keyWord = keyWord ; 62 } 63 //------------------------------------------------------------------- 64 //main方法,程序入口 65 public static void main(String[] agrs) 66 { 67 KeyWordReset keyWordResetFrame = new KeyWordReset(); 68 keyWordResetFrame.setLayout(new FlowLayout()) ; 69 keyWordResetFrame.setTitle("密碼修改") ; 70 keyWordResetFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 71 keyWordResetFrame.setBounds(700,300,310,130) ; 72 keyWordResetFrame.setVisible(true) ; 73 } 74 }

1 //manager窗口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class Manager extends JFrame 6 { 7 //數據 8 JButton insetButton ; 9 JButton findButton ; 10 JButton deleteButton ; 11 12 InsertPanel insertPanelFrame ; 13 FindPanel findPanelFrame ; 14 DeletePanel deletePanelFrame ; 15 16 //方法 17 public Manager() 18 { 19 insetButton = new JButton("Insert") ; 20 findButton = new JButton("Find") ; 21 deleteButton = new JButton("Delete") ; 22 23 insertPanelFrame = new InsertPanel() ; 24 findPanelFrame = new FindPanel() ; 25 deletePanelFrame = new DeletePanel() ; 26 27 add(insetButton) ; 28 add(findButton) ; 29 add(deleteButton) ; 30 31 //添加監聽器 32 insetButton.addActionListener(new ActionListener() 33 { 34 public void actionPerformed(ActionEvent e) 35 { 36 insertPanelFrame.setTitle("插入窗口") ; 37 insertPanelFrame.setLocationRelativeTo(null) ; 38 insertPanelFrame.setBounds(600,100,200,400) ; 39 insertPanelFrame.setVisible(true) ; 40 } 41 }); 42 43 findButton.addActionListener(new ActionListener() 44 { 45 public void actionPerformed(ActionEvent e) 46 { 47 findPanelFrame.setTitle("用戶查找窗口") ; 48 findPanelFrame.setLocationRelativeTo(null) ; 49 findPanelFrame.setBounds(100,100,400,400) ; 50 findPanelFrame.setVisible(true) ; 51 } 52 }); 53 54 deleteButton.addActionListener(new ActionListener() 55 { 56 public void actionPerformed(ActionEvent e) 57 { 58 deletePanelFrame.setTitle("刪除窗口") ; 59 deletePanelFrame.setLocationRelativeTo(null) ; 60 deletePanelFrame.setBounds(100,100,400,400) ; 61 deletePanelFrame.setVisible(true) ; 62 } 63 }); 64 } 65 66 67 //main方法,程序測試 68 public static void main(String[] agrs) 69 { 70 Manager managerFrame = new Manager() ; 71 managerFrame.setLayout(new GridLayout(3,1,0,5)) ; 72 managerFrame.setTitle("管理員操作窗口") ; 73 managerFrame.setLocationRelativeTo(null) ; 74 // userFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 75 // userFrame.setSize(400,400) ; 76 managerFrame.setBounds(250,100,190,190) ; 77 managerFrame.setVisible(true) ; 78 } 79 }

1 //insert窗口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class InsertPanel extends JFrame implements BookListInterface 6 { 7 //數據 8 private String bookName ; 9 private String writer ; 10 private String bookRack ; 11 private String introduction ; 12 //組件 13 JPanel jp1 ; 14 JPanel jp2 ; 15 JLabel jlb1 ; 16 JLabel jlb2 ; 17 JLabel jlb3 ; 18 JLabel jlb4 ; 19 JTextField jtf1 ; 20 JTextField jtf2 ; 21 JTextField jtf3 ; 22 JTextArea jta ; 23 JButton jbt ; 24 //方法 25 public InsertPanel() 26 { 27 jp1 = new JPanel(); 28 // jp2 = new JPanel(new BorderLayout()); 29 jp2 = new JPanel(); 30 jlb1 = new JLabel("書名:") ; 31 jlb2 = new JLabel("作者:") ; 32 jlb3 = new JLabel("書架:") ; 33 jlb4 = new JLabel("介紹:") ; 34 jtf1 = new JTextField(15) ; 35 jtf2 = new JTextField(15) ; 36 jtf3 = new JTextField(15) ; 37 jta = new JTextArea(15,15) ; 38 jbt = new JButton("提交") ; 39 40 jp1.add(jlb1) ; 41 jp1.add(jtf1) ; 42 jp1.add(jlb2) ; 43 jp1.add(jtf2) ; 44 jp1.add(jlb3) ; 45 jp1.add(jtf3) ; 46 jp1.add(jlb4) ; 47 jp1.add(jta) ; 48 49 jp2.add(jbt); 50 51 add(jp1,BorderLayout.CENTER) ; 52 add(jp2,BorderLayout.SOUTH) ; 53 54 jbt.addActionListener(new ActionListener() 55 { 56 public void actionPerformed(ActionEvent e) 57 { 58 bookName = jtf1.getText() ; 59 writer = jtf2.getText() ; 60 bookRack = jtf3.getText() ; 61 introduction= jta.getText() ; 62 63 // System.out.println(bookName+" "+writer+" "+bookRack+" "+introduction); 64 65 bookList.insert(bookName,writer,bookRack,introduction) ; 66 67 jtf1.setText("") ; 68 jtf2.setText("") ; 69 jtf3.setText("") ; 70 jta.setText("") ; 71 } 72 }); 73 } 74 75 76 //main方法,程序測試 77 public static void main(String[] agrs) 78 { 79 InsertPanel insertPanelFrame = new InsertPanel() ; 80 insertPanelFrame.setTitle("插入窗口") ; 81 insertPanelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 82 insertPanelFrame.setBounds(100,100,200,400) ; 83 insertPanelFrame.setVisible(true) ; 84 } 85 }

1 //findPanel窗口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class FindPanel extends JFrame implements BookListInterface 6 { //組件 7 JPanel jp1; 8 JLabel jlb ; 9 JTextField jtf ; 10 JPanel jp2; 11 JTextArea jta ; 12 13 //構造方法 14 public FindPanel() 15 { 16 //JButton jbt = new JButton("daf"); 17 // setLayout(new BorderLayout()) ; 18 19 jp1 = new JPanel() ; 20 jlb = new JLabel("書名:") ; 21 jtf = new JTextField(15) ; 22 jp1.add(jlb) ; 23 jp1.add(jtf) ; 24 25 jp2 = new JPanel() ; 26 jp2.setLayout(new BorderLayout()) ; 27 jta = new JTextArea() ; 28 jp2.add(jta) ; 29 30 //添加組件 31 add(jp1,BorderLayout.NORTH) ; 32 add(jp2,BorderLayout.CENTER) ; 33 34 //添加監聽器 35 jtf.addActionListener(new ActionListener() 36 { 37 public void actionPerformed(ActionEvent e) 38 { 39 String bookName = jtf.getText() ; 40 String output = bookList.find(bookName) ; 41 jta.setText(output) ; 42 jtf.setText("") ; 43 } 44 }); 45 46 47 } 48 49 //main方法,程序測試 50 public static void main(String[] agrs) 51 { 52 FindPanel findPanelFrame = new FindPanel() ; 53 // userFrame.setLayout(new BorderLayout()) ; 54 findPanelFrame.setTitle("用戶查找窗口") ; 55 findPanelFrame.setLocationRelativeTo(null) ; 56 // userFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 57 // userFrame.setSize(400,400) ; 58 findPanelFrame.setBounds(100,100,400,400) ; 59 findPanelFrame.setVisible(true) ; 60 } 61 }

1 //deletePanel窗口 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.awt.*; 5 public class DeletePanel extends JFrame implements BookListInterface 6 { //組件 7 JPanel jp1; 8 JLabel jlb ; 9 JTextField jtf ; 10 JPanel jp2; 11 JTextArea jta ; 12 13 //構造方法 14 public DeletePanel() 15 { 16 jp1 = new JPanel() ; 17 jlb = new JLabel("書名:") ; 18 jtf = new JTextField(15) ; 19 jp1.add(jlb) ; 20 jp1.add(jtf) ; 21 22 jp2 = new JPanel() ; 23 jp2.setLayout(new BorderLayout()) ; 24 jta = new JTextArea() ; 25 jp2.add(jta) ; 26 27 //添加組件 28 add(jp1,BorderLayout.NORTH) ; 29 add(jp2,BorderLayout.CENTER) ; 30 31 //添加監聽器 32 jtf.addActionListener(new ActionListener() 33 { 34 public void actionPerformed(ActionEvent e) 35 { 36 String bookName = jtf.getText() ; 37 String fintResult = bookList.find(bookName) ; 38 if(fintResult.equals("沒有此書" )) 39 jta.setText(fintResult) ; 40 else 41 jta.setText(bookList.delete(bookName)) ; 42 jtf.setText("") ; 43 } 44 }); 45 } 46 47 //main方法,程序測試 48 public static void main(String[] agrs) 49 { 50 DeletePanel deletePanelFrame = new DeletePanel() ; 51 // userFrame.setLayout(new BorderLayout()) ; 52 deletePanelFrame.setTitle("刪除窗口") ; 53 deletePanelFrame.setLocationRelativeTo(null) ; 54 // userFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 55 // userFrame.setSize(400,400) ; 56 deletePanelFrame.setBounds(100,100,400,400) ; 57 deletePanelFrame.setVisible(true) ; 58 } 59 }
接口:(不過這個接口不怎么好)

1 //接口 2 interface BookListInterface 3 { 4 5 BookList bookList = new BookList(); 6 }
說明文檔:
分析現象:A登陸圖書圖書管理程序,根據書名尋找書籍。B作為該程序的管理員,可以直接修改圖書管理程序里中的數據,可以插入、刪除里面的數據。
對象:人、書、電腦
分析數據和方法:
- 數據:書名bookName、作者writer、書架bookrack、介紹introduction
- 方法:插入insert()、查找find()、刪除delete()
包裝:把數據和方法包裝起來,分為兩個類,一個是書類,一個書組類
- 方法實現:根據書本的方法,合理變動這次實驗的方法的實現,比如:插入insert()、查找find()、刪除delete()的方法其實都差不多,只不過有些情況就特別考慮
測試: 為了讓界面好看,並且也是為了提升自己的能力,自己就做了一個圖像化界面來進行測試
收尾:測試成功后,修改部分代碼的格式,使得代碼方便閱讀
以下是Book類和BookList類的UML圖:
Book |
-bookName:String 書名 -writer:String 作者 -bookRack:String 書架 -introduction:String 簡介
|
+Book(String, String, String ,String) 構造方法 +display():String 返回書的詳細信息 +getBookName():String 返回書名 +getWriterName():String 返回作者名
|
BookList |
- book: Book[] Book類對象數組 -nElems:int 記錄書的數目 -max:int 記錄對象數組的大小 -doubleNum:int 插入的書本數目超過對象數組的大小時,使用 doubleNum使得對象數組的大小翻倍
|
+ BookList() 構造方法 insert(String , String , String ,String ):void 插入 +find(String):String 根據書查找 +display(Book):String 顯示 +delete(String):String 根據書名刪除
|