package Book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean status;//状态(是否借出)
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 isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", status=" + ( (status == true) ? " 借出 " : " 未借出 ")+
'}';
}
}
package Book;
import java.security.PublicKey;
public class BookList {
private Book[] books;
private int usedSize;
public BookList() {
this.books=new Book[100];
books[0] = new Book("三国演义","罗贯中",62,"小说");
books[1] = new Book("西游记","吴承恩",56,"小说");
books[2] = new Book("水浒传","施耐庵",78,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {
return this.books[pos];
}
public void setBooks(int pos,Book book){
this.books[pos]=book;
}
}
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Add implements OperationJieKou{
@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.next();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
}
}
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Borrow implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“借阅书籍”);
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入要借阅的书名”);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
//说明有这本书!
//逻辑:把这本书的借阅状态修改为true
book.setStatus(true);
System.out.println(book);
return;
}
}
System.out.println("没有你要借阅的图书!");
}
}
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Delete implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“删除书籍”);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书名");
String name = scanner.nextLine();
int i = 0;
for (; i < bookList.getUsedSize(); i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
break;
}
}
if(i >= bookList.getUsedSize()) {
System.out.println("没有要删除的这本书!");
return;
}
int pos = i;
for(int j = pos;j < bookList.getUsedSize()-1;j++) {
Book book = bookList.getBook(j+1);
bookList.setBooks(j,book);
}
int currentSize = bookList.getUsedSize();
bookList.setUsedSize(currentSize-1);
System.out.println("删除书籍完毕!");
}
}
package Operation;
import Book.BookList;
public class Exit implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“退出系统!”);
System.exit(1);
}
}
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Find implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“查找书籍”);
Scanner scanner=new Scanner(System.in);
System.out.println(“请输入要查找的书名”);
String name=scanner.nextLine();
for (int i=0;i<bookList.getUsedSize();i++){
Book book=bookList.getBook(i);
if (book.getName().equals(name)){
System.out.println(“有这本书”);
System.out.println(book);
return;
}
}
System.out.println(“没有这本书”);
}
}
package Operation;
import Book.BookList;
public interface OperationJieKou {
void Work(BookList bookList);
}
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class Return implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“归还书籍”);
Scanner scanner=new Scanner(System.in);
System.out.println(“请输入要归还的书名”);
String name=scanner.nextLine();
for (int i=0;i<bookList.getUsedSize();i++){
Book book=bookList.getBook(i);
if (book.getName().equals(name)){
book.setStatus(false);
System.out.println(“归还成功”);
System.out.println(book);
return;
}
}
System.out.println(“没有你要归还的这本书”);
}
}
package Operation;
import Book.Book;
import Book.BookList;
public class Show implements OperationJieKou{
@Override
public void Work(BookList bookList) {
System.out.println(“展示图书!”);
for (int i=0;i<bookList.getUsedSize();i++){
Book book= bookList.getBook(i);
System.out.println(book);
}
}
}
package User;
import Operation.*;
import java.util.Scanner;
public class Maneger extends User {
public Maneger(String name) {
super(name);
this.operations = new OperationJieKou[]{
new Exit(),
new Find(),
new Add(),
new Delete(),
new Show()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("=");
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(”=");
int choice = scanner.nextInt();
return choice;
}
}
package User;
import Operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.operations=new OperationJieKou[]{new Exit(),new Find(),new Borrow(),new Return()};
}
@Override
public int menu() {
Scanner scanner =new Scanner(System.in);
System.out.println("===============================");
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("===============================");
int choice=scanner.nextInt();
return choice;
}
}
package User;
import Book.BookList;
import Operation.OperationJieKou;
public abstract class User {
public String name;
public OperationJieKou[] operations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.operations[choice].Work(bookList);
}
}
import Book.BookList;
import User.User;
import User.Maneger;
import User.NormalUser;
import java.util.Scanner;
public class Test {
public static User login(){
Scanner scanner=new Scanner(System.in);
System.out.println(“请输入您的姓名”);
String name=scanner.nextLine();
System.out.println(“请输入您的身份(1>>管理员 0>>普通用户)”);
int choice=scanner.nextInt();
if(choice == 1) {
return new Maneger(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
//1、登录 user 会引用 一个对象 这个对象可能是AdminUser NormalUser
User user = login();
while (true) {
//会调用user引用的对象的 menu方法,返回对应的选择
int choice = user.menu();
//拿choice去调用对应类的 work 方法
//这个work方法,调用谁的 ,怎么调用,交给doOperation
user.doOperation(choice,bookList);
}
}
}