图书管理系统(简易版)
一.功能
- 用户的登录
- 普通用户进行 查找图书 、借阅图书、归还图书、退出系统 相关操作
- 管理员进行 查找图书、新增图书、删除图书、显示图书、退出系统 相关操作
二.代码概览
各个类实现的功能
- book 存放一本书的属性
- BookList 存放书 记录书 的个数
- User (父类)
- AdminUser 继承父类User 实现管理员登录界面
- NormalUser 继承父类User 实现普通用户登录界面
- AddOperation 增加图书操作
- DelOperation 删除图书操作
- FindOperation 查找图书操作
- ShowOperation 显示图书操作
- BrrowOperation 借阅图书操作
- ReturnOpertion 归还图书操作
- ExitOperation 退出系统操作
- IOpreation 接口 链接与BookList的操作
三.具体实现
1.创建一个user包
User类代码:
package user;
import book.BookList;
import opera.IOperation;
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);
}
}
AdminUser类代码:
package user;
import opera.*;
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 ShowOperation(),
};
}
public int menu() {
System.out.println("*******************************");
System.out.println("hello " + 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;
}
}
NormalUser类代码:
package user;
import opera.*;
import java.util.Scanner;
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("=========================");
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;
}
}
2.创建一个book包
book类 代码实现
package 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=" + isBorrowed +
(isBorrowed == true ? " 已经被借出" : " 未被借出") +
'}';
}
}
BookList 类代码的实现
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//记录当前books数组当中有多少本书
public BookList() {
books[0] = new Book("java","张三",89,"学习");
books[1] = new Book("c++","李四",67,"学习");
books[2] = new Book("python","王五",55,"学习");
books[3] = new Book("c","赵六",99,"学习");
this.usedSize = 4;
}
public Book getBook(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setBook(Book book) {
this.books[usedSize] = book;
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
3.创建一个opera 包
AddOperation 类代码
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("1.新增图书");
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;
}
}
System.out.println("添加成功!");
bookList.setBook(book);
//修改UsedSize
bookList.setUsedSize(currentSize + 1);
bookList.setBook(currentSize + 1,book);
}
}
DelOperaion 类代码
package opera;
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 currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
System.out.println("删除成功!");
index = i;
break;
}
}
//挪动数据
for (int j = 0; j < currentSize - 1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setUsedSize(currentSize - 1);
//因为删除的是对象,所以 把最后一个置为null
bookList.setBook(currentSize - 1,null);
System.out.println("删除成功");
}
}
BrrowOperation 类代码
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BrrowOperation 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 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;
}
}
}
}
ExitOperation 类代码
package opera;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BrrowOperation 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 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;
}
}
}
}
FindOperation 类代码
package opera;
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();
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;
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();
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;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示所有图书!");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
IOperation 接口代码
package opera;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
4.Main 方法
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份:1 -> 管理员 2 -> 普通用户");
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();
user.doWork(choice, bookList);
}
}
}
四.效果展示