该系统总共有以下这么多个部分
一、book文件夹下:
- Book中有每一本书得信息,与这些信息的Get和Set方法,还有对于书的toString方法
package 一月十八日.图书管理系统.book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean status;
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 isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((status == true) ? " 借出 " : " 未借出 ") +
'}';
}
}
- BookList这里相当于一个书架,其实是由顺序表的形式实现的
package 一月十八日.图书管理系统.book;
public class BookList {
private Book[] books;
private int usedSize;
public BookList() {
this.books = new Book[10];
books[0] = new Book("三国演义","罗贯中",72,"小说");
books[1] = new Book("西游记","吴承恩",52,"小说");
books[2] = new Book("水浒传","施耐庵",62,"小说");
this.usedSize = 3;
}
public void setBooks(int pos, Book book){
this.books[pos] = book;
}
public Book getBook(int pos){
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
二、Operation文件夹下:
- 这里实现了诸多方法,都是对顺序表进行操作的,且他们都是调用了
IOperation
这个接口
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.BookList;
public interface IOperation {
void work(BookList bookList);
}
- 这之后的 增删查改 都是对上面这个接口的work进行了重写,以实现对应的操作,这里我将它们写在一起,实际中应该独立出一个.java文件
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
import 一月十八日.图书管理系统.book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("添加书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图书的名字");
String name = scanner.nextLine();
System.out.println("请输入图书的作者");
String autor = scanner.nextLine();
System.out.println("请输入图书的价格");
int price = scanner.nextInt();
System.out.println("请输入图书的种类");
String type = scanner.next();
Book book = new Book(name,autor,price, type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize, book);
bookList.setUsedSize(currentSize + 1);
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
import 一月十八日.图书管理系统.book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除图书的名字");
String name = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
break;
}
}
if(i >= bookList.getUsedSize()){
System.out.println("没有要删除的书");
return;
}
int pos = i;
for(int j = pos; j < bookList.getUsedSize() - 1; j++){
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
int currenSize = bookList.getUsedSize();
bookList.setUsedSize(currenSize - 1);
System.out.println("删除完毕");
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
import 一月十八日.图书管理系统.book.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.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
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++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
import 一月十八日.图书管理系统.book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@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++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有这本书!");
book.setStatus(true);
return;
}
}
System.out.println("没有您要借阅的书!");
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.Book;
import 一月十八日.图书管理系统.book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@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++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("有这本书!");
book.setStatus(false);
return;
}
}
System.out.println("没有您要归还的这本书!");
}
}
package 一月十八日.图书管理系统.opteration;
import 一月十八日.图书管理系统.book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(1);
}
}
三、user文件夹下:
- 该文件下放了一个父类User与AdminUser与NormalUser两个子类
- 用户类
package 一月十八日.图书管理系统.user;
import 一月十八日.图书管理系统.book.BookList;
import 一月十八日.图书管理系统.opteration.IOperation;
public abstract class User {
public String name;
public User(String name){
this.name = name;
}
public abstract int menu();
public IOperation[] operations;
public void doOperation (int choice, BookList bookList){
this.operations[choice].work(bookList);
}
}
package 一月十八日.图书管理系统.user;
import 一月十八日.图书管理系统.opteration.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("管理员菜单");
System.out.println("================================");
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;
}
}
package 一月十八日.图书管理系统.user;
import 一月十八日.图书管理系统.opteration.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("普通用户菜单");
System.out.println("================================");
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;
}
}
四、main函数部分:
package 一月十八日.图书管理系统;
import 一月十八日.图书管理系统.book.BookList;
import 一月十八日.图书管理系统.user.AdminUser;
import 一月十八日.图书管理系统.user.NormalUser;
import 一月十八日.图书管理系统.user.User;
import java.util.Scanner;
public class Main {
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scanner.nextLine();
System.out.println("请输入你的身份:1->管理员 0->普通用户");
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.doOperation(choice, bookList);
}
}
}