简易的图书管理系统中所包含的知识点主要为面向对象中的封装,继承,多态,接口,抽象类,包,顺序表的增删查改等技术。能够实现管理员的普通用户的对图书管理的部分功能。详细如下
Main.java
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
/**
* ClassName: Main
* Description:
* date: 2021/4/23 21:16
* @author wt
* @since JDK 1.8
*/
public class Main {
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请选择你的身份 1.管理员 2.普通用户 ");
int choice = scanner.nextInt();
if (choice == 1) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOpeartion(choice,bookList);
}
}
}
Book.java
package book;
/**
* ClassName: Book
* Description:书的属性
* date: 2021/4/23 15:02
* @author wt
* @since JDK 1.8
*/
public class Book {
private String name;//书名
private String author;//作者
private int price; //价格
private String type; //类型
private boolean flg; //是否借出
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 isFlg() {
return flg;
}
public void setFlg(boolean flg) {
this.flg = flg;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((flg == true)? "以借出" : "未借出")+
'}';
}
}
BookList.java
package book;
/**
* ClassName: BookList
* Description:
* date: 2021/4/23 15:03
* 书架
* @author wt
* @since JDK 1.8
*/
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList() {
books[0] = new Book("三国演义","罗贯中",56,"小说");
books[1] = new Book("西游记","吴承恩",77,"小说");
books[2] = new Book("水浒传","施耐庵",66,"小说");
this.usedSize = 3;
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book getBooks(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
IOpeartion .java定义的一个接口类
package operation;
import book.BookList;
/**
* ClassName: IOpeartion
* Description:
* date: 2021/4/23 20:25
* 接口
* @author wt
* @since JDK 1.8
*/
public interface IOpeartion {
void work(BookList bookList);
}
下面所以的对图书的操作类都继承这个接口。
Add.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Add
* Description:添加图书
* date: 2021/4/23 20:29
* 新增图书
* @author wt
* @since JDK 1.8
*/
public class Add implements IOpeartion {
@Override
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("新增图书");
System.out.println("请输入书名:");
String name = scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入类型:");
String type = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
int curSize = bookList.getUsedSize();
Book book = new Book(name,author,price,type);
int pos = bookList.getUsedSize();
bookList.setBooks(pos,book);
bookList.setUsedSize(curSize+1);
System.out.println("新增成功!");
}
}
Borrow.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Borrow
* Description:
* date: 2021/4/23 20:31
* 借阅图书
* @author wt
* @since JDK 1.8
*/
public class Borrow implements IOpeartion{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书");
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String bookname = scanner.nextLine();
System.out.println();
for (int i = 0; i < bookList.getUsedSize(); i++) {
//将i下标的这本书放到book里
Book book = bookList.getBooks(i);
//取出这个书的书名与要输入的书名bookname比较
if (book.getName().equals(bookname)) {
book.setFlg(true);
System.out.println("借阅成功!");
return;
}
}
//
System.out.println("没有这本书!!!");
}
}
Del.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Del
* Description:
* date: 2021/4/23 20:32
* 删除图书
* @author wt
* @since JDK 1.8
*/
public class Del implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.nextLine();
int pos = -1;
for (int i = 0; i <bookList.getUsedSize() ; i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(name)) {
pos = i;
break;
}
}
if (pos == -1) {
System.out.println("没有这本书!");
return;
}
//有效个数用curSize表示
int curSize = bookList.getUsedSize();
for (int i = pos; i < curSize-1; i++) {
//顺序表删除操作
//[i] = [i+1]
Book book = bookList.getBooks(i+1);
bookList.setBooks(i,book);
}
bookList.setUsedSize(curSize-1);
//curSize的一直没有变化,curSize-1始终表示最后一个有效个数的位置的下标
//置为空,因为books为数组,存放的为引用类型,需要释放内存
bookList.setBooks(curSize-1,null);
}
}
Display.java
package operation;
import book.BookList;
/**
* ClassName: Display
* Description:显示图书
* date: 2021/4/23 20:33
* 显示图书
* @author wt
* @since JDK 1.8
*/
public class Display implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("显示图书");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBooks(i));
}
}
}
Exit.java
package operation;
import book.BookList;
/**
* ClassName: Exit
* Description:
* date: 2021/4/23 20:34
* 退出系统
* @author wt
* @since JDK 1.8
*/
public class Exit implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
Find.java
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Find
* Description:查找图书
* date: 2021/4/23 20:35
*
* @author wt
* @since JDK 1.8
*/
public class Find implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String bookname = scanner.nextLine();
System.out.println();
for (int i = 0; i < bookList.getUsedSize(); i++) {
//将i下标的这本书放到book里
Book book = bookList.getBooks(i);
//取出这个书的书名与要输入的书名bookname比较
if (book.getName().equals(bookname)) {
System.out.println("找到这本书:");
System.out.println(book);
return;
}
}
//
System.out.println("没有这本书!!!");
}
}
Return.java
package operation;
import book.BookList;
import java.util.Scanner;
/**
* ClassName: Return
* Description:
* date: 2021/4/23 20:36
* 归还图书
* @author wt
* @since JDK 1.8
*/
public class Return implements IOpeartion {
@Override
public void work(BookList bookList) {
System.out.println("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的书名:");
String name = scanner.nextLine();
for (int i = 0; i <bookList.getUsedSize() ; i++) {
if (bookList.getBooks(i).getName().equals(name)) {
bookList.getBooks(i).setFlg(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有这本书!!!");
}
}
User.java
package user;
import book.BookList;
import operation.IOpeartion;
/**
* ClassName: User
* Description:
* date: 2021/4/23 19:22
* @author wt
* @since JDK 1.8
*/
public abstract class User {
public String name;
//1.准备一个接口数组 存储每一个对象
public IOpeartion[] IOpeartions;
public User(String name) {
this.name = name;
}
public abstract int menu();
//3.
public void doOpeartion(int choice, BookList bookList) {
this.IOpeartions[choice].work(bookList);
}
}
Admin.java
package user;
import operation.*;
import java.util.Scanner;
/**
* ClassName: AdminUser
* Description:
* date: 2021/4/23 20:40
* 管理员
* @author wt
* @since JDK 1.8
*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.IOpeartions = new IOpeartion[] {
new Exit(),
new Find(),
new Add(),
new Del(),
new Display(),
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
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("========================================");
int choice = scanner.nextInt();
return choice;
}
}
NormalUser.java
package user;
import operation.*;
import java.util.Scanner;
/**
* ClassName: NormalUser
* Description:
* date: 2021/4/23 20:41
* 普通用户
* @author wt
* @since JDK 1.8
*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.IOpeartions = new IOpeartion[] {
new Exit(),
new Find(),
new Borrow(),
new Return()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
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("========================================");
int choice = scanner.nextInt();
return choice;
}
}