首先让我们看一下这个代码的功能!
我们可以清楚的发现这个代码 有两个角色 管理员 普通用户 而且他们所能 实现的能力不同!
那我们实现分为几个part 书 方法 用户 整合(main)
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean borrow;
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 getBorrow() {
return borrow;
}
public void setBorrow(boolean borrow) {
this.borrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +","+
((borrow == true)?" 已经借出":" 未被借出") +
'}';
}
}
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList(){
books[0] = new Book("微积分","小明",49,"学习");
books[1] = new Book("线性代数","小张",79,"学习");
books[2] = new Book("离散数学","小芳",66,"学习");
usedSize = 3;
}
//位置的书
public Book getBook(int pos){
return books[pos];
}
//你要放的书
public void setBook(int pos,Book book){
books[pos] = book;
}
//多少书
public int getUsedSize() {
return usedSize;
}
//修改书的个数
public void setUsedSize(int size) {
usedSize = size;
}
}
public interface IOperation {
void work(BookList bookList);
}
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入你想查找的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int current = bookList.getUsedSize();
for (int i = 0; i < current; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到这本书了!");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的这本书");
}
}
public abstract class User {
protected String name;
protected IOperation[] iOperations;
public User(String name) {
this.name = name;
}
abstract int menu();
public void doOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
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("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;
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelectOperation(),
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 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 = login();
//User user == new AdminUser(name);1
//User user == new NormalUser(name);0
while(true){
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}
总的来说 应该都能看懂 但有一个part 我觉得可以说说
public static void main(String[] args) { BookList bookList = new BookList(); User user = login(); while(true){ //根据身份打印菜单选择方法 int choice = user.menu(); user.doOperation(choice,bookList); } }
public void doOperation(int choice, BookList bookList){ this.iOperations[choice].work(bookList); }
上图片!直观一点
红红火火恍恍惚惚哈哈哈哈哈 520 我突然发现了图书馆的更有趣的方式