不同的用户有不同的菜单,然后进行操作。?
通过需求我们可以提取图书类,书架类
图书类存放图书的基本信息,书架类存放书本及其它的数量,以及操作图书的方法等等。
接口类(用来操作书架里面的图书)-》写具体的操作实现类其次提取出用户类,用户类分为普通用户类 和 管理员类
基本框架代码:
package book;
/**
* @Auther: TheMyth
* @Date: 2022-11-16 - 11 - 16 - 20:59
* @Description: com.bookmanagement.book
* @version: 1.0
*/
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) {//这里没构造isBorrowed是因为它的默认值是false,意味着刚开始书都是未被借出的
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=" + isBorrowed +
'}';
}
}
package book;
/**
* @Auther: TheMyth
* @Date: 2022-11-17 - 11 - 17 - 23:55
* @Description: com.bookmanagement.book
* @version: 1.0
*/
public class BookList {//书架类
public static final int DEFAULT_SIZE = 10;
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize;//当前books数组当中的数量
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:37
* @Description: operate
* @version: 1.0
*/
public interface Ioperation {//图书的操作接口
void work(BookList bookList);//操作书架类中的书数组
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:48
* @Description: operate
* @version: 1.0
*/
public class AddOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:53
* @Description: operate
* @version: 1.0
*/
public class BrrowOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:50
* @Description: operate
* @version: 1.0
*/
public class DeleteOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:51
* @Description: operate
* @version: 1.0
*/
public class ExitOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:49
* @Description: operate
* @version: 1.0
*/
public class FindOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:52
* @Description: operate
* @version: 1.0
*/
public class ReturnOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:51
* @Description: operate
* @version: 1.0
*/
public class ShowOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("显示图书!");
}
}
package user;
import book.BookList;
import operate.Ioperation;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:34
* @Description: user
* @version: 1.0
*/
public abstract class User {
protected String name;
protected Ioperation[] ioperations;//操作方法的接口数组,存放操作方法的实现类
public User(String name) {
this.name = name;
}
//因为菜单在这儿不需要具体的实现,所以将菜单定义为抽象方法,自然User也变成了抽象类
public abstract int menu();
//通过用户的选择,来实现不同的操作,调用接口数组里面的操作实现类的方法
public void doWork(int choice, BookList bookList) {
this.ioperations[choice].work(bookList);//通过接口数组下标得到具体的操作实现类,再调用操作类的方法
}
}
package user;
import operate.*;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:35
* @Description: user
* @version: 1.0
*/
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
/*this.ioperations = new Ioperation[4];
ioperations[0] = new ExitOperation();
ioperations[1] = new FindOperation();
ioperations[2] = new BrrowOperation();
ioperations[3] = new ReturnOperation();*/
//动态初始化接口数组的变量 (接口数组存放操作的实现类)
this.ioperations = new Ioperation[]{
new ExitOperation(),
new FindOperation(),
new BrrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("??*********???**********??");
System.out.println("hello🌻 " + name + " 欢迎来到图书管理系统🕌");
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;
}
}
package user;
import operate.*;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:34
* @Description: user
* @version: 1.0
*/
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.ioperations = new Ioperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DeleteOperation(),
new ShowOperation()
};
}
@Override
public int menu() {//因为要根据不同的输入,做不同的操作,所以返回值用int接受
System.out.println("??*********???**********??");
System.out.println("hello🌻 " + name + " 欢迎来到图书管理系统🕌");
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;
}
}
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 9:53
* @Description: PACKAGE_NAME
* @version: 1.0
*/
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 (1 == choice) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();//根据返回的不同用户,调用不同的菜单
while (true) {
int choice = user.menu();//接受操作数,根据操作数调用不同的操作方法
//根据 choice 和 user 来确定调用哪个对象的哪个操作
user.doWork(choice, bookList);
}
}
}
此时的框架已经搭建好了,只需要写具体操作实现类的业务逻辑。
Book类的toString方法修改:
BookList类新增getBook方法:?
ShowOperation类修改:
ExitOperation类修改:?
FindOperation类修改:?
BookList类新增setBook方法:
AddOpration类:
BookLisk类重载setBook方法:
DeleteOperation类:
?
BorrowOperation类:?
?
ReturnOperation类:
package book;
/**
* @Auther: TheMyth
* @Date: 2022-11-16 - 11 - 16 - 20:59
* @Description: book
* @version: 1.0
*/
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) {//这里没构造isBorrowed是因为它的默认值是false,意味着刚开始书都是未被借出的
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=" + isBorrowed +
(isBorrowed == false ? " 未被借出" : " 已被借出") +
'}';
}
}
package book;
/**
* @Auther: TheMyth
* @Date: 2022-11-17 - 11 - 17 - 23:55
* @Description: book
* @version: 1.0
*/
public class BookList {//书架类
public static final int DEFAULT_SIZE = 10;
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize;//当前books数组当中的数量
public BookList() {
books[0] = new Book("数据结构", "张三", 40, "计算机类");
books[1] = new Book("计算机组成原理", "李四", 45, "计算机类");
books[2] = new Book("操作系统", "王五", 50, "计算机类");
books[3] = new Book("计算机网络", "themyth", 35, "计算机类");
books[4] = new Book("Java程序设计", "Holis", 25, "计算机类");
this.usedSize = 5;
}
//因为存放Book类的数组是被封装的,所以要提供方法来获取书,通过下标pos得到具体的一本书
public Book getBook(int pos) {
return this.books[pos];//返回的是下标为pos的书
}
//在书架的最后一本书位置后面接着存放一本书
public void setBook(Book book) {
this.books[usedSize] = book;//[0, usedSize - 1]这个区间是原来每本书的下标
}
//在书架的指定位置增加一本书
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:37
* @Description: operate
* @version: 1.0
*/
public interface Ioperation {//图书的操作接口
void work(BookList bookList);//操作书架类中的书数组
}
package operate;
import book.Book;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:51
* @Description: operate
* @version: 1.0
*/
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++) {
//System.out.println(bookList[i]);bookList只是一个引用,不是数组
Book book = bookList.getBook(i);//通过下标的变换,得到不同的书
System.out.println(book);//因为这里重写了toString,可以直接打印书的详细信息
}
}
}
package operate;
import book.BookList;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:51
* @Description: operate
* @version: 1.0
*/
public class ExitOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//退出JVM
}
}
package operate;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:49
* @Description: operate
* @version: 1.0
*/
public class FindOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的书名:");
String findBookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (findBookName.equals(book.getName())) {
System.out.println("找到了这本书!");
System.out.println(book);
return;
}
}
System.out.println("查无此书!");
}
}
package operate;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:48
* @Description: operate
* @version: 1.0
*/
public class AddOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String addBookName = scanner.nextLine();
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
System.out.println("请输入类型:");
String type = scanner.next();//注意这儿不能用nextLine(),因为会读取上面的回车符,导致不能录入类型
Book book = new Book(addBookName, author, price, type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmpBook = bookList.getBook(i);
if (addBookName.equals(tmpBook.getName())) {
System.out.println("已经有这本书了,不能存入了!");
return;
}
}
bookList.setBook(book);
//修改usedSize
bookList.setUsedSize(currentSize + 1);
}
}
package operate;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:50
* @Description: operate
* @version: 1.0
*/
public class DeleteOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书名:");
String deleteBookName = scanner.nextLine();
int index = -1;
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmpBook = bookList.getBook(i);
if (deleteBookName.equals(tmpBook.getName())) {
index = i;
break;
}
}
if (index != -1) {
for (int j = index; j < currentSize - 1; j++) {//j <= currentSize - 2
//bookList[j] = bookList[j + 1];
Book book = bookList.getBook(j + 1);
bookList.setBook(j, book);
}
//书本数量减少一本
bookList.setUsedSize(currentSize - 1);
//因为删除的是对象,要把最后一个元素置为null
bookList.setBook(currentSize - 1, null);
System.out.println("删除成功!");
} else {
System.out.println("这本书不存在,无法删除!");
}
}
}
package operate;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:53
* @Description: operate
* @version: 1.0
*/
public class BrrowOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("输入你要借阅的图书:");
Scanner scanner = new Scanner(System.in);
String borrowBookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmpbook = bookList.getBook(i);
if (borrowBookName.equals(tmpbook.getName()) && !tmpbook.isBorrowed()) {//tmpbook.isBorrowed() == false
tmpbook.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
/*if (borrowBookName.equals(bookList.getBook(i).getName()) && !bookList.getBook(i).isBorrowed()) {
bookList.getBook(i).setBorrowed(true);
System.out.println("借阅成功!");
}*/
}
System.out.println("没有这本书!");
}
}
package operate;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:52
* @Description: operate
* @version: 1.0
*/
public class ReturnOperation implements Ioperation {
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("输入你要归还的图书:");
Scanner scanner = new Scanner(System.in);
String returnBookName = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book tmpbook = bookList.getBook(i);
if (returnBookName.equals(tmpbook.getName()) && tmpbook.isBorrowed()) {//tmpbook.isBorrowed() == ture
tmpbook.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
}
}
package user;
import book.BookList;
import operate.Ioperation;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:34
* @Description: user
* @version: 1.0
*/
public abstract class User {
protected String name;
protected Ioperation[] ioperations;//接口数组存放操作的实现类
public User(String name) {
this.name = name;
}
//因为菜单在这儿不需要具体的实现,所以将菜单定义为抽象方法,自然User也变成了抽象类
public abstract int menu();
//通过用户的选择,来实现不同的操作,调用接口数组里面的操作实现类的方法
public void doWork(int choice, BookList bookList) {
this.ioperations[choice].work(bookList);//通过接口数组下标得到具体的操作实现类,再调用操作类的方法
}
}
package user;
import operate.*;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:35
* @Description: user
* @version: 1.0
*/
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
/*this.ioperations = new Ioperation[4];
ioperations[0] = new ExitOperation();
ioperations[1] = new FindOperation();
ioperations[2] = new BrrowOperation();
ioperations[3] = new ReturnOperation();*/
//动态初始化接口数组的变量 (接口数组存放操作的实现类)
this.ioperations = new Ioperation[]{
new ExitOperation(),
new FindOperation(),
new BrrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("??*********???**********??");
System.out.println("hello🌻 " + name + " 欢迎来到图书管理系统🕌");
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;
}
}
package user;
import operate.*;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 0:34
* @Description: user
* @version: 1.0
*/
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.ioperations = new Ioperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DeleteOperation(),
new ShowOperation()
};
}
@Override
public int menu() {//因为要根据不同的输入,做不同的操作,所以返回值用int接受
System.out.println("??*********???**********??");
System.out.println("hello🌻 " + name + " 欢迎来到图书管理系统🕌");
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;
}
}
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
/**
* @Auther: TheMyth
* @Date: 2022-11-18 - 11 - 18 - 9:53
* @Description: PACKAGE_NAME
* @version: 1.0
*/
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 (1 == choice) {
return new AdminUser(name);
} else {
return new NormalUser(name);
}
}
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();//根据返回的不同用户,调用不同的菜单
while (true) {
int choice = user.menu();//接受操作数,根据操作数调用不同的操作方法
//根据 choice 和 user 来确定调用哪个对象的哪个操作
user.doWork(choice, bookList);
}
}
}