java實(shí)現(xiàn)圖書管理系統(tǒng)
本文通過實(shí)例為大家分享了java實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、背景介紹
通過一段時(shí)間java編程的學(xué)習(xí),需要一個(gè)比較綜合的實(shí)例來進(jìn)行編程的練習(xí),是一個(gè)對(duì)前段時(shí)間所學(xué)內(nèi)容進(jìn)行總合提升的一個(gè)契機(jī)。選擇了圖書管理系統(tǒng),會(huì)用到的javaSE知識(shí)有:變量、包、繼承、類、接口、循環(huán)結(jié)構(gòu)等。是一個(gè)很綜合的典例。
二、核心需求
1.用戶可以登錄到系統(tǒng)上 分為 管理員、普通用戶兩種角色,這兩種不同的角色根據(jù)自己的身份可以實(shí)現(xiàn)不同的操作。
普通用戶
a)查閱某個(gè)書籍的信息
b)借閱書籍
c) 歸還書籍
d)退出程序
管理員
a)查閱某個(gè)書籍的信息
b)增加書籍
c) 刪除書籍
d)查看書籍列表
e)退出程序
程序框架結(jié)構(gòu)圖

三、代碼以及詳解
1.User類
package booksystem;
import booksystem.operation.IOperation;
abstract public class User {
protected String name;//定義書名
protected IOperation[] operations;//定義一個(gè)接口數(shù)組
public abstract int menu();//是用戶和管理員的父類,不進(jìn)行實(shí)例化,所以定義為抽象方法
public void doOperation(int choice,BookList bookList){
IOperation operation=this.operations[choice-1];
operation.work(bookList);
}
}
User類是NormalUser類和Admin類的父類,由于不需要實(shí)例化,將menu()函數(shù)定義為了抽象函數(shù)。
2.NormalUser類
package booksystem;
import booksystem.operation.*;
import booksystem.operation.IOperation;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
this.name = name;
this.operations = new IOperation[]
{
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation(),
};
}
@Override
public int menu(){
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("Hello"+name+"Welcome to use booksyetem");
System.out.println("1.查閱書籍信息");
System.out.println("2.借閱書籍");
System.out.println("3.歸還書籍");
System.out.println("4.退出系統(tǒng)");
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("請(qǐng)輸入您的選擇:");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;//返回一個(gè)整型數(shù)
}
}
NormalUser類針對(duì)與普通用戶而編寫,在代碼中定義了一個(gè)接口數(shù)組,在其中添加了普通用戶需要用到的查閱、借閱、歸還、退出系統(tǒng)的四大功能,在menu()函數(shù)中也按照同樣的順序,menu()函數(shù)是重寫父類的,所以為了提醒編譯器,在函數(shù)頭前加上了 @Override進(jìn)行提示。
3.Admin類
package booksystem;
import booksystem.operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
this.name=name;
this.operations=new IOperation[]{
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
new ExitOperation(),
};
}
@Override
public int menu(){
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("Hello"+name+"Welcome to use booksyetem");
System.out.println("1.查閱書籍信息");
System.out.println("2.新增書籍信息");
System.out.println("3.刪除書籍信息");
System.out.println("4.退出系統(tǒng)");
System.out.println("~~~~~~~~~~~~~~~~~~");
System.out.println("請(qǐng)輸入您的選擇:");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
Admin類編寫的思路和NormalUser類的思路相同,區(qū)別在于用戶界面的不同,對(duì)應(yīng)要使用的功能也不同,分別是查閱、新增、刪除和退出系統(tǒng)。
4.Book類
package booksystem;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed = false;
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name=" + name + '\'' + ",author" + author + '\'' + ",price=" + price + ",type='"
+ '\'' + type + '\'' + ",isBorrow=" + isBorrowed + '}';
}
public String getName() {
return name;
}
public boolean isBorrowed(){
return isBorrowed;
}
public void setBorrowed(boolean borrowed){
isBorrowed=borrowed;
}
}
Book類針對(duì)書籍,定義了有關(guān)書的屬性,作者、價(jià)格、名字、類別,重寫了toString函數(shù),以及對(duì)于書的幾個(gè)常用的操作功能函數(shù),getName,以及判讀是否借出和定義書籍借出狀態(tài)的函數(shù)。
5.BookList類
package booksystem;
public class BookList {
private Book[] books=new Book[100];//定義一book數(shù)組
private int size=0;
public BookList(){
books[0]=new Book("計(jì)算機(jī)網(wǎng)絡(luò)教程","郝文源",125,"專業(yè)書籍");
books[1]=new Book("盜墓筆記","南派三叔",150,"網(wǎng)絡(luò)小說");
books[2]=new Book("三體","劉慈欣",178,"科幻小說");
size = 3;
}//給book數(shù)組中初始化一些書
public Book getBook(int index){
return books[index];
}
public void setBook(int index,Book book)
{
books[index]=book;
}
public int getSize(){
return size;
}
public void setSize(int size){
this.size=size;
}
}
BookList類中定義了一個(gè)book數(shù)組,并給數(shù)組中初始化了一些書,定義了常用的功能函數(shù)
6.Main類
package booksystem;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Object o=null;
BookList booklist= new BookList();
User user=login();//上轉(zhuǎn)型,這里先調(diào)用了login()函數(shù),返回一個(gè)Admin對(duì)象或NormalUser對(duì)象
while(true){
int choice=user.menu();
user.doOperation(choice,booklist);
}//在進(jìn)行退出系統(tǒng)的功能時(shí),會(huì)一直進(jìn)行循環(huán),menu()函數(shù)最終會(huì)返回一個(gè)整型數(shù),對(duì)應(yīng)選擇操作中的一項(xiàng)
}
public static User login() {
System.out.println("請(qǐng)輸入您的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("請(qǐng)輸入您的角色:1 管理員 0 普通用戶");//根據(jù)不同的選擇創(chuàng)建對(duì)應(yīng)的對(duì)象
int who = scanner.nextInt();
if (who == 1) {
return new Admin(name);
}
return new NormalUser(name);
}
}
主函數(shù)中主要實(shí)現(xiàn)了login()函數(shù),根據(jù)登錄系統(tǒng)用戶的選擇,決定不同的身份,返回兩種對(duì)象中的一種,在while循環(huán)中,只要不進(jìn)行exit功能,循環(huán)便會(huì)一直執(zhí)行。
7.IOperation
package booksystem.operation;
import booksystem.BookList;
public interface IOperation {
void work(BookList bookList);
}
9.AddOperation
package booksystem.operation;
import booksystem.Book;
import booksystem.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList){
Scanner scanner=new Scanner(System.in);
System.out.println("新增書籍");
System.out.println("請(qǐng)輸入新書籍的名稱");
String name=scanner.next();
System.out.println("請(qǐng)輸入新書籍的作者");
String author=scanner.next();
System.out.println("請(qǐng)輸入新書籍的價(jià)格");
double price=scanner.nextDouble();
System.out.println("請(qǐng)輸入新書籍的類別");
String type=scanner.next();
Book newBook=new Book(name,author,price,type);
int curSize=bookList.getSize();
bookList.setBook(curSize,newBook);
bookList.setSize(curSize+1);
System.out.println("新增書籍成功");
}
}
10.BorrowOperation
package booksystem.operation;
import booksystem.Book;
import booksystem.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList)
{
Scanner scanner=new Scanner(System.in);
System.out.println("借閱書籍");
System.out.println("請(qǐng)輸入要借閱的書籍的名稱");
String name=scanner.next();
int i=0;
for(;i<bookList.getSize();i++)
{
if(bookList.getBook(i).getName().equals(name)){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("未找到指定的書籍,無法借閱!");
return;
}
Book currentBook=bookList.getBook(i);
if(currentBook.isBorrowed()){
System.out.println("該書籍已經(jīng)被借出!");
return;
}
bookList.getBook(i).setBorrowed(true);
System.out.println("借書成功!");
}
}
11.DelOperation
package booksystem.operation;
import booksystem.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList)
{
Scanner scanner=new Scanner(System.in);
System.out.println("刪除書籍");
System.out.println("請(qǐng)輸入要?jiǎng)h除的書籍的名稱");
String name=scanner.next();
int i=0;
for(;i<bookList.getSize();i++)
{
if(bookList.getBook(i).getName().equals(name)){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("您輸入的書籍在+"+name+"在系統(tǒng)中沒有找到!刪除失??!");
return;
}
if(i==bookList.getSize()-1)
{
bookList.setSize(bookList.getSize()-1);
System.out.println("刪除成功!");
return;
}
bookList.setBook(i,bookList.getBook(bookList.getSize()-1));
bookList.setSize(bookList.getSize()-1);
System.out.println("刪除成功!");
}
}
13.ExitOperation
package booksystem.operation;
import booksystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList)
{
System.out.println("退出程序");
System.exit(0);
}
}
14.FindOperation
package booksystem.operation;
import booksystem.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList)
{
System.out.println("退出程序");
System.exit(0);
}
}
15.ReturnOperation
package booksystem.operation;
import booksystem.BookList;
import booksystem.Book;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList){
Scanner scanner=new Scanner(System.in);
System.out.println("歸還書籍");
System.out.println("請(qǐng)輸入要還的書籍的名稱");
String name=scanner.next();
int i=0;
for(;i<bookList.getSize();i++)
{
Book book=bookList.getBook(i);
if(book.getName().equals(i))
{
break;
}
}
if(i>=bookList.getSize())
{
System.out.println("書籍沒有找到,無法歸還");
return;
}
Book currentBook=bookList.getBook(i);
if(!currentBook.isBorrowed())
{
System.out.println("這本書沒有借出,無法歸還");
}
currentBook.setBorrowed(false);
System.out.println("歸還書籍成功");
return;
}
}
四、編程截圖及測試圖

包和類放置圖

運(yùn)行截圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖書管理系統(tǒng)java版
- 一個(gè)簡陋的java圖書管理系統(tǒng)
- 圖書管理系統(tǒng)java代碼實(shí)現(xiàn)
- JAVA初級(jí)項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
- java實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
- JAVA實(shí)現(xiàn)圖書管理系統(tǒng)項(xiàng)目
- Java圖書管理系統(tǒng)課程設(shè)計(jì)
- java實(shí)現(xiàn)簡單圖書管理系統(tǒng)
- 配置idea將Java與數(shù)據(jù)庫連接起來實(shí)現(xiàn)一個(gè)簡單的圖書管理系統(tǒng)
相關(guān)文章
Spring Boot中快速操作Mongodb數(shù)據(jù)庫指南
這篇文章主要給大家介紹了關(guān)于Spring Boot中如何快速操作Mongodb的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
淺談spring-boot的單元測試中,@Before不被執(zhí)行的原因
這篇文章主要介紹了淺談spring-boot的單元測試中,@Before不被執(zhí)行的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
spring cloud openfeign 源碼實(shí)例解析
這篇文章主要介紹了spring cloud openfeign 源碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
springboot中的pom文件?project報(bào)錯(cuò)問題
這篇文章主要介紹了springboot中的pom文件?project報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring?Boot?內(nèi)置工具類ReflectionUtils的實(shí)現(xiàn)
ReflectionUtils是一個(gè)反射工具類,它封裝了Java反射的操作,使得我們能夠更輕松地操作和訪問類的方法、字段,本文主要介紹了Spring?Boot?內(nèi)置工具類ReflectionUtils的實(shí)現(xiàn),感興趣的可以了解一下2023-11-11
spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟
在Java中,@Scheduled注解是用于指定定時(shí)任務(wù)的執(zhí)行規(guī)則的,這篇文章給大家介紹spring boot定時(shí)器實(shí)現(xiàn)定時(shí)同步數(shù)據(jù)的操作步驟,感興趣的朋友一起看看吧2023-12-12

