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 this.name; } public void setName(String name) { this.name = name; } public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return this.price; } public void setPrice(int price) { this.price = price; } public String getType() { return this.type; } public void setType(String type) { this.type = type; } public void setBorrowed(boolean borrowed) { this.isBorrowed = borrowed; } public boolean isBorrowed() { return this.isBorrowed; } public String toString() { return "Book{name='" + this.name + '\'' + ", author='" + this.author + '\'' + ", price=" + this.price + ", type='" + this.type + '\'' + "," + (this.isBorrowed ? "已经被借出" : "未被借出") + '}'; } }
BookList
package Book; public class BookList { private Book[] books = new Book[10]; private int size; public BookList() { this.books[0] = new Book("三国演义", "罗贯中", 20, "小说"); this.books[1] = new Book("挪威的森林", "村上春树", 100, "小说"); this.books[2] = new Book("活着", "余华", 20000, "小说"); this.size = 3; } public Book getBook(int pos) { return this.books[pos]; } public void setBook(Book book, int pos) { this.books[pos] = book; } public int getSize() { return this.size; } public void setSize(int size) { this.size = size; } }
Operation
public class AddOperation implements IOperation { public AddOperation() { } 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("请输入价格:"); int price = scanner.nextInt(); scanner.nextLine(); System.out.println("请输入类型:"); String type = scanner.nextLine(); Book newbook = new Book(name, author, price, type); bookList.setBook(newbook, bookList.getSize()); bookList.setSize(bookList.getSize() + 1); System.out.println("添加成功!"); } } public class BorrowOperation implements IOperation { public BorrowOperation() { } public void work(BookList bookList) { System.out.println("借阅图书"); System.out.println("请输入你要借阅的图书:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int size = bookList.getSize(); for(int i = 0; i < size; ++i) { Book book = bookList.getBook(i); if (book.getName().equals(name) && !book.isBorrowed()) { book.setBorrowed(true); System.out.println("借阅成功!"); return; } } System.out.println("您所要借阅的书不存在或已经被借出"); } } public class DelOperation implements IOperation { public DelOperation() { } public void work(BookList bookList) { System.out.println("删除图书!"); int size = bookList.getSize(); if (size == 0) { System.out.println("书架为空,无法删除!!"); } else { System.out.println("请输入你要删除的书:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int index = -1; int i; Book book2; for(i = 0; i < size; ++i) { book2 = bookList.getBook(i); if (book2.getName().equals(name)) { index = i; break; } } if (index == -1) { System.out.println("没有你要删除的书"); } else { for(i = index; i < size - 1; ++i) { book2 = bookList.getBook(i + 1); bookList.setBook(book2, i); } bookList.setSize(size - 1); System.out.println("删除成功"); } } } } public class ExitOperation implements IOperation { public ExitOperation() { } public void work(BookList bookList) { int size = bookList.getSize(); for(int i = 0; i < size; ++i) { bookList.setBook((Book)null, i); } bookList.setSize(0); System.exit(0); } } public class FindOperation implements IOperation { public FindOperation() { } public void work(BookList bookList) { System.out.println("查询图书!"); System.out.println("请输入你要查找的书:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int size = bookList.getSize(); for(int i = 0; i < size; ++i) { Book book = bookList.getBook(i); if (book.getName().equals(name)) { System.out.println(book); return; } } System.out.println("查无此书!"); } } import Book.BookList; public interface IOperation { void work(BookList var1); } public class ReturnOperation implements IOperation { public ReturnOperation() { } public void work(BookList bookList) { System.out.println("归还图书!"); System.out.println("请输入你要归还的图书:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int size = bookList.getSize(); for(int i = 0; i < size; ++i) { Book book = bookList.getBook(i); if (book.getName().equals(name) && book.isBorrowed()) { book.setBorrowed(false); System.out.println("归还成功!"); return; } } System.out.println("您所要归还的图书已经被归还"); } } public class ShowOperation implements IOperation { public ShowOperation() { } public void work(BookList bookList) { int size = bookList.getSize(); for(int i = 0; i < size; ++i) { Book book = bookList.getBook(i); System.out.println(book); } } }
User
public class AdminUser extends User { IOperation[] iOperations = new IOperation[]{new ExitOperation(), new FindOperation(), new DelOperation(), new AddOperation(), new ShowOperation()}; public AdminUser(String name) { super(name); } public int menu() { System.out.println("============================"); System.out.println("Hello " + this.name + " 欢迎来到图书管理系统!"); System.out.println("请输入你要进行的操作:\n1.查询图书\n2.删除图书\n3.添加图书\n4.显示图书\n0.退出系统"); 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); } } package User; import Book.BookList; import Operation.BorrowOperation; import Operation.ExitOperation; import Operation.FindOperation; import Operation.IOperation; import Operation.ReturnOperation; import java.util.Scanner; public class NormalUser extends User { IOperation[] iOperations = new IOperation[]{new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation()}; public NormalUser(String name) { super(name); } public int menu() { System.out.println("============================"); System.out.println("Hello " + this.name + " 欢迎来到图书管理系统!"); System.out.println("请输入你要进行的操作:\n1.查询图书\n2.借阅图书\n3.归还图书\n0.退出系统"); 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); } } public abstract class User { protected 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); } }