要构建这样的一个系统,最难的其实就是找对象。让我们思考一下,在这样一个系统下,有哪些对象呢?最容易想到的,应该就是书和人。我们先从书开始考虑。
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;
}
为了打印出的格式与简介中所示一致,我们可以在Book类中重写toString方法:
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
( (isBorrowed == true) ? ",已借出" : ",未借出" )+
//", isBorrowed=" + isBorrowed +
'}';
}
原理:println打印时调用的是Object类中的toString方法,而所有类都默认继承了Object类,重写toString方法后,调用时发生动态绑定,调用子类重写的方法,导致打印格式改变。
public BookList() {
this.books = new Book[10];
this.usedSize = 0;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
接下来,我们就迎来了第一个难题:如何组织这些操作,让每一个操作都独立。
方法一:我们把每一个操作都写成一个类,需要时直接调用即可。
但是这种写法比较复杂,输入的不同需要使用多分支结构判断实现调用。
方法二:我们还可以定义一个接口IOPeration里面放入一个work方法(传入BookList),以便后面创建一个接口数组,在其中对应位置实例化对应的对象,实现类型的统一。
如下:
public interface IOPeration {
void work(BookList booklist);
}
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {
IOPeration ioPeration = this.ioPerations[choice];
ioPeration.work(bookList);
}
}
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
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;
}
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
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;
}
}
我们要创建一个login方法来登录,choice变量接收后,进行判断,根据值的不同返回AdminUser或NormalUser对象。
public class Main {
public static User login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
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 = new AdminUser(name);
// user = new NormalUser(name);
User user = login();
while (true) {//循环打印菜单
int choice = user.menu();
//根据你菜单返回的choice来执行对应的操作
user.doOperation(choice, bookList);
}
}
}