下面代码主要是写了一个小型的图书馆管理系统。基本功能就是增删改查。
系统虽然小,但是分层,综合应用上有很强的实际意义,作为菜鸟的我,应当好好研究下,从中吸取经验。
1 import java.util.List; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.util.Iterator; 5 6 /* 7 1. 编号 8 2. 书名 9 3. 借出状态 10 */ 11 class Book { 12 13 private int id ; 14 private String name ; 15 private String flag ; 16 17 public void setId(int id){ 18 this.id = id ; 19 } 20 public int getId(){ 21 return this.id; 22 } 23 24 public void setName(String name){ 25 this.name = name ; 26 } 27 public String getName(){ 28 return this.name; 29 } 30 31 public void setFlag(String flag){ 32 this.flag = flag ; 33 } 34 public String getFlag(){ 35 return this.flag; 36 } 37 } 38 39 class Manager{ 40 private List<Book> list ; 41 42 final static int maxCount = 100; 43 44 public List<Book> getList(){ 45 return initList(); 46 } 47 48 private List<Book> initList() { 49 50 this.list = new ArrayList<Book>(); 51 Book book1 = new Book(); 52 book1.setId(1); 53 book1.setName("水浒传"); 54 book1.setFlag("可借"); 55 56 list.add(book1); 57 58 Book book2 = new Book(); 59 book2.setId(2); 60 book2.setName("三国演义"); 61 book2.setFlag("可借"); 62 63 list.add(book2); 64 65 Book book3 = new Book(); 66 book3.setId(3); 67 book3.setName("西游记"); 68 book3.setFlag("已借出"); 69 70 list.add(book3); 71 72 return this.list; 73 } 74 75 /* 76 添加 77 */ 78 public void addBookToList(Book book) { 79 if(this.list.size() <= maxCount) { 80 this.list.add(book); 81 } 82 } 83 84 /* 85 删除 86 */ 87 public boolean delBookFromList(Book book) { 88 boolean flag = false ; 89 Iterator<Book> it = this.list.iterator(); 90 while(it.hasNext()) { 91 Book has_book = it.next(); 92 if(has_book.getName.equals(book.getName())){ 93 it.remove(); 94 flag = true; 95 } 96 } 97 return flag; 98 } 99 100 public boolean borrowBook(Book book){ 101 boolean flag = false; 102 Iterator<Book> it = this.list.iterator(); 103 while(it.hasNext()) { 104 Book has_book = it.next(); 105 if(has_book.getName.equals(book.getName()){ 106 if("可借".equals(has_book.getFlag())){ 107 flag = true ; 108 } 109 } 110 } 111 return flag ; 112 } 113 114 } 115 116 public class BookManager { 117 118 public static void main(String args[]){ 119 List<Book> bookList = new ArrayList<Book>(); 120 Manager m = new Manager(); 121 122 bookList = m.getList(); 123 124 Iterator<Book> it = bookList.iterator(); 125 while(it.hasNext()){ 126 Book book = it.next(); 127 System.out.println( 128 " 编号:" + book.getId() + 129 " 书名:" + book.getName() + 130 " 状态:" + book.getFlag() 131 ); 132 } 133 134 Book boo1 = new Book(); 135 boo1.setId(4); 136 boo1.setName("大话西游"); 137 boo1.setFlag("可借"); 138 139 m.addBookToList(boo1); 140 141 Scanner scanner = new Scanner(System.in); 142 String name = scanner.next(); 143 144 Book removeBook = new Book(); 145 removeBook.setName(name); 146 147 boolean flag = m.delBookFromList(removeBook); 148 149 Book b_Book = new Book(); 150 b_Book.setName(scanner.next()); 151 boolean borrowFlag = m.borrowBook(b_Book); 152 } 153 154 }