java圖書管理系統實現
阿新 • • 發佈:2018-11-07
題目:編寫java程式碼,實現對圖書的管理,包括增刪改查、借閱及歸還。
實現程式碼如下:
import java.util.Scanner; public class BookTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BookManager book = new BookManager(); boolean flag=true; while (flag) { //介面 System.out.println("------------"); System.out.println("歡迎來到圖書管理系統"); System.out.println("0 借閱排行榜"); System.out.println("1 增加圖書"); System.out.println("2 檢視圖書"); System.out.println("3 刪除圖書"); System.out.println("4 借閱圖書"); System.out.println("5 歸還圖書"); System.out.println("6 退出"); System.out.println("------------"); System.out.println("請選擇:"); int choice = sc.nextInt(); // try/catch try { book.Manage(choice); } catch (Exception e) { System.out.println(e.getMessage()); } } } }
import java.util.LinkedHashMap; import java.util.Scanner; import java.util.Set; public class BookManager{ LinkedHashMap<String, Book> stu = new LinkedHashMap<String, Book>(); Book book = new Book(); Scanner sc = new Scanner(System.in); public void Manage(int choice) throws Exception{ switch (choice) { case 0: lists(); break; case 1: add(); break; case 2: find(); break; case 3: delete(); break; case 4: borrow(); break; case 5: back(); break; case 6: System.out.println("您已退出該系統"); break; default: { throw new Exception("沒有該功能,請重新選擇!"); } } } //排行榜 public void lists() { Set<String> set=stu.keySet(); for(String key:set) { if(stu.get(key).getNum()<stu.get(key+1).getNum()) { System.out.println(stu.get(key)); } } } //新增方法 public void add() { System.out.println("請輸入所要新增的圖書編號:"); String key = sc.next(); System.out.println("請輸入所要新增的圖書名稱:"); String name = sc.next(); System.out.println("請輸入所要新增的圖書碼 :"); String id = sc.next(); System.out.println("請輸入所要新增的圖書價格"); double price = sc.nextDouble(); System.out.println("請輸入所要新增的圖書數目:"); int num = sc.nextInt(); stu.put(key, new Book(name,id,price,num)); System.out.println("新增完成!"); find(); } //檢視方法 public void find() { System.out.println("所有圖書如下:"); Set<String> set=stu.keySet(); for(String key:set) { System.out.print(stu.get(key)); if(stu.get(key).getNum()==0) { System.out.println("不可借"); }else if(stu.get(key).getNum()>0) { System.out.println("可借"); } } } //刪除方法\ public void delete() { System.out.println("請輸入刪除的圖書名稱:"); String key2 = sc.next(); Set<String> set=stu.keySet(); for(String key:set) { if(stu.get(key).getName().equals(key2)) { stu.remove(key); System.out.println("刪除成功"); }else { System.out.println("該圖書不存在"); } } } //借閱方法 public void borrow() { System.out.println("請輸入您所要借閱的圖書名稱:"); String names = sc.next(); Set<String> set=stu.keySet(); for(String key:set) { if(stu.get(key).getName().equals(names)) { if(stu.get(key).getNum()>0) { int num=stu.get(key).getNum()-1; stu.get(key).setNum(num); System.out.println("借閱成功"); }else { System.out.println("該書庫存不足"); } }else { System.out.println("該圖書不存在"); } } } //歸還方法 public void back() { System.out.println("請輸入所要歸還的圖書名稱:"); String names = sc.next(); Set<String> set=stu.keySet(); for(String key:set) { if(stu.get(key).getName().equals(names)) { int num=stu.get(key).getNum()+1; stu.get(key).setNum(num); System.out.println("歸還成功"); }else { System.out.println("該書不在本庫中"); } } } }
public class Book { private String id; private String name; private double price; private int num; public Book() { } public Book(String name, String id, double price, int num) { this.name = name; this.id = id; this.price = price; this.num = num; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return this.name + "\t" + this.id + "\t" + this.price + "\t" + this.num; } }
總結:此次的借閱排行榜並未真正實現,接下來會補充