目录
一、前言
运用JAVA所学知识,针对“类和对象”、“继承和多态”、“抽象类和接口”等知识的总和运用,巩固所学知识。
本图书管理系统采用分装不同模块的形式,将各模块的功能巧妙的联系到一起,实现了最终的图书管理系统。
二、体系框架的搭建
将该图书管理系统总体分为三个模块:书籍和书架模块、功能实现模块、用户管理员模块。
如图所示:
三、各模块的具体实现
(一)书籍和书架模块
①Book类
设置书籍基本属性,属性设置为私有属性实现分装效果,编写书本的构造方法,创建私有属性的getter和setter方法,便于外部调用接口。重写toString方法,以便书本的打印。
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 "name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + (isBorrowed==true ? "\'已借出\'" : "\'未借出");
}
}
②Boolist类
创建书架,用于存储书籍信息,本图书管理系统采用数组的方式来存储书籍(默认创建了3本书籍)。
public class BookList {
private Book[] books;
private int usedSize;
public BookList(int capacity) {
books = new Book[capacity];
books[0] = new Book("三国演义","罗贯中",19,"小说");
books[1] = new Book("西游记","吴承恩",29,"小说");
books[2] = new Book("水浒传","施耐庵",8,"小说");
usedSize = 3;
}
//新增书籍
public void addBook(Book book) {
if(usedSize == books.length) {
books = Arrays.copyOf(books,2*books.length);
}
books[usedSize++] = book;
}
//查找书籍
public int findBook(String name) {
for (int i = 0; i < usedSize; i++) {
if(name.equals(books[i].getName())) {
return i;
}
}
return -1;
}
//删除书籍
public void delBook(int findIndex) {
for (int i = findIndex; i < usedSize-1; i++) {
books[i] = books[i+1];
}
usedSize--;
}
//显示打印书籍
public void showBooks() {
for (int i = 0; i < usedSize; i++) {
System.out.println(books[i]);
}
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
(二)功能实现模块
①Operation接口
运用接口的知识,让各个功能实现方法实现Operation接口,重写接口中的work方法。便于后续运用多态调用各个功能的方法(该内容会在Main方法中讲解)
public interface Operation {
void work(BookList bookList);
}
②AddOperation类
实现Operation接口,重写work方法,实现新增图书功能。
public class AddOperation implements Operation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入添加书籍的名称");
String name = scanner.nextLine();
System.out.println("请输入作者");
String author = scanner.nextLine();
System.out.println("请输入价格");
int price = scanner.nextInt();
System.out.println("请输入类型");
String type = scanner.next();
Book book = new Book(name,author,price,type);
bookList.addBook(book);
}
}
③BorrowOperation 类
实现Operation接口,重写work方法,实现借阅图书功能。
public class BorrowOperation implements Operation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入借阅书籍的名称");
String name = scanner.nextLine();
int findIndex = bookList.findBook(name);
if(findIndex != -1) {
Book book = bookList.getBooks()[findIndex];
if(book.isBorrowed() == false) {
System.out.println("借阅成功!");
book.setBorrowed(true);
} else {
System.out.println("该书籍已借出");
}
} else {
System.out.println("未有这本书籍");
}
return;
}
}
④DelOperation类
实现Operation接口,重写work方法,实现删除图书功能。
public class DelOperation implements Operation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入删除书籍的名称");
String name = scanner.nextLine();
int findIndex = bookList.findBook(name);
if(findIndex != -1) {
bookList.delBook(findIndex);
System.out.println("删除成功!");
} else {
System.out.println("未有这本书籍");
}
return;
}
}
⑤ExitOperation类
实现Operation接口,重写work方法,实现退出图书管理功能。
public class ExitOperation implements Operation{
@Override
public void work(BookList bookList) {
System.exit(0);
}
}
⑥FindOperation类
实现Operation接口,重写work方法,实现查找图书功能。
public class FindOperation implements Operation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入查找书籍的名称");
String name = scanner.nextLine();
int findIndex = bookList.findBook(name);
if(findIndex != -1) {
System.out.println("查找成功!");
System.out.println(bookList.getBooks()[findIndex]);
} else {
System.out.println("未有这本书籍");
}
return;
}
}
⑦ReturnOperation类
实现Operation接口,重写work方法,实现归还图书功能。
public class ReturnOperation implements Operation{
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入归还书籍的名称");
String name = scanner.nextLine();
int findIndex = bookList.findBook(name);
if(findIndex != -1) {
Book book = bookList.getBooks()[findIndex];
if(book.isBorrowed() == true) {
System.out.println("归还成功!");
book.setBorrowed(false);
} else {
System.out.println("该书籍未借出");
}
} else {
System.out.println("未有这本书籍");
}
return;
}
}
⑧ShowOperation类
实现Operation接口,重写work方法,实现显示所有图书功能。
public class ShowOperation implements Operation{
@Override
public void work(BookList bookList) {
bookList.showBooks();
}
}
(三)用户管理员模块
①User抽象类
User抽象类中需要抽象用户的公共的属性,这是接口不能做的,因此采用抽象类的方式,将普通用户和管理员用户公共的属性抽象出来。而定义的Operation[]接口数组,目的是为了实现利用java多态,向上转型的特性,运行时绑定各个功能类调用其方法。
public abstract class User {
protected String name;
protected Operation[] operations;
public User(String name) {
this.name = name;
}
public abstract int manu();
public abstract void doOperation(int choice,BookList bookList);
}
②AdminUser子类
AdminUser类继承了抽象父类的属性和重写了抽象父类的抽象方法打印菜单方法menu,返回操作数字,以便在main方法中调用到doOperation的方法,从而使用户模块和功能模块之间得到联系。
package library.user;
import library.book.BookList;
import library.operation.*;
import java.util.Scanner;
public class AdminUser extends User {
private String name;
public AdminUser(String name) {
super(name);
operations = new Operation[]{//利用接口数组引用其实现类,实现多态
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()};
}
@Override
public void doOperation(int choice, BookList bookList) {//具体实现多态的方法
operations[choice].work(bookList);
}
public int manu() {
System.out.println("管理员的菜单!");
System.out.println("**************************");
System.out.println("hello" + super.name + "欢迎来到图书管理系统");
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子类
AdminUser类同样也是继承了抽象父类的属性和重写了抽象父类的抽象方法打印菜单方法menu,返回操作数字,以便在main方法中调用到doOperation的方法,从而使用户模块和功能模块之间得到联系。
package library.user;
import library.book.BookList;
import library.operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
operations = new Operation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()};
}
@Override
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
@Override
public int manu() {
System.out.println("普通用户的菜单!");
System.out.println("**************************");
System.out.println("hello! ->" + super.name + "<-欢迎来到图书管理系统");
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;
}
}
四、Main类
将图书各封装模块合并,实现图书管理系统的功能
package library;
import library.book.BookList;
import library.user.AdminUser;
import library.user.NormalUser;
import library.user.User;
import java.util.Scanner;
public class Main {
public static User login() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名");
String name = sc.nextLine();
System.out.println("请选择用户权限:管理员1/普通用户2");
int num = sc.nextInt();
User user;
if(num == 1) {
user = new AdminUser(name);
} else {
user = new NormalUser(name);
}
return user;
}
public static void main(String[] args) {
BookList bookList = new BookList(10);
//用户权限
User user = login();
while (true) {
//打印菜单
int choice = user.manu();
//用户操作
user.doOperation(choice, bookList);
}
}
}