怎麼用Java程式碼實現圖書管理系統
一、功能介紹
此圖書管理系統借助IDEA開發工具實現
圖書館系統一共有兩種身分的存取:
1.管理員身分:
2.一般使用者身分:
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); } }
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);
}
}
登入後複製
2. AdminUserpackage 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;
}
}
登入後複製
3. NormalUserpackage 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包我們對book的屬性進行書寫,以及在BookList種對圖書庫的書進行初始化.1. Bookpackage 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; } }
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) ? "该书已借出" : "该书未借出" )+
'}';
}
}
登入後複製
2. BookListpackage 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;
}
}
登入後複製
五、operations包我們的圖書管理系統有很多具體的操作,為了後面方便多態,以及檢驗錯誤,所以我們實作一個具體的IOperation接口,每一個具體的操作去實現這個接口.1. IOperation接口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; } }
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
登入後複製
2. AddOperation增加圖書: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程式碼實現圖書管理系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

Java是熱門程式語言,適合初學者和經驗豐富的開發者學習。本教學從基礎概念出發,逐步深入解說進階主題。安裝Java開發工具包後,可透過建立簡單的「Hello,World!」程式來實踐程式設計。理解程式碼後,使用命令提示字元編譯並執行程序,控制台上將輸出「Hello,World!」。學習Java開啟了程式設計之旅,隨著掌握程度加深,可創建更複雜的應用程式。
