学校Java课的课程实验之一。
用到的数据结构:哈希表
题目要求:
1. 建立的图书类包含如下信息:编号、书名、作者、出版社、出版日期。
2. 能够实现根据以下关键字查询图书:编号、书名、作者、出版社。
3. 能够实现图书信息的录入、删除和修改。
4.录入的图书至少要有10本以上。
5.具有图形用户界面.
功能介绍:
录入、删除、修改图书;
按编号、书名、作者、出版社查询图书。
界面设计:
主界面(会的不多,所以用了最简易的流式布局):
检索模块:
从四个检索字段中选择一种,输入检索词,点击“检索”按钮后会在右边的文本框中显示检索到的内容。
录入/修改模块:
在主窗口中点击“录入”,可在弹出的“录入对话框”中输入图书信息,点击“确定”后,录入的信息将显示在状态框里,并且在图书列表中追加了新录入的图书。
删除模块:
在主窗口中点击“删除”,可在弹出的“删除对话框”中输入书名,点击“确定”后,该名字的图书将从图书馆删除。
类的设计:
MyDate类 被Book类引用
Book类 被Library类引用
Library类 被Gui类引用
Gui类 图形界面,引用以上三个类
辅助类MyDate的实现
1 public class MyDate 2 { 3 //data members 4 private int year; 5 private int month; 6 private int day; 7 //methods 8 public MyDate(){this(1995,2,28);} 9 public MyDate(int year, int month, int day) 10 { 11 this.year = year; 12 this.month = month; 13 this.day = day; 14 } 15 public void set_year(int year) 16 { 17 this.year = year; 18 } 19 public void set_month(int month) 20 { 21 this.month = month; 22 } 23 public void set_day(int day) 24 { 25 this.day = day; 26 } 27 28 public void set_date(int year, int month, int day) 29 { 30 this.year = year; 31 this.month = month; 32 this.day = day; 33 } 34 35 public int get_year() 36 { 37 return year; 38 } 39 public int get_month() 40 { 41 return month; 42 } 43 public int get_day() 44 { 45 return day; 46 } 47 48 public String toString() 49 { 50 return (year+"/"+month+"/"+day); 51 } 52 }
MyDate.java
书类Book的实现
1 import java.util.*; 2 public class Book{ 3 static int count=0; 4 private int num; 5 private String title; 6 private Vector<String> author; 7 private String publisher; 8 private MyDate date; 9 10 public int getNum() 11 {return num;} 12 public String getTitle() 13 {return title;} 14 public Vector<String> getAuthor() 15 {return author;} 16 public String getPublisher() 17 {return publisher;} 18 public MyDate getDate() 19 {return date;} 20 21 //can not set num 22 public void setTitle(String title) 23 {this.title=title;} 24 private void setAuthor(Vector<String> author) 25 {this.author=author;} 26 public void setPublisher(String publisher) 27 {this.publisher=publisher;} 28 public void setDate(MyDate date) 29 {this.date=date;} 30 31 public Book() 32 { 33 this("???","???",new MyDate()); 34 count++; 35 this.num=count; 36 } 37 public Book(String title, String publisher, MyDate date) 38 { 39 count++; 40 this.num=count; 41 this.title=title; 42 this.author=new Vector<String>(); 43 this.publisher=publisher; 44 this.date=date; 45 } 46 47 @Override 48 public String toString() 49 { 50 return (num+"\\"+title+"\\"+author+"\\"+publisher+"\\"+date+"\n"); 51 } 52 53 public void addAuthor(String anAuthor) 54 {author.add(anAuthor);} 55 }
Book.java
图书馆类Library.java
1 import java.util.*; 2 public class Library{ 3 private Hashtable<Integer, Book> numList;//num is unique, and the other three are probably not 4 private Hashtable<String, Book> titleList;//regard the books with the same name different 5 private Hashtable<String, Vector<Book>> authorList; 6 private Hashtable<String, Vector<Book>> publisherList; 7 8 public Library() 9 { 10 numList=new Hashtable<Integer, Book>(); 11 titleList=new Hashtable<String, Book>(); 12 authorList=new Hashtable<String, Vector<Book>>(); 13 publisherList=new Hashtable<String, Vector<Book>>(); 14 } 15 16 @Override 17 public String toString() 18 { 19 return ("图书馆现有 "+numList.size()+" 本书"); 20 } 21 22 public void addBook(Book aBook) 23 { 24 numList.put(new Integer(aBook.getNum()),aBook); 25 26 titleList.put(aBook.getTitle(),aBook); 27 28 for(String anAuthor: aBook.getAuthor()) 29 { 30 if(!authorList.containsKey(anAuthor)) 31 authorList.put(anAuthor, new Vector<Book>()); 32 (authorList.get(anAuthor)).add(aBook); 33 } 34 35 if(!publisherList.containsKey(aBook.getPublisher())) 36 publisherList.put(aBook.getPublisher(), new Vector<Book>()); 37 (publisherList.get(aBook.getPublisher())).add(aBook); 38 } 39 40 private void removeBook(Book aBook) 41 {//be called by the same name public method 42 numList.remove(new Integer(aBook.getNum())); 43 44 titleList.remove(aBook.getTitle()); 45 46 (publisherList.get(aBook.getPublisher())).remove(aBook); 47 if((publisherList.get(aBook.getPublisher())).isEmpty()) 48 publisherList.remove(aBook.getPublisher()); 49 50 for(String anAuthor: aBook.getAuthor()) 51 { 52 (authorList.get(aBook.getAuthor())).remove(aBook); 53 if((authorList.get(aBook.getAuthor())).isEmpty()) 54 authorList.remove(aBook.getAuthor()); 55 } 56 } 57 58 public String removeBook(String aTitle) 59 { 60 String result=""; 61 if(titleList.get(aTitle)==null) 62 { 63 result="这本书不存在"; 64 } 65 else 66 { 67 result="您已经删除 "+titleList.get(aTitle).toString(); 68 removeBook(titleList.get(aTitle));//call the function with the same name 69 } 70 return result; 71 } 72 public String modifyBook(String aTitle,String anAuthor,String aPublisher,MyDate aDate) 73 { 74 titleList.get(aTitle).addAuthor(anAuthor); 75 titleList.get(aTitle).setPublisher(aPublisher); 76 titleList.get(aTitle).setDate(aDate); 77 String result="您已经修改 "+titleList.get(aTitle).toString(); 78 return result; 79 } 80 81 82 public String listBooksWithNum(int aNum) 83 { 84 String result=""; 85 Book book=numList.get(aNum); 86 result+=book; 87 return result; 88 } 89 public String listBooksWithTitle(String aTitle) 90 { 91 String result=""; 92 Book book=titleList.get(aTitle); 93 result+=book; 94 return result; 95 } 96 public String listBooksWithTitle() 97 { 98 String result=""; 99 Enumeration<String> titles=titleList.keys(); 100 while(titles.hasMoreElements()) 101 {result+=titles.nextElement();} 102 return result; 103 } 104 public String listBooksWithAuthor(String anAuthor) 105 { 106 String result=""; 107 Iterator<Book> books=(authorList.get(anAuthor)).iterator(); 108 while(books.hasNext()) 109 {result+=books.next();} 110 return result; 111 } 112 public String listBooksWithPublisher(String aPublisher) 113 { 114 String result=""; 115 Vector<Book> books=publisherList.get(aPublisher); 116 for(Book book: books) 117 result+=book; 118 return result; 119 } 120 public String listAllBooks() 121 { 122 return(numList.toString()); 123 } 124 }
图形界面类的实现
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import java.util.*; 5 public class Gui 6 { 7 static Library lib=new Library(); 8 static String result=""; 9 public static void main(String[] args) 10 { 11 Board mybox=new Board(); 12 13 Book aBook=new Book("Java 程序设计","清华大学出版社",new MyDate(1999,2,10)); 14 aBook.addAuthor("郑莉"); 15 lib.addBook(aBook); 16 17 aBook=new Book("C语言程序设计", "清华大学出版社", new MyDate(2005,3,20)); 18 aBook.addAuthor("谭浩强"); 19 lib.addBook(aBook); 20 21 aBook=new Book("计算机原理简明教程", "北京理工大学出版社", new MyDate(2009,5,22)); 22 aBook.addAuthor("王铁峰"); 23 lib.addBook(aBook); 24 25 aBook=new Book("数理语言学", "商务印书馆", new MyDate(2010,5,6)); 26 aBook.addAuthor("冯志伟"); 27 lib.addBook(aBook); 28 29 aBook=new Book("大学英语通用翻译教程", "对外经济贸易大学出版社", new MyDate(2012,11,18)); 30 aBook.addAuthor("余静娴"); 31 lib.addBook(aBook); 32 33 aBook=new Book("考研词汇分频速记", "西安交通大学出版社", new MyDate(2013,6,20)); 34 aBook.addAuthor("俞敏洪"); 35 lib.addBook(aBook); 36 37 aBook=new Book("计算机组成原理", "高等教育出版社", new MyDate(2008,4,20)); 38 aBook.addAuthor("唐朔飞"); 39 lib.addBook(aBook); 40 41 aBook=new Book("马克思主义基本原理概论", "高等教育出版社", new MyDate(2013,3,24)); 42 aBook.addAuthor("本书编写组"); 43 lib.addBook(aBook); 44 45 aBook=new Book("数据结构", "清华大学出版社", new MyDate(2013,8,20)); 46 aBook.addAuthor("邓俊辉"); 47 lib.addBook(aBook); 48 49 aBook=new Book("概率论基础教程", "机械工业出版社", new MyDate(2005,12,26)); 50 aBook.addAuthor("Sheldon Ross"); 51 lib.addBook(aBook); 52 53 54 55 56 result= lib.toString()+"\n"+lib.listAllBooks(); 57 58 mybox.addWindowListener(new WindowAdapter() 59 { 60 public void windowClosing(WindowEvent e) 61 {System.exit(0);} 62 }); 63 } 64 } 65 66 class Board extends JFrame implements ActionListener//主面板 67 { 68 public static final int Width=300; 69 public static final int Height=200; 70 String proList[]={"编号","书名","作者","出版社"}; 71 JTextField keywd; 72 static JTextArea state_text; 73 static JTextArea result_text; 74 static int flg=0;//类变量标记录入/修改 75 JComboBox comboBox; 76 JButton submit,record,delete,change; 77 JLabel keyword,type; 78 public Board() 79 { 80 setSize(Width,Height); 81 setTitle("图书馆"); 82 Container conPane=getContentPane(); 83 conPane.setLayout(new FlowLayout()); 84 85 comboBox=new JComboBox(proList); 86 type=new JLabel("查询类别"); 87 conPane.add(type); 88 conPane.add(comboBox); 89 90 keyword=new JLabel("检索关键字"); 91 keywd=new JTextField(10); 92 conPane.add(keyword); 93 conPane.add(keywd); 94 95 submit=new JButton("检索"); 96 submit.addActionListener(this); 97 conPane.add(submit); 98 99 record=new JButton("录入"); 100 record.addActionListener(this); 101 conPane.add(record); 102 103 delete=new JButton("删除"); 104 delete.addActionListener(this); 105 conPane.add(delete); 106 107 change=new JButton("修改"); 108 change.addActionListener(this); 109 conPane.add(change); 110 111 state_text=new JTextArea(5,20); 112 state_text.setLineWrap(true); 113 conPane.add(state_text); 114 115 result_text=new JTextArea(20,40); 116 result_text.setLineWrap(true); 117 conPane.add(result_text); 118 conPane.setVisible(true); 119 120 this.setVisible(true); 121 this.pack(); 122 } 123 124 public void actionPerformed(ActionEvent e) 125 { 126 if(e.getSource()==submit) 127 { 128 String state="以关键字 "+comboBox.getSelectedItem().toString()+" 查询:"+"\n"; 129 state += "您输入的关键字为: "+keywd.getText(); 130 state_text.setText(state); 131 String search_result=""; 132 search_result+="以下是您要查找的书:"+"\n"; 133 if(comboBox.getSelectedIndex()==0) 134 { 135 search_result+=Gui.lib.listBooksWithNum(Integer.parseInt(keywd.getText())); 136 } 137 else if(comboBox.getSelectedIndex()==1) 138 { 139 search_result+=Gui.lib.listBooksWithTitle(keywd.getText()); 140 } 141 else if(comboBox.getSelectedIndex()==2) 142 { 143 search_result+=Gui.lib.listBooksWithAuthor(keywd.getText()); 144 } 145 else if(comboBox.getSelectedIndex()==3) 146 { 147 search_result+=Gui.lib.listBooksWithPublisher(keywd.getText()); 148 } 149 result_text.setText(search_result); 150 } 151 152 if(e.getSource()==record) 153 { 154 MyDialog recording=new MyDialog(this,"录入"); 155 flg=1; 156 recording.setVisible(true); 157 } 158 else if(e.getSource()==delete) 159 { 160 DeDialog deleting=new DeDialog(this,"删除"); 161 deleting.setVisible(true); 162 } 163 else if(e.getSource()==change) 164 { 165 MyDialog changing=new MyDialog(this,"修改"); 166 flg=2; 167 changing.setVisible(true); 168 } 169 } 170 public static void returnName(String s) 171 { 172 state_text.setText(s); 173 result_text.setText("现在图书馆里有的书: "+"\n"+Gui.result); 174 } 175 } 176 class MyDialog extends JDialog implements ActionListener//录入、修改的对话框 177 { 178 JLabel title,author,publisher,date; 179 JTextField text_title,text_author,text_publisher,text_date_year,text_date_month,text_date_day; 180 JButton done; 181 MyDialog(JFrame F,String s) 182 { 183 super(F,s,true); 184 Container con=this.getContentPane(); 185 con.setLayout(new FlowLayout()); 186 setModal(false); 187 done=new JButton("确定"); 188 done.addActionListener(this); 189 190 title=new JLabel("书名"); 191 text_title=new JTextField(10); 192 text_title.setEditable(true); 193 con.add(title); 194 con.add(text_title); 195 196 author=new JLabel("作者"); 197 text_author=new JTextField(10); 198 text_author.setEditable(true); 199 con.add(author); 200 con.add(text_author); 201 202 publisher=new JLabel("出版社"); 203 text_publisher=new JTextField(10); 204 text_publisher.setEditable(true); 205 con.add(publisher); 206 con.add(text_publisher); 207 208 date=new JLabel("出版日期(年/月/日)"); 209 con.add(date); 210 211 text_date_year=new JTextField(3); 212 text_date_year.setEditable(true); 213 con.add(text_date_year); 214 215 text_date_month=new JTextField(3); 216 text_date_month.setEditable(true); 217 con.add(text_date_month); 218 219 text_date_day=new JTextField(3); 220 text_date_day.setEditable(true); 221 con.add(text_date_day); 222 223 con.add(done); 224 con.setVisible(true); 225 this.pack(); 226 } 227 public void actionPerformed(ActionEvent e) 228 { 229 if(Board.flg==1)//录入 230 { 231 MyDate aDate=new MyDate(Integer.parseInt(text_date_year.getText()), 232 Integer.parseInt(text_date_month.getText()),Integer.parseInt(text_date_day.getText())); 233 Book aBook=new Book(text_title.getText(),text_publisher.getText(), aDate); aBook.addAuthor(text_author.getText()); 234 Gui.lib.addBook(aBook); 235 Gui.result= Gui.lib.toString()+"\n"+Gui.lib.listAllBooks(); 236 Board.returnName("您已添加 "+"\n"+aBook.toString()+" 到图书馆中"); 237 } 238 else if(Board.flg==2)//修改 239 { 240 MyDate aDate=new MyDate(Integer.parseInt(text_date_year.getText()), 241 Integer.parseInt(text_date_month.getText()),Integer.parseInt(text_date_day.getText())); 242 String s=Gui.lib.modifyBook(text_title.getText(),text_author.getText(),text_publisher.getText(),aDate); 243 Gui.result= Gui.lib.toString()+"\n"+Gui.lib.listAllBooks(); 244 Board.returnName(s); 245 } 246 setVisible(true); 247 dispose(); 248 } 249 } 250 class DeDialog extends JDialog implements ActionListener//删除的对话框 251 { 252 JLabel title; 253 static JTextField text_title; 254 JButton done; 255 DeDialog(JFrame F, String s) 256 { 257 super(F,s,true); 258 Container con=this.getContentPane(); 259 con.setLayout(new FlowLayout()); 260 con.setSize(300,500); 261 setModal(false); 262 done=new JButton("确定"); 263 done.addActionListener(this); 264 265 title=new JLabel("书名"); 266 text_title=new JTextField(10); 267 text_title.setEditable(true); 268 con.add(title); 269 con.add(text_title); 270 271 con.add(done); 272 con.setVisible(true); 273 this.pack(); 274 } 275 public void actionPerformed(ActionEvent e) 276 { 277 String s=Gui.lib.removeBook(DeDialog.text_title.getText()); 278 Gui.result= Gui.lib.toString()+"\n"+Gui.lib.listAllBooks(); 279 Board.returnName(s); 280 setVisible(true); 281 dispose(); 282 283 } 284 }
Gui.java
学习过程的参考资料:
Java语言程序设计/郑莉/清华大学出版社
微学苑Java教程 http://www.weixueyuan.net/view/6055.html
时间: 2024-10-21 14:08:01