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=" + isBorrowed +
(isBorrowed == true ? "已经被借出" : "未被借出")+
'}';
}
}
BookList
public class BookList {
private static final int DEFAULT_SIZE = 10;
private Book[] books = new Book[DEFAULT_SIZE];
public BookList() {
books[0] = new Book("三国演义","罗贯中",56,"小说");
books[1] = new Book("西游记","吴承恩",60,"小说");
books[2] = new Book("红楼梦","曹雪芹",76,"小说");
this.usedSize = 3;
}
public Book getBook(int pos) {
return this.books[pos];
}
public void setBook (Book book) {
this.books[usedSize] = book;
}
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
private int usedSize;//记录当前books数组当中有多少本书
public void setBooks(int pos, Book book) {
books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
IOperation接口
public interface IOperation {
void work(BookList bookList);
}
AddOperation
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("输入价格:");
int price = scanner.nextInt();
System.out.println("输入类型:");
String type = scanner.nextLine();
Book book = new Book(name, author, price, type);
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
// System.out.println(bookList[i]); 不能这样写
Book tmp = bookList.getBook(i);
if (tmp.getName().equals(name)) {
System.out.println("已经存在这本书了,不能再存储了");
return;
}
}
bookList.setBook(book);
//修改usedSize
bookList.setUsedSize(currentSize + 1);
/*public void work(BookList bookList) {
System.out.println("新增图书");
}*/
}
}
BorrowedOperation
public class BorrowedOperation 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 currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name) &&
!book.isBorrowed() == false) {
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
}
}
DeleteOperation
public class DeleteOperation 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 currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize; i++) {
// System.out.println(bookList[i]); 不能这样写
Book tmp = bookList.getBook(i);
if (tmp.getName().equals(name)) {
index = i;
break;
}
}
for (int j = 0; j < currentSize-1; j++) {
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setUsedSize(currentSize-1);
//因为删除的是对象所以把最后一个置为null
bookList.setBook(currentSize-1,null);
}
}
ExitOperation
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
FindOperation
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 currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
System.out.println("找到了这本书:");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
/*public void work(BookList bookList) {
System.out.println("查找图书");
}*/
}
ReturnOperation
public class ReturnOperation 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 currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name) &&
!book.isBorrowed() == true) {
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
}
ShowOperation
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]); 不能这样写
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
Main
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();
while (true) {
int choice = user.menu();
//根据 choice 和 user 来确定调用哪个对象的哪个操作
user.doWork(choice, bookList);
}
}
}