![包和类的放置位置](https://i-blog.csdnimg.cn/blog_migrate/206ef35ad731d7abaa5fd41da54de79a.png)
package package_bookmanager.book;
public class Book {
private String name;
private String id;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String id, String author,
int price, String type, boolean isBorrowed) {
this.name = name;
this.id = id;
this.author = author;
this.price = price;
this.type = type;
this.isBorrowed = isBorrowed;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
package package_bookmanager.book;
public class BookList {
private Book[] books = new Book[100];
private int size;
public BookList() {
books[0] = new Book("金瓶梅",
"001", "兰陵笑笑生", 100,
"古典名著", false);
books[1] = new Book("水浒传",
"002", "施耐庵", 100,
"古典名著", false);
books[2] = new Book("西游记",
"003", "吴承恩", 100,
"古典名著", false);
size = 3;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Book getBook(int index) {
return books[index];
}
public void setBook(int index, Book book) {
books[index] = book;
}
}
package package_bookmanager.operation;
import package_bookmanager.book.Book;
import package_bookmanager.book.BookList;
import java.util.Scanner;
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.next();
System.out.println("请输入序号:");
String id = scanner.next();
System.out.println("请输入作者: ");
String author = scanner.next();
System.out.println("请输入价格:");
int price = scanner.nextInt();
System.out.println("请输入类别: ");
String type = scanner.next();
Book book = new Book(name, id,
author, price, type, false);
bookList.setBook(bookList.getSize(), book);
bookList.setSize(bookList.getSize() + 1);
}
}
package package_bookmanager.operation;
import package_bookmanager.book.Book;
import package_bookmanager.book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借阅的书的编号: ");
String id = scanner.next();
for (int i = 0; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
if (!book.getId().equals(id)) {
continue;
}
if (book.isBorrowed()) {
System.out.println("这本书已经被借走了!");
break;
}
book.setBorrowed(true);
}
}
}
package package_bookmanager.operation;
import package_bookmanager.book.Book;
import package_bookmanager.book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的序号: ");
String id = scanner.next();
int i = 0;
for (; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getId().equals(id)) {
break;
}
}
if (i >= bookList.getSize()) {
System.out.println("未找到要删除的书籍!");
return;
}
Book lastBook = bookList.getBook(bookList.getSize() - 1);
bookList.setBook(i, lastBook);
bookList.setSize(bookList.getSize() - 1);
System.out.println("删除成功!");
}
}
package package_bookmanager.operation;
import package_bookmanager.book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("goodbye!");
System.exit(0);
}
}
package package_bookmanager.operation;
import package_bookmanager.book.Book;
import package_bookmanager.book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名: ");
String name = scanner.next();
int count = 0;
for (int i = 0; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
if (book.getName().equals(name)) {
System.out.println(book);
count++;
}
}
if (count == 0) {
System.out.println("没找到此书");
} else {
System.out.println("共计找到 " + count + " 本书!");
}
}
}
package package_bookmanager.operation;
import package_bookmanager.book.BookList;
public interface IOperation {
void work(BookList bookList);
}
package package_bookmanager.operation;
import package_bookmanager.book.BookList;
public class PrintAllOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("打印所有书籍信息");
for (int i = 0; i < bookList.getSize(); i++) {
System.out.println(bookList.getBook(i));
}
System.out.println("共计 " + bookList.getSize() + " 本书!");
}
}
package package_bookmanager.operation;
import package_bookmanager.book.Book;
import package_bookmanager.book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("归还书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的书的编号: ");
String id = scanner.next();
for (int i = 0; i < bookList.getSize(); i++) {
Book book = bookList.getBook(i);
if (!book.getId().equals(id)) {
continue;
}
if (!book.isBorrowed()) {
System.out.println("这本书已经被归还了!");
break;
}
book.setBorrowed(false);
}
}
}
```java
package package_bookmanager.user;
import package_bookmanager.operation.*;
import java.util.Scanner;
public class Admin extends User {
public Admin(String name) {
super(name);
operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new PrintAllOperation(),
};
}
@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("4. 打印所有信息");
System.out.println("0. 退出");
System.out.println("============");
System.out.println("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
package package_bookmanager.user;
import package_bookmanager.operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
operations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
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 package_bookmanager.user;
import package_bookmanager.book.BookList;
import package_bookmanager.operation.IOperation;
abstract public class User {
protected String name;
protected IOperation[] operations;
public User(String name) {
this.name = name;
}
abstract public int menu();
public void doOperation(int choice, BookList bookList) {
operations[choice].work(bookList);
}
}
package package_bookmanager;
import package_bookmanager.book.BookList;
import package_bookmanager.user.Admin;
import package_bookmanager.user.NormalUser;
import package_bookmanager.user.User;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while (true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.next();
System.out.println("请输入您的角色:(1 普通用户 2 管理员)");
int role = scanner.nextInt();
if (role == 1) {
return new NormalUser(name);
}
return new Admin(name);
}
}