前言
該項目核心需求:
實現簡單登錄
管理員端實現下列功能
①查找書籍
②增加書籍
③刪除書籍
④展示全部書籍
⑤退出系統
通用戶實現下列功能
①查詢書籍
②借閱書籍
③歸還書籍
④退出系統
項目類的設計展示
圖書相關的類
Book:定義書籍的信息 BookList:表示書庫,里面存放書籍
package book;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */public class Book { //定義成員變量 private String name; private String author; private int price; private String type; private boolean isBorrowed = false; //表示書的借閱狀態 public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type;// this.isBorrowed = isBorrowed; } public String getName() { return name; } public String getAuthor() { return author; } public int getPrice() { return price; } public String getType() { return type; } public boolean isBorrowed() { return isBorrowed; } public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } public void setPrice(int price) { this.price = price; } public void setType(String type) { this.type = type; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }// @Override// public String toString() {// return "Book{" // "name='" name ''' // ", author='" author ''' // ", price=" price // ", type='" type ''' // ", isBorrowed=" isBorrowed // '}';// } @Override public String toString() { return "Book{" "name='" name ''' ", author='" author ''' ", price=" price ", type='" type ''' ((isBorrowed == true) ? " 借閱狀態: 已借出" : " 借閱狀態: 未借出") '}'; }}
package book;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */public class BookList { public int usedSize = 3; //書架 public Book[] books = new Book[10]; //書的類型為Book,用順數組book去存儲 public BookList() { books[0] = new Book("三國演義","羅貫中", 100, "小說"); books[1] = new Book("水滸傳", "施耐庵", 100, "小說"); books[2] = new Book("西游記", "吳承恩", 100, "小說"); } //給指定位置放書 public void setBooks(int pos,Book book) { this.books[pos] = book; } //拿到指定位置的書 public Book getBooks(int pos){ return this.books[pos]; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; }}
對書庫(順序表)操作的類
//新增public class AddOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("添加書籍"); Scanner sc = new Scanner(System.in); System.out.println("請輸入書名"); String name = sc.nextLine(); System.out.println("請輸入書的作者"); String author = sc.nextLine(); System.out.println("請輸入書的價格"); int price = sc.nextInt(); System.out.println("請輸入書的類型"); String type = sc.next(); Book newBook = new Book(name,author,price,type); //構建新書(對象) int size = booklist.getUsedSize(); //通過bookList引用訪問當前順序表長度 booklist.setBooks(size,newBook); //將新書放在順序表最后面 booklist.setUsedSize(size 1); //順序表放了新書之后,長度加1 }}//借閱public class BorrowOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("借閱書籍"); Scanner sc = new Scanner(System.in); System.out.println("請輸入書名"); String name = sc.nextLine(); //name為所要借閱書名 for (int i = 0; i < booklist.getUsedSize(); i ) { //通過booklist下標遍歷每一本書 Book book = booklist.getBooks(i); if(book.getName().equals(name)){ //如果為true,說明要借閱的書存在,我們需要做的是修改書的借閱狀態 book.setBorrowed(true); //為true表示書已經結出 return ; } } System.out.println("非常抱歉,本館沒有您要借閱的書!"); // }}//刪除public class DelOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("刪除書籍"); Scanner sc = new Scanner(System.in); System.out.println("請輸入書名"); String name = sc.nextLine(); //name為所要刪除的書名 int i = 0; for (; i < booklist.getUsedSize(); i ) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ break; } } if(i >= booklist.getUsedSize()){ System.out.println("沒有要刪除的這本書!"); return ; } //此時i為所要刪除書的下標 for (int j = i; j < booklist.getUsedSize()-1; j ) { Book book = booklist.getBooks(j 1); //獲得j 1位置的書 booklist.setBooks(j,book); //將j 1位置的書給j位置 } int size = booklist.getUsedSize(); //獲得順序表長度 booklist.setUsedSize(size-1); //刪除書后,長度減去1 System.out.println("書已被刪除!"); }}//展示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.getBooks(i); System.out.println(book); } }}//查找public class FindOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("查找書籍"); Scanner sc = new Scanner(System.in); System.out.println("請輸入書名"); String name = sc.nextLine(); //name為所要查找書名 for (int i = 0; i < booklist.getUsedSize(); i ) { //通過booklist下標遍歷每一本書 Book book = booklist.getBooks(i); if(book.getName().equals(name)){ System.out.println("該書存在!"); System.out.println(book); //直接打印書的信息,toString方法已被重寫 return ; } } System.out.println("沒有這本書!"); }}//歸還public class ReturnOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("歸還書籍"); Scanner sc = new Scanner(System.in); System.out.println("請輸入書名"); String name = sc.nextLine(); //name為所要歸還的書名 for (int i = 0; i < booklist.getUsedSize(); i ) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ book.setBorrowed(false); System.out.println(book); //直接打印書的信息,toString方法已被重寫 return ; } } System.out.println("沒有你要歸還的這本書!"); }}//退出public class ExitOperation implements IOperation{ @Override public void work(BookList booklist) { System.out.println("退出系統"); System.exit(1); //表示退出系統 }}//總接口public interface IOperation { void work(BookList booklist);}
用戶相關類
用戶類
package user;import book.BookList;import operation.IOperation;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:55 */abstract public class User { public String name; public IOperation[] operations; public User(String name){ this.name = name; } /* 新建menu方法,理解為用戶菜單 因為SpecialPerson和OrdinaryPerson繼承了User,所以讓兩個子類重寫menu方法,二者的菜單展示不一致 此時menu可以沒有具體實現,因而將它設計為抽象方法,因此User類成為抽象類 */ abstract public int menu(); //operations中存放的是哪些操作方法,得看子類 public void doOperation(int choice, BookList bookList){ this.operations[choice].work(bookList); }}
管理員
package user;import operation.*;import java.util.Scanner;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:56 * 管理員 */public class SpecialPerson extends User{ //構造方法 public SpecialPerson(String name){ super(name); this.operations = new IOperation[]{ new ExitOperation(), //0退出系統 new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } /* 重寫父類User的menu方法 menu作用:返回你要執行的操作 */ @Override public int menu() { System.out.println("管理員菜單!"); System.out.println("================================="); System.out.println("hello " this.name " 熱誠歡迎使用本校圖書館管理系統!"); 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 sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; }}
普通用戶
package user;import operation.*;import java.util.Scanner;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:57 * 普通用戶 */public class OrdinaryPerson extends User{ //構造方法 public OrdinaryPerson(String name){ super(name); this.operations = new IOperation[]{ new ExitOperation(), //0退出系統 new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } //重寫父類User的menu方法 @Override public int menu() { System.out.println("普通用戶菜單!"); System.out.println("================================="); System.out.println("hello" this.name "熱誠歡迎使用本校圖書館管理系統!"); System.out.println("1.查找圖書"); System.out.println("2.借閱圖書"); System.out.println("3.歸還圖書"); System.out.println("0.退出系統"); System.out.println("================================"); System.out.println("請按照提示選擇相應操作: "); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; }}
主函數程序
import book.BookList;import user.OrdinaryPerson;import user.SpecialPerson;import user.User;import java.util.Scanner;/** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:30 */public class TrqTest { //login(登錄)方法返回用戶實例,類型為User public static User login(){ Scanner sc = new Scanner(System.in); System.out.println("請輸入你的姓名: "); String name = sc.nextLine(); System.out.println("請輸入你的身份> 1:管理員 0:普通用戶"); int number = sc.nextInt(); if(number == 1){ return new SpecialPerson(name); }else{ return new OrdinaryPerson(name); } } public static void main(String[] args) { BookList bookList = new BookList(); //登錄 User user = login(); // 父類引用指向子類實例(向上轉型) while(true) { //user.menu() 調用哪個menu方法,根據登錄身份決定 int choice = user.menu(); //此doOperation方法是當前user自己的方法 //choice選擇當前用戶operations數組中的相應的類,該類產生的對象調用自己的work方法 user.doOperation(choice, bookList); } }}
運行一下看看具體怎么樣吧
感謝你看到這里,文章有什么不足還請指正,覺得文章對你有幫助的話記得給我點個贊,每天都會分享java相關技術文章或行業資訊,歡迎大家關注和轉發文章!
歡迎關注公眾號:前程有光,領取一線大廠Java面試題總結 各知識點學習思維導 一份300頁pdf文檔的Java核心知識點總結!
版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。