主要类:
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 根据书名删除 |