JavaSE实现图书管理系统
目录
一、图书管理系统功能
管理员端:添加图书、删除图书、查找图书、显示图书、退出
登入需要输入密码。
普通用户端:借阅图书、归还图书、查找图书、显示图书、退出
登入无需输入密码
相关包的组成:
二、代码实现
1.书的属性
Book类(父类)
直接在重写的toString
方法中判断是否借出
package Book;
import java.util.Objects;
public class Book {
private String name;
private String writer;
private double price;
private boolean isborrow;
public Book(String name, String writer, double price) {
this.name = name;
this.writer = writer;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", writer='" + writer + '\'' +
", price=" + price +'\'' +
(isborrow?",以借出":",未借出")+
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isIsborrow() {
return isborrow;
}
public void setIsborrow(boolean isborrow) {
this.isborrow = isborrow;
}
}
BookList类
package Book;
import java.util.Arrays;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:20
*/
public class BookList {
private Book[] books = new Book[10];
private int number ;
public BookList() {
books[0] = new Book("活着","余华",19.9);
books[1] = new Book("红楼梦","曹雪芹",29.3);
books[2] = new Book("哑舍","玄色",35.2);
number = 3;
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "BookList{" +
"books=" + Arrays.toString(books) +
'}';
}
}
2.功能的实现
标准接口
后续方法都重写标准接口里面的work
package Interface;
import Book.BookList;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:37
*/
public interface INterface {
void work(BookList bookList);
}
AddBook类
直接在books
数组后加一个book
元素,并把记录数组中元素个数的数字+1。
package Interface;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:38
*/
public class AddBook implements INterface {
@Override
public void work(BookList bookList) {
System.out.println("添加图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入图书名->");
String name = scanner.nextLine();
System.out.print("请输入作者->");
String author = scanner.nextLine();
System.out.print("请输入价格->");
double price = scanner.nextDouble();
Book book = new Book(name,author,price);
bookList.setBooks(bookList.getNumber(),book);
int num = bookList.getNumber()+1;
bookList.setNumber(num);
}
}
DelBook类
直接在books
数组中删除对应名字的书,把这本书之后的所有书前移一位,再把记录数组中元素个数的数字-1。
package Interface;
import Book.BookList;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:39
*/
public class DelBook implements INterface{
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的书名: ");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getNumber(); i++) {
if(bookList.getBooks(i).getName().equals(name)) {
for (int j = i; j < bookList.getNumber()-1; j++) {
bookList.setBooks(j,bookList.getBooks(j+1));
}
bookList.setNumber(bookList.getNumber()-1);
System.out.println("删除成功!");
return;
}
}
System.out.println("图书馆没有这本书哦~");
return;
}
}
BorrowBook类
查找书架中是否有这本书,如果有的话并且这本书没有被借出的话,把这本书的状态改成已借出,如果这本书已被借或者书架中没有这本书就退出。
package Interface;
import Book.BookList;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:42
*/
public class BorrowBook implements INterface {
@Override
public void work(BookList bookList) {
System.out.println("借出图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要借图书的书名: ");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getNumber(); i++) {
if(bookList.getBooks(i).getName().equals(name)) {
if(bookList.getBooks(i).isIsborrow() == true) {
System.out.println("这本书以被借出!");
return;
}
bookList.getBooks(i).setIsborrow(true);
System.out.println("借书成功!");
return;
}
}
System.out.println("本图书馆没有这本书哦~");
return;
}
}
FindBook类
按照书名查找图书。
package Interface;
import Book.BookList;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:40
*/
public class FindBook implements INterface {
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的书名: ");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getNumber(); i++) {
if(bookList.getBooks(i).getName().equals(name)) {
System.out.println(bookList.getBooks(i).toString());
return;
}
}
System.out.println("图书馆没有这本书哦~");
return;
}
}
ReturnBook类
归还图书,如果书架中有这本以借出的书就把以借出状态改成未借出,如果书架中没有这本书就退出。
package Interface;
import Book.BookList;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:42
*/
public class ReturnBook implements INterface{
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要归还的书名: ");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getNumber(); i++) {
if(bookList.getBooks(i).getName().equals(name)) {
bookList.getBooks(i).setIsborrow(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("这本书不是本图书馆的哦~");
return;
}
}
SeeBook类
打印书架中的所有书
package Interface;
import Book.BookList;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:41
*/
public class SeeBook implements INterface{
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
for (int i = 0; i < bookList.getNumber(); i++) {
System.out.println(bookList.getBooks(i).toString());
}
}
}
ExitBook类
退出程序
package Interface;
import Book.BookList;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:41
*/
public class ExitBook implements INterface {
@Override
public void work(BookList bookList) {
System.out.println("退出程序");
System.exit(0);
}
}
三、用户端
User类(抽象父类)
package User;
import Book.BookList;
import Interface.INterface;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:47
*/
public abstract class User {
private String name;
//创建一个里面全都是接口的数组
public INterface[] iNterfaces;
public abstract int menu();
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void inplementInterface(int score, BookList bookList) {
this.iNterfaces[score].work(bookList);
}
}
AdminUser类
package User;
import Interface.*;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:49
*/
public class AdminUser extends User{
public static final String code = "admin";
public AdminUser(String name) {
super(name);
//iNterface是接口,里面存的类都实现了这个接口,相当于向上转型
this.iNterfaces = new INterface[]{
new ExitBook(),
new AddBook(),
new DelBook(),
new FindBook(),
new SeeBook()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("尊敬的管理员:"+getName()+" 欢迎进入图书管理系统!");
System.out.println("1.添加图书");
System.out.println("2.删除图书");
System.out.println("3.查找图书");
System.out.println("4.显示图书");
System.out.println("0.退出");
System.out.print("请输入进行的操作:->");
int score = scanner.nextInt();
return score;
}
}
NormalUser类
package User;
import Interface.*;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:53
*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iNterfaces = new INterface[]{
new ExitBook(),
new BorrowBook(),
new ReturnBook(),
new FindBook(),
new SeeBook()
};
}
@Override
public int menu() {
System.out.println("亲爱的会员:"+getName()+" 欢迎进入图书管理系统!");
System.out.println("1.借阅图书");
System.out.println("2.归还图书");
System.out.println("3.查找图书");
System.out.println("4.显示图书");
System.out.println("0.退出");
System.out.print("请输入进行的操作:->");
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
return score;
}
}
四、Test类
alMenu方法(各种功能的使用集合)
public static User alMenu() {
System.out.println("欢迎来到图书管理系统!");
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入身份:1 -> 图书管理员 2 -> 普通用户");
int indentity = scanner.nextInt();
scanner.nextLine();
if(1 == indentity) {
int frequency = 3;
while (frequency > 0) {
System.out.print("请输入密码:");
String mycode = scanner.nextLine();
if(mycode.equals(AdminUser.code)) {
break;
}
frequency --;
if(frequency == 0) {
System.out.println("退出程序!");
return null;
}
System.out.println("输入错误!您还有"+frequency+"次机会!");
}
return new AdminUser(name);
}else if(2 == indentity) {
return new NormalUser(name);
}
return null;
}
main方法
public static void main(String[] args) {
BookList bookList = new BookList();
User user = alMenu();
if(null == user){
System.exit(0);
}
while (true) {
int score = user.menu();
user.inplementInterface(score,bookList);
}
}
把Test类里的代码拼在一起
import Book.BookList;
import User.*;
import java.util.Scanner;
/**
* @Author YuanYuan
* @Date 2022/5/18
* @Time 10:55
*/
public class Test {
public static User alMenu() {
System.out.println("欢迎来到图书管理系统!");
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入身份:1 -> 图书管理员 2 -> 普通用户");
int indentity = scanner.nextInt();
scanner.nextLine();
if(1 == indentity) {
int frequency = 3;
while (frequency > 0) {
System.out.print("请输入密码:");
String mycode = scanner.nextLine();
if(mycode.equals(AdminUser.code)) {
break;
}
frequency --;
if(frequency == 0) {
System.out.println("退出程序!");
return null;
}
System.out.println("输入错误!您还有"+frequency+"次机会!");
}
return new AdminUser(name);
}else if(2 == indentity) {
return new NormalUser(name);
}
return null;
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = alMenu();
if(null == user){
System.exit(0);
}
while (true) {
int score = user.menu();
user.inplementInterface(score,bookList);
}
}
}
三、相关解释
-
解释一下
User
里面的public INterface[] iNterfaces;
就是创建一个里面全都存接口的数组。 -
解释一下下面两段代码:
管理员端里的:
this.iNterfaces = new INterface[]{
new ExitBook(),
new AddBook(),
new DelBook(),
new FindBook(),
new SeeBook()
};
普通用户端里的:
this.iNterfaces = new INterface[]{
new ExitBook(),
new BorrowBook(),
new ReturnBook(),
new FindBook(),
new SeeBook()
};
iNterface
是接口,里面存的类都实现了这个接口,相当于向上转型
就像这样:
- 把
main
函数中的
user.inplementInterface(score,bookList);
与User
中的inplementInterface
方法以及解释第2点结合解释
User
中的inplementInterface
方法:
public void inplementInterface(int score, BookList bookList) {
this.iNterfaces[score].work(bookList);
}
回看Test类:
我们知道通过我们的选择,返回了对应的类。
所以此时user
中存的就是我们返回的类,可能是AdminUser
类或者NormalUser
类,发生了向上转型。
此时user
中存的是子类,调用的是对应子类的menu()
,发生了动态绑定。
score
中存了我们对图书管理的功能的序号。
再执行下面这条语句,跳到user
中的inplementInterface
方法中,传参功能的序号和书架。
接下来解析this.iNterfaces[score].work(bookList);
语句
联合上面的一起总结: