图书管理系统
- 1. 描述
- a> 角色
- b> 操作
- 2. 实现
- a> Book 类
- b> BookList 类
- c> IOperation 操作接口
- d> AddBook 类
- e> DeleteBook 类
- f> UpdateBook 类
- g> FindBook 类
- h> BorrowBook 类
- i> ReturnBook 类
- j> DisplayBook 类
- k> Exit 类
- l> User 类
- m> Admin 类
- n> NormalUser 类
- o> Main 类
- 3. 测试结果
- a> 管理员
- b> 普通用户
1. 描述
a> 角色
图书管理系统有两个角色对书进行操作, 分别是普通用户类和管理员类.
b> 操作
操作可划分为: 增添图书, 删除图书, 更新图书, 查找图书, 借阅图书, 归还图书, 退出系统
2. 实现
a> Book 类
package java0126;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:37
*/
public class Book {
private String name;
private String author;
private int price;
private String type;
// 表示没有被借
private boolean isBorrowed = false;
public Book(String name, int price, String author, String type) {
this.name = name;
this.price = price;
this.author = author;
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) {
this.isBorrowed = borrowed;
}
public void bookImformation() {
System.out.println(this.name + " " + this.price + " "
+ this.author + " " + this.type + " " + this.isBorrowed);
}
}
b> BookList 类
package java0126;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:37
*/
public class BookList {
private Book[] books = new Book[100] ;
private int size;
// 图书管理系统的关闭和开启条件
private boolean isOpen = true;
public BookList() {
this.books[this.size++] = new Book("三国演义",100, "罗贯中","历史小说");
}
public void addBook(String name, int price, String author, String type) {
this.books[size++] = new Book(name, price, author, type);
}
public Book getBook(int index) {
if (!checkIndex(index)) {
return null;
}
return this.books[index];
}
public void setSize(int size) {
this.size = size;
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean open) {
this.isOpen = open;
}
public int getSize() {
return this.size;
}
public void borrowBook(int index) {
if (!checkIndex(index)) {
return;
}
if (!this.books[index].isBorrowed()) {
this.books[index].setBorrowed(true);
}
}
public void displayBook() {
if (this.books == null) {
System.out.println("没有书籍");
return;
}
System.out.println(" 书名 " + " 价格 " + " 作者 " + " 类型 " + " 是否被借");
for (int i = 0; i < this.size; i++) {
this.books[i].bookImformation();
}
}
public void deleteBook(int index) {
if (!checkIndex(index)) {
return;
}
for (int i = index; i < this.size - 1; i++) {
this.books[i] = this.books[i + 1];
}
this.size--;
}
public void updateBook(int index) {
if (!checkIndex(index)) {
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的内容:");
System.out.println("1. 书名");
System.out.println("2. 作者");
System.out.println("3. 类型");
System.out.println("4. 价格");
System.out.println("5. 书的状态");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入修改后的书名: ");
String name = scanner.next();
this.books[index].setName(name);
break;
case 2:
System.out.println("请输入修改后的作者: ");
String author = scanner.next();
this.books[index].setAuthor(author);
break;
case 3:
System.out.println("请输入修改后的类型: ");
String type = scanner.next();
this.books[index].setType(type);
break;
case 4:
System.out.println("请输入修改后的价格: ");
int price = scanner.nextInt();
this.books[index].setPrice(price);
break;
case 5:
System.out.println("请输入修改后的书的状态(是否被借): ");
boolean isBorrowed = scanner.hasNextBoolean();
this.books[index].setBorrowed(isBorrowed);
break;
}
}
public boolean checkIndex(int index) {
if (index < 0 && index >= this.size) {
System.out.println("输入有误, 您输入的下标不存在!");
return false;
}
return true;
}
}
c> IOperation 操作接口
package java0126.Operation;
import java0126.BookList;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:46
*/
public interface IOperation {
public void work(BookList bookList);
}
d> AddBook 类
package java0126.Operation;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:42
*/
public class AddBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("添加书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书的名字:");
String name = scanner.next();
System.out.println("请输入书的价格:");
int price = scanner.nextInt();
System.out.println("请输入书的类型:");
String type = scanner.next();
System.out.println("请输入书的作者:");
String author = scanner.next();
bookList.addBook(name, price, author, type);
}
}
e> DeleteBook 类
package java0126.Operation;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:43
*/
public class DeleteBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("删除书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书的下标");
int index = scanner.nextInt();
bookList.deleteBook(index);
}
}
f> UpdateBook 类
package java0126.Operation;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:43
*/
public class UpdateBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("更新书籍信息");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要更新的书的下标:");
int index = scanner.nextInt();
bookList.updateBook(index);
}
}
g> FindBook 类
package java0126.Operation;
import java0126.Book;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:43
*/
public class FindBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要找的输的下标");
int index = scanner.nextInt();
Book book = bookList.getBook(index);
book.bookImformation();
}
}
h> BorrowBook 类
package java0126.Operation;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 15:48
*/
public class BorrowBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借的书的下标: ");
int index = scanner.nextInt();
// 该书存在 并且 书的状态是 false 未被借
if (bookList.checkIndex(index) && !bookList.getBook(index).isBorrowed()) {
// 借书, 将书的状态改为 true 表示已被借阅
bookList.getBook(index).setBorrowed(true);
}
}
}
i> ReturnBook 类
package java0126.Operation;
import java0126.BookList;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 21:45
*/
public class ReturnBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("还书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借的书的下标: ");
int index = scanner.nextInt();
if (bookList.checkIndex(index) && bookList.getBook(index).isBorrowed()) {
bookList.getBook(index).setBorrowed(false);
}
}
}
j> DisplayBook 类
package java0126.Operation;
import java0126.BookList;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 14:12
*/
public class DisplayBook implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("显示书籍");
bookList.displayBook();
}
}
k> Exit 类
package java0126.Operation;
import java0126.BookList;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 14:19
*/
public class Exit implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出");
bookList.setOpen(false);
}
}
l> User 类
package java0126;
import java0126.Operation.IOperation;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:51
*/
public abstract class User {
protected String name;
protected IOperation[] userOperation;
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
this.userOperation[choice - 1].work(bookList);
}
}
m> Admin 类
package java0126;
import java0126.Operation.*;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:51
*/
public class Admin extends User {
public Admin(String name) {
this.name = name;
userOperation = new IOperation[]{
new FindBook(),
new AddBook(),
new DeleteBook(),
new UpdateBook(),
new DisplayBook(),
new Exit()
};
}
@Override
public int menu() {
System.out.println("=====================");
System.out.println("1.查找书籍");
System.out.println("2.添加书籍");
System.out.println("3.删除书籍");
System.out.println("4.更新书籍信息");
System.out.println("5.显示书籍信息");
System.out.println("6.退出");
System.out.println("=====================");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的选择的操作: ");
int choice = scanner.nextInt();
return choice;
}
}
n> NormalUser 类
package java0126;
import java0126.Operation.*;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:51
*/
public class NormalUser extends User{
public NormalUser(String name) {
this.name = name;
userOperation = new IOperation[]{
new FindBook(),
new BorrowBook(),
new ReturnBook(),
new DisplayBook(),
new Exit()
};
}
@Override
public int menu() {
System.out.println("=====================");
System.out.println("1.查找书籍");
System.out.println("2.借阅书籍");
System.out.println("3.归还书籍");
System.out.println("4.显示书籍信息");
System.out.println("5.退出");
System.out.println("=====================");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的选择的操作: ");
int choice = scanner.nextInt();
return choice;
}
}
o> Main 类
package java0126;
import java.util.Scanner;
/**
* @author FMM
* @version 7.0
* @date 2021/1/26 12:49
*/
public class Main {
public static void main(String[] args) {
// 1. 初始化书籍
BookList bookList = new BookList();
// 2. 登录
User user = login();
// 3. 执行相关操作
while (bookList.isOpen() == true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
public static User login() {
System.out.println("0.管理员");
System.out.println("1.用户");
System.out.print("请输入您的选择:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
System.out.print("请输入用户名:");
String name = scanner.next();
if (choice == 1) {
return new NormalUser(name);
}
return new Admin(name);
}
}
3. 测试结果
a> 管理员
管理角色测试结果
b> 普通用户
用户角色测试结果