前言
在本篇博客当中,我们会使用Java基础语法来简单实现一个图书管理系统,主要用到的知识为:封装、多态、继承、接口等等,并不会使用数据库来存储数据,请注意
需求
1. 要求设置管理员和普通用户两种身份,在登陆时就会提示用户选择
2. 管理员的菜单界面如下:
3. 普通用户的菜单界面如下:
模块实现和代码演示
book包:book类和BookList类
package book;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isLend;
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isLend() {
return isLend;
}
public void setLend(boolean lend) {
isLend = lend;
}
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
(isLend ? " 已借出" : " 未借出")+
'}';
}
}
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public BookList() {
books[0] = new Book("平凡的世界","路遥",98,"长篇小说");
books[1] = new Book("活着","余华",31,"长篇小说");
books[2] = new Book("白夜行","【日】东野圭吾",30,"长篇小说");
books[3] = new Book("小王子","【法】圣·埃克苏佩里",12,"短篇童话");
this.usedSize = 4;
}
public Book getBook(int pos){
return books[pos];
}
public boolean ifFull() {
if(this.usedSize == books.length) {
return true;
} else {
return false;
}
}
//新增图书的方法
public void setBook(int pos, Book book) {
books[pos] = book;
}
}
user包:管理员和普通用户
Uesr接口:
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
public String name;
public IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
管理员:AdminUser
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(), //0
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu() {
System.out.println("=====管理员菜单=====");
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; //返回出去让里面知道是选了哪种操作
}
}
普通用户:RegularUser
package user;
import operation.*;
import java.util.Scanner;
public class RegularUser extends User{
public RegularUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(), //0
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
public int menu() {
System.out.println("=====普通用户菜单=====");
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; //返回出去让里面知道是选了哪种操作
}
}
operation包:对图书的各种操作
新增图书:AddOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书...");
if (bookList.ifFull()) {
System.out.println("书架已经满了,不能新增了!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要新增图书的书名:");
String bookName = scanner.nextLine();
System.out.println("请输入你要新增图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入你要新增图书的价格:");
int price = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入你要新增图书的类型:");
String type = scanner.nextLine();
Book book = new Book(bookName,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBook(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增图书成功!");
}
}
查找图书: FindOperation
package operation;
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);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
System.out.println("找到这本书了!");
System.out.println(book);
return;
}
}
System.out.println("没有你想找的书~~~");
}
}
删除图书:DelOperation
package operation;
import book.Book;
import book.BookList;
import org.w3c.dom.ls.LSOutput;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书...");
System.out.println("请输入你想要删除的图书的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int pos = -1; //记录下标
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
//找到了,记录下该书的下标
pos = i;
break;
}
}
if (pos == -1) {
//没找到
System.out.println("没有你要删的书");
return;
}
//找到了,开始删除书
for (int j = pos; j < currentSize - 1; j++) {
//拿后一本书放到要删的书的位置上
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
//修改usedSize
bookList.setUsedSize(currentSize-1);
//把最后的位置置为空
bookList.setBook(currentSize-1,null);
System.out.println("删除成功~~~");
}
}
显示图书:ShowOperation
package operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书...");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
借阅图书:BorrowedOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书...");
System.out.println("请输入你要借阅的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
book.setLend(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("借阅失败~~~");
}
}
归还图书:ReturnOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书...");
System.out.println("请输入你要归还的书名:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(bookName)) {
book.setLend(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("归还失败~~~");
}
}
退出系统:ExitOperation
package operation;
import book.Book;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统...");
//把所有书置空
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBook(i,null);
}
System.exit(0);
}
}
Main测试
import book.BookList;
import user.AdminUser;
import user.RegularUser;
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:管理员 2.普通用户");
int choice = scanner.nextInt();
if (choice == 1) {
AdminUser adminUser = new AdminUser(name);
return adminUser;
} else {
RegularUser regularUser = new RegularUser(name);
return regularUser;
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}
成果展示