目录
实现该项的目的:整合并强化前期的学习,充分利用Java面向对象的编程思想来实现该项目,进一步加深对于:类、对象、抽象类、接口、封装、继承、多态的理解.
项目要求
1.实现管理员操作界面和借阅者操作界面两个功能;
2.管理员操作端:
①显示图书;
②查找图书;
③添加图书;
④删除图书;
⑤退出.
3.借阅者操作端:
①显示图书;
②查找图书;
③借阅图书;
④归还图书;
⑤退出.
为了方便对于代码的理解以及代码的规范性,创建了如下的包和类
book包主要用来图书的存储、operation包主要用来实现图书管理系统的操作、user包用来区分管理端和借阅端.
项目代码储存
代码已上传至gitee仓库中,请点击:图书管理系统
在目录中找到textbook文件即可获取.
代码实现
book包
Book.java
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;//书的类型
private boolean isBorrow;
//构造方法重载,主要用来实现增加图书功能时的初始化
public Book(){
this.name = "0";
this.author = "0";
this.price = 0;
this.type = "未借出";
}
//构造方法重载
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 isBorrow() {
return isBorrow;
}
public void setBorrow(boolean borrow) {
isBorrow = borrow;
}
//重写toString方法
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
'}';
}
}
BookList.java
package book;
public class BookList {
private Book[] books = new Book[20];
private int usedSize;//当前书架上有几本书,默认初始化为0
//对图书系统初始化
public BookList(){
books[0] = new Book("斗罗大陆", "唐三", 27, "未借出");
books[1] = new Book("Java编程思想", "Bruce Eckel", 108, "未借出");
books[2] = new Book("Effective Java", "Joshua Bloch", 119, "未借出");
this.usedSize = 3;
}
//实现指定图书信息的打印功能
public void printBook(int serial){
System.out.println(books[serial].toString());
}
public Book[] getBookList() {
return books;
}
public void setBookList(Book[] bookList) {
this.books = bookList;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//把书放到pos位置上
public void setBook(int pos, Book book){
books[pos] = book;
}
//获取pos位置上的书
public Book getBook(int pos){
return books[pos];
}
}
operation包
IOperation.java //创建接口类
package operation;
import book.BookList;
public interface IOperation {
public void work(BookList bookList);
}
AddOperation.java //增加图书操作类,实现类来实现接口类
import book.*;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============新增图书=============");
Scanner scan = new Scanner(System.in);
Book book = new Book();
System.out.println("请输入书名: ");
book.setName(scan.next());
System.out.println("请输入作者: ");
book.setAuthor(scan.next());
System.out.println("请输入书的价格: ");
book.setPrice(scan.nextInt());
bookList.setBook(bookList.getUsedSize(), book);
bookList.setUsedSize(bookList.getUsedSize() + 1);
System.out.println("添加成功");
}
}
BorrowOperation.java //借阅图书操作类,实现类来实现接口类
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============借阅图书=============");
Scanner scan = new Scanner(System.in);
System.out.println("请输入你要借阅的图书的名称: ");
String name = scan.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
if(bookList.getBook(i).isBorrow()){
System.out.println("《" + name + "》已经借出! ");
return;
}
bookList.getBook(i).setType("已借出");
bookList.getBook(i).setBorrow(true);
System.out.println("借书成功");
return;
}
}
System.out.println("未找到《" + name + "》这本书");
}
}
DelOperation.java //删除图书操作类,实现类来实现接口类
package operation;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============删除图书=============");
Scanner scan = new Scanner(System.in);
System.out.println("请输入你想要删除的书名: ");
String name = scan.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
for (int j = i; j < bookList.getUsedSize() - 1 ; j++) {
bookList.setBook(j, bookList.getBook(j + 1));
}
bookList.setUsedSize(bookList.getUsedSize() - 1);
System.out.println("删除《" + name + "》成功! ");
return;
}
}
System.out.println("《" + name + "》不存在,无法进行删除! ");
}
}
DisplayOperation.java //显示图书操作类,实现类来实现接口类
package operation;
import book.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============图书显示=============");
for (int i = 0; i < bookList.getUsedSize(); i++) {
bookList.printBook(i);
}
}
}
ExitOperation.java //退出图书系统操作类,实现类来实现接口类
package operation;
import book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============退出系统=============");
System.exit(0);
}
}
FindOperation.java //查找图书操作类,实现类来实现接口类
package operation;
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 scan = new Scanner(System.in);
String name = scan.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
System.out.println("成功查找到《" + name + "》");
byte id = 0;
System.out.println("是否查看本书全部信息 【0】是 【1】否");
do {
id = scan.nextByte();
if(id == 1 || id == 0){
break;
}
System.out.println("输入有误,请重新输入");
id = scan.nextByte();
} while(id != 0 && id !=1);
if(id == 0){
bookList.printBook(i);
return;
}
return;
}
}
System.out.println("未查找到该书! ");
}
}
ReturnOperation.java //归还图书操作类,实现类来实现接口类
package operation;
import book.*;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("=============归还图书=============");
System.out.println("输出要归还的书名: ");
Scanner scan = new Scanner(System.in);
String name = scan.next();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(bookList.getBook(i).getName().equals(name)){
if(! bookList.getBook(i).isBorrow()){
System.out.println("此书未被借出,无需归还! ");
return;
}
bookList.getBook(i).setBorrow(false);
bookList.getBook(i).setType("未借出");
System.out.println("归还成功! ");
return;
}
}
System.out.println("图书存储系统中无《" + name + "》这本书");
}
}
user包
user.java // 父类/基类/超类
package user;
import book.*;
import operation.IOperation;
abstract public class User {
private String name;
public IOperation[] iOperations;
abstract public int menu();
public void doIOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
AdminUser.java //user类的子类/派生类,实现管理员操作界面
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new DisplayOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation()
};
}
@Override
public int menu() {
Scanner scan = new Scanner(System.in);
System.out.println("=============管理界面=============");
System.out.println("Hello\"" + super.getName() + "\"欢迎进入YaYa图书系统");
System.out.println("1.显示图书");
System.out.println("2.查找图书");
System.out.println("3.增加图书");
System.out.println("4.删除图书");
System.out.println("0.退出系统");
return scan.nextInt();
}
}
CommonUser.java //user类的子类/派生类,实现借阅者操作界面
package user;
import operation.*;
import java.util.Scanner;
public class CommonUser extends User {
public CommonUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitOperation(),
new DisplayOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
Scanner scan = new Scanner(System.in);
System.out.println("=============用户界面=============");
System.out.println("Hello\"" + super.getName() + "\"欢迎进入YaYa图书系统");
System.out.println("1.图书显示");
System.out.println("2.查找图书");
System.out.println("3.借阅图书");
System.out.println("4.归还图书");
System.out.println("0.退出系统");
return scan.nextInt();
}
}
Main类
import book.*;
import operation.IOperation;
import user.*;
import java.util.Scanner;
public class Main {
public static User forReady() {
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scan.next();
byte id = 0;
do {
System.out.println("请输入你的身份: 【0】普通用户 【1】管理员");
id = scan.nextByte();
if(id == 0 || id == 1){
break;
}
System.out.println("输入有误,请重新输入! ");
id = scan.nextByte();
} while(id != 1 && id != 0);
if(id == 1){
return new AdminUser(name);
}
return new CommonUser(name);
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = forReady();
while(true) {
int choice = user.menu();
user.doIOperation(choice, bookList);
}
}
}
代码演示
管理员界面演示
请输入你的姓名:
YayAA
请输入你的身份: 【0】普通用户 【1】管理员
1
=============管理界面=============
Hello"YayAA"欢迎进入YaYa图书系统
1.显示图书
2.查找图书
3.增加图书
4.删除图书
0.退出系统
1
=============图书显示=============
Book{name='斗罗大陆', author='唐三', price=27, type='未借出'}
Book{name='Java编程思想', author='Bruce Eckel', price=108, type='未借出'}
Book{name='Effective Java', author='Joshua Bloch', price=119, type='未借出'}
=============管理界面=============
Hello"YayAA"欢迎进入YaYa图书系统
1.显示图书
2.查找图书
3.增加图书
4.删除图书
0.退出系统
4
=============删除图书=============
请输入你想要删除的书名:
斗罗大陆
删除《斗罗大陆》成功!
=============管理界面=============
Hello"YayAA"欢迎进入YaYa图书系统
1.显示图书
2.查找图书
3.增加图书
4.删除图书
0.退出系统
=============图书显示=============
Book{name='Java编程思想', author='Bruce Eckel', price=108, type='未借出'}
Book{name='Effective Java', author='Joshua Bloch', price=119, type='未借出'}
用户界面演示
请输入你的姓名:
借阅者
请输入你的身份: 【0】普通用户 【1】管理员
0
=============用户界面=============
Hello"借阅者"欢迎进入YaYa图书系统
1.图书显示
2.查找图书
3.借阅图书
4.归还图书
0.退出系统
3
=============借阅图书=============
请输入你要借阅的图书的名称:
斗罗大陆
借书成功
=============用户界面=============
Hello"借阅者"欢迎进入YaYa图书系统
1.图书显示
2.查找图书
3.借阅图书
4.归还图书
0.退出系统
1
=============图书显示=============
Book{name='斗罗大陆', author='唐三', price=27, type='已借出'}
Book{name='Java编程思想', author='Bruce Eckel', price=108, type='未借出'}
Book{name='Effective Java', author='Joshua Bloch', price=119, type='未借出'}
=============用户界面=============
Hello"借阅者"欢迎进入YaYa图书系统
1.图书显示
2.查找图书
3.借阅图书
4.归还图书
0.退出系统
4
=============归还图书=============
输出要归还的书名:
斗罗大陆
归还成功!
=============用户界面=============
Hello"借阅者"欢迎进入YaYa图书系统
1.图书显示
2.查找图书
3.借阅图书
4.归还图书
0.退出系统
1
=============图书显示=============
Book{name='斗罗大陆', author='唐三', price=27, type='未借出'}
Book{name='Java编程思想', author='Bruce Eckel', price=108, type='未借出'}
Book{name='Effective Java', author='Joshua Bloch', price=119, type='未借出'}