java实现基于管理员和普通用户的图书管理系统
1、项目分析
-
管理员:
①查阅某本图书的信息;
②增加书籍;
③删除书籍;
④查看所有书籍列表;
⑤退出程序。 -
普通用户:
①查阅某本图书的信息;
②借阅书籍;
③归还书籍;
④退出程序。1、书籍类(Book类)
2、书籍列表(BookList类)
3、用户(User类)
4、管理员(Admin类)
5、普通用户(NormalUser类)
6、用户所支持的操作(IOperation接口)①增加书籍(AddOperation类)
②删除书籍(DelOperaton类)
③查找书籍(FindOperation类)
④显示书籍(DisplayOperation类)
⑤借阅书籍(BorrowOperation类)
⑥归还书籍(ReturnOperation类)
⑦退出系统(ExitOperation类)7、主函数(Main类)
2、代码实现
Book类:
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed = false;
public Book(String name,String author,double price,String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
public String getName(){
return name;
}
public boolean isBorrowed(){
return isBorrowed;
}
public void setBorrowed(boolean borrowed){
isBorrowed = borrowed;
}
}
BookList类:
public class BookList {
private Book[] books = new Book[100];
private int size = 0;
public BookList(){
books[0] = new Book("三国演义","罗贯中",80,"长篇小说");
books[1] = new Book("西游记","吴承恩",80,"经典文学");
books[2] = new Book("水浒传","施耐庵",80,"长篇小说");
books[3] = new Book("红楼梦","曹雪芹",80,"长篇小说");
size = 4;
}
public Book getBooks(int index) {
return books[index];
}
public void setBooks(int index,Book book) {
books[index] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
User类:
import BookSystem.operation.IOperation;
abstract class User {
protected String name;
protected IOperation operations[];
public abstract int menu();
public void doOperation(int choice, BookList bookList){
IOperation operation = this.operations[choice-1];
operation.work(bookList);
}
}
Admin类:
import BookSystem.operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name){
this.name = name;
this.operations = new IOperation[]{
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
System.out.println(name+"Welcome!");
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);
int choice = scanner.nextInt();
return choice;
}
}
NormalUser类:
import BookSystem.operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name){
this.name = name;
this.operations = new IOperation[]{
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation()
};
}
@Override
public int menu() {
System.out.println(name+" Welcome!");
System.out.println("1.查找书籍");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("4.关闭系统");
System.out.println("请输入你的选择:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
IOperation接口:
import BookSystem.BookList;
public interface IOperation {
void work(BookList bookList);
}
AddOperation类:
import BookSystem.Book;
import BookSystem.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList){
Scanner scanner = new Scanner(System.in);
System.out.println("添加图书");
System.out.println("请输入书籍的名称:");
String name = scanner.next();
System.out.println("请输入新书籍的作者:");
String author = scanner.next();
System.out.println("请输入新书籍的价格:");
double price = scanner.nextDouble();
System.out.println("请输入新书籍的类别:");
String type = scanner.next();
Book newBook = new Book(name,author,price,type);
//将新书添加的BookList的末尾
int curSize = bookList.getSize();
bookList.setBooks(curSize,newBook);
bookList.setSize(curSize + 1);
System.out.println("新增书籍成功!");
}
}
DelOperation类:
import BookSystem.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList){
Scanner scanner = new Scanner(System.in);
System.out.println("删除图书");
System.out.println("请输入删除书籍的名称:");
String name = scanner.next();
int i=0;
for(;i<bookList.getSize();i++){
if(name.equals(bookList.getBooks(i).getName())){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("您输入的【"+name+"】在系统中没找到!删除失败!");
return;
}
//找到即删除该位置的元素
if(i==bookList.getSize() - 1){
//当前删除元素是最后一个元素
bookList.setSize(bookList.getSize()-1);
System.out.println("删除成功!");
return;
}
//删除的元素是中间元素
bookList.setBooks(i,bookList.getBooks(bookList.getSize()-1));
bookList.setSize(bookList.getSize()-1);
System.out.println("删除成功!");
}
}
FindOperation类:
import BookSystem.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList){
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需查找的书籍名称:");
String name = scanner.next();
for(int i=0;i<bookList.getSize();i++){
if(bookList.getBooks(i).getName().contains(name)){
System.out.println(bookList.getBooks(i));
}
}
}
}
DisplayOperation类:
import BookSystem.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList){
System.out.println("显示图书列表" );
for(int i=0;i<bookList.getSize();i++){
System.out.println(bookList.getBooks(i));
}
}
}
BorrowOperation类:
import BookSystem.Book;
import BookSystem.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList){
System.out.println("借阅图书");
System.out.println("请输入需借书的名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
int i=0;
for (;i<bookList.getSize();i++){
bookList.getBooks(i).getName().equals(name);
break;
}
if(i>=bookList.getSize()){
System.out.println("未找到指定书籍,无法借阅!");
}
Book curentBook = bookList.getBooks(i);
if(curentBook.isBorrowed()) {
System.out.println("该书籍已被借出!");
return;
}
bookList.getBooks(i).setBorrowed(true);
System.out.println("借书成功!");
return;
}
}
ReturnOperation类:
import BookSystem.Book;
import BookSystem.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.next();
int i=0;
for(i=0;i<bookList.getSize();i++){
Book book = bookList.getBooks(i);
if(book.getName().equals(name)){
break;
}
}
if(i>=bookList.getSize()){
System.out.println("书籍未找到,无法归还!");
}
Book currentBook = bookList.getBooks(i);
if(currentBook.isBorrowed()){
System.out.println("本书未借出,无法归还!");
return;
}
currentBook.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
ExitOperation类:
import BookSystem.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList){
System.out.println("退出系统");
System.exit(0);
}
}
Main类:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//1.实例化书籍
BookList bookList = new BookList();
//2.创建用户
User user = login();
//3.进入循环
while(true){
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.next();
System.out.println("请输入您的身份:1.管理员 0.用户");
int who = scanner.nextInt();
if(who == 1) {
return new Admin(name);
}
return new NormalUser(name);
}
}