JAVA實現圖書管理系統(初階)

一.抽象出對象:

1.要有書架,圖書,用戶(包括普通用戶,管理員用戶)。根據這些我們可以建立幾個包,來把繁雜的代碼分開,再通過一個類來把這些,對象整合起來實現系統。說到整合,肯定缺不了,相關接口,我們再定義一個,放接口,和擴展這個接口的方法。

如圖:

二.構思:

1.先在書架類上,初始化好默認書籍,其他構造方法(如:getBook,setBook(在具體的下標,放書和返回書)),具體,在寫實現接口的方法時,來增加。

public class BookList {//組合的方式,初始化書架private Book[] books = new Book[10];private int usedSize;//實際放的,書的個數//初始化書架(放書)public BookList() {this.books[0] = new Book("三國演義", "羅貫中", 12, "小說");this.books[1] = new Book("紅樓夢", "曹雪芹", 13, "小說");this.books[2] = new Book("西游記", "吳承恩", 14, "小說");this.usedSize = 3;}//返回一本,pos(要找的書)下標的書public Book getBook(int pos) {return books[pos];}//插入一本書的方法(相當于,要初始化好,書架原來已有的書)public void setBook(int pos, Book books) {this.books[pos] = books;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book[] getBooks() {return books;}public void setBooks(Book[] books) {this.books = books;}
}

2.在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;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +"," +((isBorrowed == true) ? "已借出" : "未借出") +/*" isBorrowed=" + isBorrowed*/'}';}
}

3.在User類中,定義好name和,相關構造方法,以及接口命名的數組,為后面,到達調用,擴展了接口的類,里的方法,做鋪墊。

public abstract class User {protected String name;//定義,接口命名,類型的數組,后續配合,// 【return new AdminUser(name);】就可以看出,再加上接口調用的方法,就知道,操作了哪一個方法protected IOperation[] iOperations;//要根據子類,來初始化,父類成員變量public User(String name) {this.name = name;}public abstract int menu();//這里封裝一個方法,提供給,Main調用。public void DoIOperation(int choice, BookList bookList) {//這里,iOperations數組,里有我們要的對象,通過,數組里的對象,調用接口里的方法iOperations[choice].work(bookList);}
}

4.管理員類中(AdminUser)和普通用戶類中(NormalUser)繼承了user類,初始化好系統菜單,相關構造方法。(這個構造方法很關鍵,用接口作為數組相當于實例化了,擴展了接口的類,的方法,達到調用系統具體方法的作用?

public class NormalUser extends User{public NormalUser(String name) {super(name);//通過【return new AdminUser(name);】,再加上實現接口的方法,就知道,操作了哪一個方法//登錄界面,選擇了哪個,角色(NormalUser)或者(AdminUser),this就是哪個的引用this.iOperations = new IOperation[] {//這些對象都實現了,iOperations接口,所以不會報錯//下面相當于實例化了,擴展了接口的類,的方法,達到調用系統具體方法的作用new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation(),};}public int menu() {System.out.println("歡迎" + this.name + "使用圖書系統");System.out.println("********普通用戶菜單********");System.out.println("1. 查找圖書");System.out.println("2. 借閱圖書");System.out.println("3. 歸還圖書");System.out.println("0. 退出系統");System.out.println("*************************");Scanner scanner = new Scanner(System.in);System.out.println("請輸入你的操作:");int choice = scanner.nextInt();return choice;}}

5.在Main類中寫好,登錄界面,及整合一下,如何實例化對象,來操作系統。

 public static void main(String[] args) {//實例化書架BookList bookList = new BookList();//通過返回值,向上轉型,確定用戶//這里的user是,返回的(AdminUser),或者(NormalUser)User user = login();while (true) {//然后,通過返回信息,調用恰當的菜單int choice = user.menu();//發生了,動態綁定/*** 根據choice,返回值看看,調用了哪個方法** 1.哪個對象?* 答:User user = login();** 2.哪個方法?-》進一步,還要確定,當前對象,包含了這些方法*答:在構造方法【return new AdminUser(name)】運行時,會初始化好,對應的操作對象。** 注意:后面通過父類對象,調用方法,(int choice = user.menu();),通過choice判斷* 調用了,哪個方法 ,接下來就要對,父類,進行操作*/user.DoIOperation(choice, bookList);}}}

6.初始化好,接口和,菜單里操作系統的work方法(實現了這個接口的,類就是,每個操作系統的方法)

public interface IOperation {//這個接口,有操作書架的方法,在其他類實現,就可以,操作性的區分,不同用戶的方法public void work(BookList bookList);
}

7.接下來就是實現了,接口的每一個類每個操作系統的方法

以下是管理員菜單方法:

(1).查找圖書:

public class FindOperation implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("查找圖書");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("請輸入你要查找的圖書");String name = scanner.nextLine();for (int i = 0; i < currentSize; i++) {//遍歷,書架,已初始化的書Book book = bookList.getBook(i);if (book.getName().equals(name)) {System.out.println("找到了");System.out.println(book);return;}}System.out.println("沒有你要找的書...");}
}

(2).新增圖書:

public class AddOperation implements IOperation{@Overridepublic void work(BookList bookList) {//1.判斷書架(數組)是否滿了int currentSize = bookList.getUsedSize();if (currentSize == bookList.getBooks().length) {System.out.println("該書架滿了,不能放了");return;}//2.構建對象Scanner scanner = new Scanner(System.in);System.out.println("請輸入新增的書名");String name = scanner.nextLine();System.out.println("請輸入新增的作者");String author = scanner.nextLine();System.out.println("請輸入新增的價格");int price = scanner.nextInt();System.out.println("請輸入新增的類型");String type = scanner.next();Book newBook = new Book(name, author, price, type);//3.判斷書架是否,已經存在這本書for (int i = 0; i < currentSize; i++) {//遍歷,書架,已初始化的書Book book = bookList.getBook(i);if (book.getName().equals(name)) {System.out.println("書已經存在了,不用再添加了");return;}}//插入圖書bookList.setBook(currentSize, newBook);bookList.setUsedSize(currentSize+1);}
}

(3).刪除圖書:

public class DelOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("刪除圖書");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("請輸入你要刪除的圖書");String name = scanner.nextLine();int pos = 0;int i = 0;for (; i < currentSize; i++) {//遍歷,書架,已初始化的書Book book = bookList.getBook(i);if (book.getName().equals(name)) {//找到要刪除的,位置pos = i;break;}}if (i == currentSize) {System.out.println("沒有找到你要刪除的圖書");}//開始刪除for (int j = pos; j < currentSize-1; j++) {//思路:bookList[j] = bookList[j+1];//先找到j+1,那個位置,然后覆蓋Book book = bookList.getBook(j+1);bookList.setBook(j, book);}//更新下標bookList.setUsedSize(currentSize-1);System.out.println("刪除成功");}
}

(4).顯示圖書:

public class ShowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("顯示圖書");int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {//遍歷下標,把找到的圖書打印出來Book book = bookList.getBook(i);System.out.println(book);}}}

(5).退出系統:

public class ExitOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系統");System.exit(0);}
}

以下是普通用戶菜單方法:

(1).退出系統和查找圖書,是普通人員和管理員的共同方法

(2)歸還圖書:

public class ReturnOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("歸還圖書");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("請輸入你要歸還的圖書");String name = scanner.nextLine();for (int i = 0; i < currentSize; i++) {//遍歷,書架,已初始化的書Book book = bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),還是false(未借出)if (book.isBorrowed()) {book.setBorrowed(false);return;}}}System.out.println("錯誤,沒有你要歸還的圖書");}
}

(3)借閱圖書:

public class BorrowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("借閱圖書");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("請輸入你要借閱的圖書");String name = scanner.nextLine();for (int i = 0; i < currentSize; i++) {//遍歷,書架,已初始化的書Book book = bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),還是false(未借出)if (book.isBorrowed()) {System.out.println("該書已經被借出");return;}book.setBorrowed(true);//置為借出System.out.println("借閱成功");return;}}System.out.println("沒有找到你要借閱的那本書");}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/16543.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/16543.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/16543.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[數組查找]2.圖解二分查找及其代碼實現

二分查找 二分查找也是一種在數組中查找數據的算法。和線性查找不同&#xff0c;它只能查找已經排好序的數據。二分查找通過比較數組中間的數據與目標數據的大小&#xff0c;可以得知目標數據是在數組的左邊還是右邊。因此&#xff0c;比較一次就可以把查找范圍縮小一半。重復執…

嵌入式進階——舵機控制PWM

&#x1f3ac; 秋野醬&#xff1a;《個人主頁》 &#x1f525; 個人專欄:《Java專欄》《Python專欄》 ??心若有所向往,何懼道阻且長 文章目錄 舵機信號線代碼示例初始化PWM初始化UART打印日志初始化外部中斷Extimain函數 舵機最早用于船舶上實現轉向功能,由于可以通過程序連…

MySQL中, 自增主鍵和UUID作為主鍵有什么區別?

首先我們來看看, 存儲自增主鍵和uuid的數據類型 我們知道, mysql中作為主鍵的通常是int類型的數據, 這個 數據從第一條記錄開始, 從1開始主鍵往后遞增, 例如我有100條數據, 那么根據主鍵排序后, 里面的記錄從上往下一次就是1, 2, 3 ... 100, 但是UUID就不一樣了, UUID是根據特殊…

打卡信奧刷題(21)用Scratch圖形化工具信奧P7071 [CSP-J2020] 優秀的拆分

使用2進制進行拆分是比較好的解決方案&#xff0c;畢竟對于大家來說二進制轉換是非常熟的&#xff0c;如果不會可以參考打卡信奧刷題&#xff08;19&#xff09;用Scratch圖形化工具信奧B3972 [語言月賽 202405] 二進制 題解 &#xff0c;輸出的時候再轉換一下輸出&#xff0c;…

M功能-支付平臺(三)

target&#xff1a;離開柬埔寨倒計時-221day 前言 今天周六&#xff0c;但是在柬埔寨還是工作日&#xff0c;想著國內的朋友開始休周末就羨慕呀&#xff0c;記不清在這邊過了多少個周六了&#xff0c;多到我已經習慣了。而且今天技術部還停電了&#xff0c;真的是熱的受不了呀…

c++11:智能指針的種類以及使用場景

指針管理困境 內存釋放&#xff0c;指針沒有置空&#xff1b;內存泄漏&#xff1b;資源重復釋放 怎樣解決&#xff1f; RAII 智能指針種類 shared_ptr 實現原理&#xff1a;多個指針指向同一資源&#xff0c;引用計數清零&#xff0c;再調用析構函數釋放內存。 使用場景…

ASP.NET 代碼審計

ASP.NET 官方文檔 名詞解釋 IIS&#xff08;Internet Information Services&#xff09; IIS 是微軟開發的一款 Web 服務器軟件&#xff0c;用于在 Windows 服務器上托管和提供Web應用程序和服務。它支持 HTTP、HTTPS、FTP、SMTP 等多種協議&#xff0c;主要用于&#xff1a…

基于混合Transformer-CNN模型的多分辨率學習方法的解剖學標志檢測

文章目錄 Anatomical Landmark Detection Using a Multiresolution Learning Approach with a Hybrid Transformer-CNN Model摘要方法實驗結果 Anatomical Landmark Detection Using a Multiresolution Learning Approach with a Hybrid Transformer-CNN Model 摘要 精確定位…

跨域計算芯片,一把被忽視的汽車降本尖刀

作者 |王博 編輯 |德新 2019年前后&#xff0c;「中央運算單元區域控制」的架構被提出。基于這一趨勢&#xff0c;從板級的多芯片&#xff0c;到板級的單芯片&#xff0c;集成度越來越高&#xff0c;跨域計算芯片隨之來到聚光燈下。 跨域計算芯片的特點是&#xff0c;與專為智…

Django 里傳參給html文件

第一步&#xff1a;在 urls.py 文件里修改 from django.contrib import admin from django.urls import path from app01 import views # 添加這一行urlpatterns [#path(admin/, admin.site.urls),path(index/, views.index), # 添加這一行 ]第二步&#xff1a;在 settings…

若依框架的配置文件詳解:從數據庫配置到高級定制

若依框架&#xff08;RuoYi&#xff09;作為一個基于Spring Boot和MyBatis的快速開發平臺&#xff0c;提供了豐富的配置選項&#xff0c;讓開發者能夠靈活地調整和擴展其功能。配置文件在若依框架中扮演著至關重要的角色&#xff0c;通過合理配置&#xff0c;可以實現對數據庫連…

牛客網刷題 | BC97 回文對稱數

目前主要分為三個專欄&#xff0c;后續還會添加&#xff1a; 專欄如下&#xff1a; C語言刷題解析 C語言系列文章 我的成長經歷 感謝閱讀&#xff01; 初來乍到&#xff0c;如有錯誤請指出&#xff0c;感謝&#xff01; 描述 今天牛牛學到了回文…

鎖相環的一些學習筆記--(1)

下圖兩組1.2.3可以對應起來&#xff1b; 一些分析&#xff1a; 1.根據這個可知最后vco_voltage停在0.5v 參考資料&#xff1a; 1. Matlab https://www.bilibili.com/video/BV1bR4y1Z7Xg/?spm_id_from333.1296.top_right_bar_window_history.content.click&vd_source555…

Redis RDB 持久化問題

前言 Redis 是內存數據庫&#xff0c;它將自己的數據儲存在內存里面&#xff0c;如果不想辦法將儲存在內存中的數據保存到磁盤里面&#xff0c;那么一旦服務器進程退出&#xff0c;服務器中的數據也就沒了。 因此&#xff0c;Redis 提供了 RDB 持久化功能&#xff0c;這個功能…

如何將Windows PC變成Wi-Fi熱點?這里提供詳細步驟

序言 Windows 10和Windows 11都有內置功能,可以將你的筆記本電腦(或臺式機)變成無線熱點,允許其他設備連接到它并共享你的互聯網連接。以下是操作指南。 由于Windows中隱藏的虛擬Wi-Fi適配器功能,你甚至可以在連接到另一個Wi-Fi網絡或無線路由器時創建Wi-Fi熱點,通過另…

魯教版七年級數學上冊-筆記

文章目錄 第一章 三角形1 認識三角形2 圖形的全等3 探索三角形全等的條件4 三角形的尺規作圖5 利用三角形全等測距離 第二章 軸對稱1 軸對稱現象2 探索軸對稱的性質4 利用軸對稱進行設計 第三章 勾股定理1 探索勾股定理2 一定是直角三角形嗎3 勾股定理的應用舉例 第四章 實數1 …

實習生在Linux環境下如何日常使用?

那我簡單來說兩個我使用的場景吧 我在搭建我們的測試環境的時候&#xff0c;先上傳jar包到測試環境對應的目錄下&#xff0c;然后呢此時jar包是不可被執行的&#xff0c;所有就有了 chmod x jar包名稱, 接下來&#xff0c;我是用 jps 查看Java的進程&#xff0c;獲取到pid之后…

Kafka 安裝教程和基本操作

一、簡介 Kafka 是最初由 Linkedin 公司開發&#xff0c;是一個分布式、分區的、多副本的、多訂閱者&#xff0c;基于 zookeeper 協調的分布式日志系統&#xff08;也可以當做 MQ 系統&#xff09;&#xff0c;常見可以用于 web/nginx 日志、訪問日志&#xff0c;消息服務等等…

基于YOLO算法實現網球運動實時分析(附源碼)

大家好&#xff0c;我是小F&#xff5e; 今天給大家介紹一個計算機視覺實戰的項目。 該項目使用YOLO算法檢測球員和網球&#xff0c;并利用cnn提取球場關鍵點。 進而分析視頻中的網球運動員&#xff0c;測量他們的速度、擊球速度和擊球次數。 使用win10電腦&#xff0c;Python …

【源碼】java + uniapp交易所源代碼/帶搭建教程java交易所/完整源代碼

java uniapp交易所源代碼/帶搭建教程java交易所/完整源代碼 帶簡潔教程&#xff0c;未測 java uniapp交易所源代碼/帶搭建教程java交易所/完整源代碼 - 吾愛資源網