import book.Book;
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User Login(){
//确认你的身份
System.out.println("请输如你的名字");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请输如你的身份,1.管理员 2.普通用户");
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();
user.doIoperation(choice,bookList);
}
}
}
2.user包
2.1User类
java复制代码
package user;
import book.BookList;
import ioperation.IOPeration;
public abstract class User {
protected String name;
public IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
2.2AdminUser类-----管理员
java复制代码
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DeleteOperation(),
new ShowOperation(),
};
}
@Override
public int menu() {
System.out.println("*****管理员菜单*****");
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;
}
}
2.3NormalUser类----普通成员
java复制代码
mport ioperation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioPerations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("*****普通用户菜单*****");
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;
}
}
3.book包
3.1Book类
java复制代码
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 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==true)?"已经借出":"未借出") +
'}';
}
}
3.2 BookList类
java复制代码
package book;
public class BookList {
//将书存在书架中
private Book[] books=new Book[10];//存10本书
private int usedSize;//记录书的数量
public BookList() {
this.books[0]=new Book("三国演义","罗贯中",18,"小说");
this.books[1]=new Book("西游记","吴承恩",8,"小说");
this.books[2]=new Book("红楼梦","曹雪芹",12,"小说");
this.usedSize=3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//获取数组的一个元素
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
this.books[pos]=book;
}
//获取整个数组
public Book[] getBooks() {
return books;
}
}