Java开发实现图书管理系统

news/2025/1/15 19:57:47/

本文用Java代码实现图书代码管理系统,有些地方可能会有纰漏,希望各位大佬鉴赏!!

文章目录

  • 文章目录

    一、Java实现图书管理系统

    1.1创建book包

    二、创建图书管理系统的操作包

    2.1创建Operation接口

    三、创建User包

    3.1创建User类

    四、主函数的实现


一、Java实现图书管理系统

1.1创建book包

首先在book中要创建Book类和BookList类,进行封装图书,在Book类中要有书名,作者,价格,类型和是否被借出图书。在BookList类中是书架对图书的封装,进行图书初始化和对图书设置的各种方法。

Book类的实现

package book;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 13:48*/
public class Book {private String name;//书名private String author;//作者private int price;//价格private String type;//类型private boolean isBorrowed;//是否被借出public Book(){}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=" + isBorrowed +(isBorrowed == true ?  "已经被借出 " :  "未被借出 ")+'}';}}

BookList类的实现

package book;/*** @author xyk的电脑* @version 1.0* @description: 书架* @date 2023/1/24 13:51*/
public class BookList {private static final int DEFAULT_SIZE = 10;private Book[] books = new Book[DEFAULT_SIZE];private int usedSize;//记录当前books数组中 有多少本书public BookList() {books[0] = new Book("三国演义","罗贯中",89,"小说");books[1] = new Book("西游记","吴承恩",78,"小说");books[2] = new Book("红楼梦","曹雪芹",49,"小说");this.usedSize = 3;}public Book getBook(int pos){return this.books[pos];}public void setBook(int pos,Book book){this.books[pos] = book;}public void setBook(Book book){this.books[usedSize] = book;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}
}

二、创建图书管理系统的操作包

2.1创建Operation接口

创建Operation接口来实现对图书的相关具体操作,其中管理员和普通用户的功能不同,需要都来实现Operation接口。

 IOPeration接口的实现

package opera;import book.BookList;public interface IOPeration {void work(BookList bookList);}

AddOperation类的实现

package opera;import book.Book;
import book.BookList;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:01*/
public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {Scanner scanner = new Scanner(System.in);System.out.println("请输入书名:");String name = scanner.nextLine();System.out.println("请输入作者:");String author = scanner.nextLine();System.out.println("请输入类型:");String type = scanner.nextLine();System.out.println("请输入价格:");int price = scanner.nextInt();Book book = new Book(name,author,price,type);int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book tmp = bookList.getBook(i);if (tmp.getName().equals(name)) {System.out.println("已经存在这本书了");return;}}bookList.setBook(book);//修改usedSizebookList.setUsedSize(currentSize+1);}
}

BrrowOperation类的实现

package opera;import book.Book;
import book.BookList;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:05*/
public class BrrowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("输入你要借阅的图书");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name) &&!book.isBorrowed()){book.setBorrowed(true);System.out.println("借阅成功");return;}}}
}

DelOperation类的实现

package opera;import book.Book;
import book.BookList;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:03*/
public class DelOpertaion implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("删除图书");System.out.println("请输入你要删除的图书名字");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedSize();int index = -1;for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)){index = i;break;}}//挪动数据for (int j = index; j < currentSize-1; j++) {Book book = bookList.getBook(j+1);bookList.setBook(j,book);}//修改sizebookList.setUsedSize(currentSize-1);//最后置为nullbookList.setBook(currentSize-1,null);}
}

ExitOperation类的实现

package opera;import book.BookList;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:04*/
public class ExitOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);}
}

FindOperation类的实现

package opera;import book.Book;
import book.BookList;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:01*/
public class FindOperation implements IOPeration {@Overridepublic void work(BookList bookList) {System.out.println("请输入书名: ");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedSize();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("没有这本书!");}
}

ReturnOperation类的实现

package opera;import book.Book;
import book.BookList;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:05*/
public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("输入你要归还的图书");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name) &&book.isBorrowed()){book.setBorrowed(false);System.out.println("归还成功");return;}}}
}

ShowOperation类的实现

package opera;import book.Book;
import book.BookList;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 14:04*/
public class ShowOperation implements IOPeration{@Overridepublic void work(BookList bookList) {int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);System.out.println(book);}}
}

三、创建User包

3.1创建User类

创建抽象类User来实现区别管理员和用户的主要功能。

package user;import book.Book;
import book.BookList;
import opera.IOPeration;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 13:57*/
public abstract class User {protected String name;protected IOPeration[] ioPerations;public User(String name){this.name = name;}public abstract int menu();public void doWork(int choice, BookList bookList){this.ioPerations[choice].work(bookList);}
}

NormalUser类的实现

package user;import opera.*;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 13:58*/
public class NormalUser extends User{public NormalUser(String name) {super(name);this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new BrrowOperation(),new ReturnOperation()};}public int menu(){System.out.println("******************************");System.out.println("hello "+name+ "欢迎来到图书小练习");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;}
}

AdminUser类的实现

package user;import opera.*;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 13:57*/
public class AdminUser extends User{public AdminUser(String name) {super(name);this.ioPerations = new IOPeration[]{new ExitOperation(),new FindOperation(),new AddOperation(),new DelOpertaion(),new ShowOperation()};}public int menu(){System.out.println("******************************");System.out.println("hello "+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 scanner = new Scanner(System.in);int choice = scanner.nextInt();return choice;}
}

四、主函数的实现

在主函数中来进行图书管理系统的操作。

import book.Book;
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;import java.util.Scanner;/*** @author xyk的电脑* @version 1.0* @description: TODO* @date 2023/1/24 13:44*/
public class Main {public static User login(){System.out.println("请输入你的姓名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("请输入你的身份:1->管理员  0->普通用户");int choice = scanner.nextInt();if (choice == 1){return new AdminUser(name);}else {return new NormalUser(name);}}public static void main(String[] args) {BookList bookList = new BookList();User user = login();while (true) {int choice = user.menu();//根据choice和user来确定调用哪个对象的操作user.doWork(choice, bookList);}}
}

http://www.ppmy.cn/news/20278.html

相关文章

动态规划--通配字符串匹配

1. 题目来源 链接&#xff1a;通配符匹配 来源&#xff1a;LeetCode 2. 题目说明 给定一个字符串 (s) 和一个字符模式 &#xff0c;实现一个支持 ‘?’ 和 ‘*’ 的通配符匹配。 ‘?’ 可以匹配任何单个字符。 ‘*’ 可以匹配任意字符串&#xff08;包括空字符串&#xff…

U3772频谱分析仪

18320918653 U3772 新的便携式频谱分析仪具有体积小&#xff0c;重量轻的特点&#xff0c;可以在微波和毫米波范围内测量无线信号日本株式会社爱德万测试是全球半导体测试系统的领先企业&#xff0c;于2005年7月6日发布了一种新的便携式频谱分析仪U3771&#xff08;频率达到3…

强化学习笔记:基于策略的学习之策略迭代(python实现)

目录 1. 前言 2. 算法流程 3. 代码及仿真结果 3.1 class PolicyIterationPlanner() 3.2 测试代码 3.3 运行结果 3.3.1 价值估计结果 3.3.2 策略迭代得到的最终策略 1. 前言 在强化学习中&#xff0c;根据是否依赖于&#xff08;环境的&#xff09;模型&#xff0c;可以分…

一线程序员带你实践学习企业实战C/C++程序员课程

作者吴从周 资源简介&#xff1a; 传统的C/C的教材和课程都属于学院派,只是单纯的讲一些知识点,却没有讲到实际开发中我们常用的知识点有哪些,本课程将打破传统的弊端,一线程序员将带你实践学习企业实战项目中LInux后端常用的C/C开发知识! 课程目录 000.百万年薪的程序员.mp…

[GNN] 图神经网络入门

GNN和GCN的入门公式一、GNN的计算二、GCN的计算跟随B站课程【GNN图神经网络最牛教程】学不会up直接下跪&#xff01;图神经网络快速入门教程&#xff08;GNN/GCN&#xff09;的笔记 一、GNN的计算 对于一个图来说&#xff0c;要更新它自身的特征&#xff0c;也要更新它邻接节点…

系统编程中的进程的概念No.1

引言&#xff1a; 北京时间2023/1/28&#xff0c;本小编04年1月9日出生&#xff0c;今天第一次理解到进程的概念&#xff0c;所以我们接下来就学习一下什么是进程以及和进程相关的一些知识。首先我们想要了解进程以及其相关的知识&#xff0c;我们要先理解一下其它方面的知识&…

STC32G 单片机通用串行通信接口及其编程

STC32G 系列单片机有4个全双工通用串行通信接口&#xff0c;串口1与串口2既有异步通信功能、又有同步通信功能&#xff0c;串口1与串口2都可进行SPI通信&#xff0c;SPI是一个全双工高速同步串行&#xff1b;通信总线串口3、串口4只有异步通信功能。本文将重点讨论其异步通信&a…

java递归问题——汉诺塔

目录 &#x1f332;&#x1f332;什么是汉诺塔&#xff1f; &#x1f430; 当只有1个圆盘的时候&#xff1a; &#x1f430; 当只有2个圆盘的时候&#xff1a; &#x1f430; 当只有3个圆盘的时候&#xff1a; &#x1f332;&#x1f332;汉诺塔代码 &#x1f430;思路 &am…