当前位置:网站首页>图书管理系统
图书管理系统
2022-07-29 02:08:00 【小王不迷糊】
前言
总结Java的语法,围绕基础语法写一个图书管理系统,图书管理系统主要有管理员和普通用户两种身份,通过不同身份对图书进行不同操作的一个系统。
一、系统框架
该系统主要分为图书、功能、用户三个模块,图书模块用来存储图书信息;功能模块用来存储系统的操作功能,如添加图书、查找图书等;用户模块将用户分为普通用户和管理员两种身份。最后,通过Main函数对所有模块进行融合是最重要的地方。

二、具体实现
1.BOOK
Book(图书信息):
public class Book {
private String name;//书名
private String author;//作者
private double price;//价格
private String type;//图书类型
private boolean isBorrow;//图书是否被借阅
public Book(String name, String author, double 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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrow=" + isBorrow +
'}';
}
}
BookList(书架信息):
public class BookList {
private Book[] books = new Book[10];//书架
private int UsedSize;//已经存放的图书个数
public BookList() {
books[0] = new Book("三国演义","罗贯中",19.9,"小说");
books[1] = new Book("西游记","吴承恩",59.9,"小说");
books[2] = new Book("水浒传","施耐庵",39.9,"小说");
this.UsedSize = 3;
}
public int getUsedSize() {
return UsedSize;
}
public void setUsedSize(int usedSize) {
UsedSize = usedSize;
}
/** *获取某个位置上的书 * @param pos 是在合法的情况下 * @return */
public Book getBook(int pos) {
return books[pos];
}
/** *设置某个位置的图书 * @param pos 是合法的情况下 * @param book */
public void setBook(int pos,Book book) {
books[pos] = book;
}
}
2.Operation
通过定义一个接口,其他各个功能通过实现接口完成各个操作工能。
接口:
//接口
public interface IOperation {
void work(BookList bookList);
}
查找图书:
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("请输入图书的名字:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(name.equals(book.getName())) {
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
展示图书:
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
System.out.println(bookList.getBook(i));
}
}
}
添加图书:
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
System.out.println("请输入作者名字:");
String author = sc.nextLine();
System.out.println("请输入图书的类型:");
String type = sc.nextLine();
System.out.println("请输入图书的价格:");
double price = sc.nextDouble();
Book book = new Book(name,author,price,type);
int size = bookList.getUsedSize();;
bookList.setBook(size,book);
bookList.setUsedSize(size+1);
System.out.println("添加成功!");
}
}
删除图书:
public class DelOperation implements IOperation{
public void delete(int pos,BookList bookList) {
for (int i = pos; i < bookList.getUsedSize()-1; i++) {
bookList.setBook(i, bookList.getBook(i+1));
}
}
@Override
public void work(BookList bookList) {
System.out.println("请输入图书的名字:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(name.equals(book.getName())) {
delete(i,bookList);
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功");
return;
}
}
System.out.println("没有找到该图书");
}
}
借阅图书:
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
System.out.println("请输入图书的名字:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(name.equals(book.getName())) {
book.setBorrow(true);
return;
}
}
System.out.println("没有这本书");
}
}
归还图书:
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请输入图书的名字:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(name.equals(book.getName())) {
book.setBorrow(false);
return;
}
}
System.out.println("没有这本书");
}
}
退出系统:
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
3.USER
User(示例):
public abstract class User {
protected String name;
public IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doIOperation(int choice,BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
AdminUser(管理员):
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()
};
}
@Override
public int menu() {
System.out.println("hello "+this.name+" 欢迎来到图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("请输入你的操作:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}
NormalUser(普通用户):
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("hello "+this.name+" 欢迎来到图书管理系统");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("请输入你的操作:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
}
4.Main
Main:
public class Main {
public static User login() {
System.out.println("请输入你的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("请输入你的身份:1->管理员;0->普通用户");
int choice = sc.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();//根据用户的类型分别调用菜单
user.doIOperation(choice,bookList);//根据choice实现某个功能
}
}
}
边栏推荐
- ES6 event binding (v-on usage)
- When I look at the source code, what am I thinking?
- [upload picture 2-cropable]
- 3d智能工厂工艺流转可视化交互展示应用优点
- Multimodal unsupervised image to image translation
- 工程经济学知识点总结
- 7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
- Mqtt routine
- ROCBOSS开源微社区轻论坛类源码
- ES2022 的 8 个实用的新功能
猜你喜欢

Servlet三种实现方式

云开发口袋工具箱微信小程序源码

Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM

Polygon point test

聊聊 Feign 的实现原理

What happens if you have to use ArrayList in multithreading?

Quickly master nodejs installation and getting started

3D intelligent factory process flow visualization interactive display application advantages

Explain asynchronous tasks in detail: task status and lifecycle management

九宫格心形拼图小程序源码/带流量主微信小程序源码
随机推荐
Remember error scheduler once Asynceventqueue: dropping event from queue shared causes OOM
What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
家庭亲戚关系计算器微信小程序源码
Intel's IPP Library (Integrated Performance Primitives)
ECCV 2022 | AirDet:无需微调的小样本目标检测方法
Esbuild Bundler HMR
Mqtt routine
Meeting notice of meeting OA
Memories of many years ago
详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
time_ Wait and close_ Cause of wait
FPGA skimming memory (Verilog implementation of ram and FIFO)
XSS range (II) xss.haozi
ROCBOSS开源微社区轻论坛类源码
Time pit in MySQL driver
where、having、group by、order by,is null,not in,子查询,delete,日期函数
物联网组件
When synchronized encounters this thing, there is a big hole, so be careful
云开发打工人必备上班摸鱼划水微信小程序源码