<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
此圖書管理系統藉助IDEA開發工具實現
圖書館系統一共有兩種身份的存取:
1.管理員身份:
2.普通使用者身份:
我們一共有三個包分別是book,operations,user實現.
main函數主要進行大致流程的進行,圖書庫的初始化,圖書管理系統的登入,以及具體操作的選擇,即實施.
import book.BookList; import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { public static void main(String[] args) { //1.先初始化圖書庫,以及初始化: BookList bookList = new BookList(); //2.登入 User user = login();//向上轉型,User接受管理員或者使用者物件 //3.列印選單,進行具體操作 while(true) { int choice = user.menu(); user.doOperation(choice,bookList); } } }
登入功能:
public static User login() { System.out.println("請輸入你的姓名: "); Scanner scanner = new Scanner(System.in); String userName = scanner.nextLine(); System.out.println("請輸入你的身份: 1-> 管理員 2-> 使用者"); int choice = scanner.nextInt(); if(choice == 1) { return new AdminUser(userName); }else { return new NormalUser(userName); } }
因為有兩套系統,管理員和普通使用者,所以我們將共同屬性提取出來一個User抽象類,以達到程式碼複用
package user; import book.BookList; import operations.IOperation; public abstract class User { protected String name; IOperation[] iOperations; public User(String name) { this.name = name; } public abstract int menu(); public void doOperation(int choice, BookList bookList) { iOperations[choice].work(bookList); } }
package user; import operations.*; import java.util.Scanner; public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } public int menu() { System.out.println("歡迎: "+name+"來到圖書館"); System.out.println("**********************************"); System.out.println("1. 查詢圖書"); System.out.println("2. 新增圖書"); System.out.println("3. 刪除圖書"); System.out.println("4. 顯示圖書"); System.out.println("0. 退出圖書"); System.out.println("**********************************"); System.out.println("請輸入你的操作: "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
package user; import operations.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } public int menu() { System.out.println("歡迎: "+name+"來到圖書館"); System.out.println("**********************************"); System.out.println("1. 查詢圖書"); System.out.println("2. 借閱圖書"); System.out.println("3. 歸還圖書"); System.out.println("0. 退出圖書"); System.out.println("**********************************"); System.out.println("請輸入你的操作: "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
我們對book的屬性進行書寫,以及在BookList種對圖書庫的書進行初始化.
package book; public class Book { private String name;//書名 private String author;//作者 private int price;//價格 private String type;//書的型別 private boolean isBorrowed;//書預設未借出 public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + ''' + ", author='" + author + ''' + ", price=" + price + ", type='" + type + ''' + ","+ ((isBorrowed == true) ? "該書已借出" : "該書未借出" )+ '}'; } }
package book; public class BookList { public Book[] books = new Book[100]; public int usedSize;//用來存當前共有多少本書 /** * 事先通過程式碼塊 * * 事先存進去三本書 */ { books[0] = new Book("java","高斯林",95,"IT"); books[1] = new Book("C++","姚琳",93,"IT"); books[2] = new Book("python","馬瑟斯",80,"IT"); this.usedSize = 3; } public Book getPos(int pos) { //獲取某一位置的書 return books[pos]; } public void setBooks(Book book,int pos) { //儲存一本書 到指定位置 books[pos] = book; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } }
我們的圖書管理系統有很多具體的操作,為了後面方便多型,以及檢驗錯誤,所以我們實現一個具體的IOperation介面,每一個具體的操作去實現這個介面.
package operations; import book.BookList; public interface IOperation { void work(BookList bookList); }
增加圖書:
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("新增圖書! "); System.out.println("請輸入要新增的圖書的名字: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("請輸入要新增的圖書的作者: "); String author = scanner.nextLine(); System.out.println("請輸入要新增的圖書的價格: "); int price = scanner.nextInt(); System.out.println("請輸入要新增的圖書的型別: "); String type = scanner.nextLine(); Book book = new Book(name,author,price,type); //1.獲取當前書存放的位置 int curSize = bookList.getUsedSize(); //2.把書放在指定位置 bookList.setBooks(book,curSize); //3.更新書的個數 bookList.setUsedSize(curSize+1); } }
借閱圖書:
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("借閱圖書! "); System.out.println("請輸入要借閱的圖書的名字: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getPos(i); if(name.equals(book.getName())) { if(book.isBorrowed()) { System.out.println("該書已經被借出! "); }else { book.setBorrowed(true); System.out.println("借閱圖書成功! "); return; } } } System.out.println("沒有你要借閱的書! "); } }
刪除圖書:
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("刪除圖書! "); System.out.println("請輸入要刪除的圖書: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); //查詢圖書是否有此圖書,記錄下標 int index = -1; for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getPos(i); if(name.equals(book.getName())) { index = i; break; } } if(index == -1) { System.out.println("沒有 "+name+"這本書!"); return; } for (int i = index; i < bookList.getUsedSize()-1; i++) { Book book = bookList.getPos(i+1); bookList.setBooks(book,i); } //刪除的書,要置空 bookList.setBooks(null, bookList.getUsedSize()-1); bookList.setUsedSize(bookList.getUsedSize()-1); } }
顯示圖書:
package operations; import book.Book; import book.BookList; public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("顯示圖書! "); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getPos(i); System.out.println(book); } } }
退出圖書:
package operations; import book.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系統! "); System.exit(0); } }
查詢圖書:
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { //查詢圖書 System.out.println("查詢圖書! "); System.out.println("請輸入要查詢的圖書: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getPos(i); if(name.equals(book.getName())) { System.out.println("找到了! "); System.out.println(book); return; } } System.out.println("沒有這本書! "); } }
歸還圖書:
package operations; import book.Book; import book.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("歸還圖書! "); System.out.println("請輸入要歸還的圖書的名字: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); for (int i = 0; i < bookList.getUsedSize(); i++) { Book book = bookList.getPos(i); if(name.equals(book.getName())) { book.setBorrowed(false); System.out.println("歸還圖書成功! "); return; } } System.out.println("沒有你要歸還的書! "); } }
到此這篇關於Java實現圖書管理系統的範例程式碼的文章就介紹到這了,更多相關Java圖書管理系統內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45