一、功能介紹
此圖書管理系統借助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); } }三、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);
}
}
2. AdminUserpackage 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 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 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 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 operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
2. AddOperation增加圖書: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); } }3. BorrowOperation借閱圖書:
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("没有你要借阅的书! "); } }4. DelOperation#刪除圖書:
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); } }5. DisplayOperation顯示圖書:
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); } } }6. ExitOperation退出圖書:
package operations; import book.BookList; public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统! "); System.exit(0); } }7. FindOperation找書:
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("没有这本书! "); } }8. ReturnOperation歸書:
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中文網其他相關文章!

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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