图书管理系统
目录
一、项目框架
1、在idea中新建一个项目取名为:BookManagement。
2、在项目的src目录下按照以下结构创建对应的 包 和.java文件。
二、图书模块代码
1、Book
package book;
/**
* Remarks: book类
* Author:panlai
* :Date:2021/4/14
*/
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBoeeowed;
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 isBoeeowed() {
return isBoeeowed;
}
public void setBoeeowed(boolean boeeowed) {
isBoeeowed = boeeowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
// ", isBoeeowed=" + isBoeeowed +
((isBoeeowed == true)?" 已借出" : " 未借出")+
'}';
}
}
2、BookList
package book;
/**
* Remarks: 图书列表
* Author:panlai
* :Date:2021/4/14
*/
public class BookList {
private Book[] books = new Book[100];
private int usedSize = 0;
public BookList(){
books[0] = new Book("三国演义","罗贯中",100,"小说");
books[1] = new Book("水浒传","施耐庵",100,"小说");
books[2] = new Book("西游记","吴承恩",100,"小说");
this.usedSize = 3;
}
//可以吧所有的操作都写在这个类当中,因为每个类都是操作books的,但是为了用接口,就不这样做了
public void setBooks(int pos, Book book){
this.books[pos] = book;
}
public Book getBook(int pos){
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
三、操作模块代码
1、新增图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Remarks: 新增图书
* Author:panlai
* :Date:2021/4/14
*/
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
System.out.println("请输入图书的作者:");
String author = sc.nextLine();
System.out.println("请输入图书的价格:");
int price = sc.nextInt();
System.out.println("请输入图书的类型:");
String type = sc.next();
Book book = new Book(name,author,price,type);
int curSize = bookList.getUsedSize();
bookList.setBooks(curSize,book);
bookList.setUsedSize(curSize+1);
System.out.println("新增成功!!");
}
}
2、借阅图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Remarks:借阅图书
* Author:panlai
* :Date:2021/4/14
*/
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要借阅图书的名字:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
//可以借阅
book.setBoeeowed(true);
System.out.println("借阅成功!!");
return;
}
}
System.out.println("没有找到你要借阅的书!!");
}
}
3、删除图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Remarks: 删除书籍
* Author:panlai
* :Date:2021/4/14
*/
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除图书的名字:");
String name = sc.nextLine();
int i = 0;
//先找
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
break;
}
}
//因为没找到退出
if (i == bookList.getUsedSize()){
System.out.println("没有这本书!!");
return ;
}
//找到了删除
int curSize = bookList.getUsedSize()-1;
for (int pos = i; pos < curSize; pos++) {
//bookList[pos] = bookList[pos+1]; //不能直接这样,要调用方法
Book book = bookList.getBook(pos+1);
bookList.setBooks(pos,book);
}
bookList.setUsedSize(curSize); //长度-1
bookList.setBooks(curSize+1,null);
System.out.println("删除成功!");
}
}
4、显示所有图书
package operation;
import book.Book;
import book.BookList;
/**
* Remarks: 展示书籍
* Author:panlai
* :Date:2021/4/14
*/
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
5、退出系统
package operation;
import book.BookList;
/**
* Remarks: 退出系统
* Author:panlai
* :Date:2021/4/14
*/
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0); //退出时,0代表正常退出
}
}
6、查找图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Remarks: 查找书籍
* Author:panlai
* :Date:2021/4/14
*/
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
System.out.println(book);
System.out.println("查找成功!!");
return;
}
}
System.out.println("没有找到你要查找的书!!");
}
}
7、操作接口
package operation;
import book.BookList;
public interface IOperation {
public void work(BookList bookList);
}
8、归还图书
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Remarks: 归还书籍
* Author:panlai
* :Date:2021/4/14
*/
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要归还图书的名字:");
String name = sc.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
//可以归还
book.setBoeeowed(false);
System.out.println("归还成功!!");
return;
}
}
System.out.println("没有找到你要归还的书!!");
}
}
四、用户登录模块代码
1、管理员
package user;
import operation.*;
import java.util.Scanner;
/**
* Remarks: 管理员,继承于User
* Author:panlai
* :Date:2021/4/14
*/
public class Admin extends User{
public Admin(String name){
super(name);
this.operations = new IOperation[]{ //这是一个关于操作的数组,里边存放的都是操作
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("============================");
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;
}
}
2、普通用户
package user;
import operation.*;
import java.util.Scanner;
/**
* Remarks: 普通用户,继承于User
* Author:panlai
* :Date:2021/4/14
*/
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operations = new IOperation[]{ //对于用户来说,IOpertation有下面几个操作,因为放在了构造方法中,所以在new对象的时这些操作就已经生成了
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("=============================");
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;
}
}
3、用户抽象类
package user;
import book.BookList;
import operation.IOperation;
/**
* Remarks: 用户类,是管理员和普通用户父类
* Author:panlai
* :Date:2021/4/14
*/
public abstract class User {
protected String name;
//1、准备一个接口数组,存储每个对象
protected IOperation[] operations;
public User(String name){
this.name = name;
}
public abstract int menu();
public void doOperation(BookList bookList,int choice){
this.operations[choice].work(bookList); //这个operatation数组中的内容(操作)早已在登录的时候存放进去了,构造方法。
}
}