通过使用JavaSE写一个简单的图书管理系统,做一个记录,不足之处欢迎大家留言指导。
🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀🎀
1.该图书管理系统分为管理员使用和普通用户使用两个方向,
目录如下
管理员用例测试:
普通用户测试用例:
代码:
接口IOperation:
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
Book类
package book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出
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 isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed == true)?"已经借出":"未借出")+
'}';
}
}
BookList类
package book;
public class BookList {
//最多可以放10本书
private Book[] books = new Book[10];
private int usedSize;//实时记录 当前books这个数组存放了几本书
public BookList() {
books[0] = new Book("三国演义", "罗贯中", 19, "小说");
books[1] = new Book("西游记", "吴承恩", 29, "小说");
books[2] = new Book("红楼梦", "曹雪芹", 9, "小说");
usedSize = 3;
}
/**
*
* @param pos 此时pos位置一定是合法的
* @return
*/
public Book getBook(int pos){
return books[pos];
}
/**
*
* @param pos 此时pos位置一定是合法的
* @param book 是你要放的书
*/
public void setBooks(int pos,Book book){
books[pos] = book;
}
/**
* 实时获取当前的 书的个数
* @return
*/
public int getUsedSize(){
return usedSize;
}
/**
* 实时的修改当前书架上 书的个数
*/
public void setUsedSize(int size){
usedSize = size;
}
//理论上来说 这些操作书架的函数 都应该写在这里面
/* public void add(){
//book
}*/
}
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("新增图书!");
Scanner scanner = new Scanner(System.in);
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();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增书籍成功!");
}
}
BorrowOperation类
package operation;
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();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
// Book book = bookList[i];
if (book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
}
}
DelOPeration类
package operation;
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 currentSize = bookList.getUsedSize();
int index = -1;
int i = 0;
for ( i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
// Book book = bookList[i];
if (book.getName().equals(name)){
index = i;
break;
}
}
//1.i>=currentSize 说明没有这本书
if (i>=currentSize){
System.out.println("没有这本书!");
return;
}
//2.break; 说明有这本书 这本书的下标存储在index里
for (int j = index; j <currentSize-1 ; j++) {
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentSize-1,null);//删除的书籍进行移动,最后的地方置为Null
bookList.setUsedSize(currentSize-1);//有效的书籍少一
System.out.println("删除成功!");
}
}
DisplayOperation类
package operation;
import book.Book;
import book.BookList;
public class DisplayOperation 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);
}
}
}
ExitOperation类
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//状态码,等同于return 0; return 1;
}
}
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);
System.out.println("请输入图书的名字");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
// Book book = bookList[i];
if (book.getName().equals(name)){
System.out.println("找到了这本书!");
System.out.println(book);
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("归还图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入归还图书的名字");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
// Book book = bookList[i];
if (book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
}
}
AdminUser类
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[ ]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
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("请输入你的操作: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
NormalUser类
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
//alt+insert快捷键出Generate创建构造方法
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[ ]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
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("请输入你的操作: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
User类
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;//用户名
protected IOperation[] iOperations;//此时并没有初始化和分配大小
public User(String name) {//右键Generate->constructor创建构造方法快捷
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
Main类
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {//返回值亮了
System.out.println("请输入你的姓名: ");
Scanner scanner = new Scanner(System.in);
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 user = login();
while (true) {
int choice = user.menu();//动态绑定
user.doOperation(choice, bookList);//user可能有两种对象q
}
}
}