public class Book {
private String name;
private String author;
private String type;
private int price;
private boolean isborrowed;
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
之所以用private修饰其书的属性,是为了体现书的密封性(书架中的属性也同理)
书架(BookList)
💕💕我们首先设置一个private权限的Book数组books,同时设置一个usedSize的变量,用于记录当前书架上存放了多少本书。我们写一个BookList的构造方法,同时将数组的长度初始化为10,然后通过快捷键设置userSize的getter and setter方法。注意不要使用编译器生成的Book数组books的getter and setter 方法,因为它针对的是books数组,并不是针对数组中存放的书,那并不是我们想要的。所以我们单独设置一个名为pos的数组下标参数,在getter方法中返回books的数组下标pos,在setter方法中,我们将 books数组的pos下标设置为一本书 。这样就能通过这两个方法去访问该数组的书成员。
现在请看以下代码:
复制代码
public class BookList {
private Book[] books;
private int usedsize;//书架上目前存放的书量
public BookList() {
this.books=new Book[10];
usedsize=4;
this.books[0] = new Book("三国演义","罗贯中",20,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.books[3]=new Book("Java","xx",18,"学习资料");
}
public int getUsedsize() {
return usedsize;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
public Book getBook(int i){
return books[i];
}
public void setBook(int i,Book book){
books[i]=book;
}
}
abstract public class User {
protected String name;
protected IOperation[] iOperations;
abstract public int menu();
public User(String name) {
this.name = name;
}
public void useoperation(int choice,BookList bookList){
IOperation iOperation = iOperations[choice];
iOperation.work(bookList);
}
}
管理员类(Administrator)
💕💕我们设置管理用户的菜单,再对应着菜单在构造方法中设置接口类型的数组的具体操作
复制代码
public class Administrator extends User{
public Administrator(String name) {
super(name);
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;
}
}
普通用户(NormalUser)
💕💕它和管理员类的操作大致相同,这里就不过多讲述了,直接上代码
复制代码
public class NormalUser extends User {
public NormalUser( String name){
super(name);
iOperations=new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new RetrunOperation()
};
}
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;
}
}
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("该操作进行中,请输入要查找的书");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedsize(); i++) {
if ((bookList.getBook(i).getName()).equals(name)) {
System.out.println("找到该书");
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("没找到该书");
}
}
显示图书 (ShowOperation)
❤️❤️ 遍历整个书架,然后挨个打印输出图书信息就可以了。
复制代码
public class ShowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("该操作进行中,将展示所有书的信息");
for (int i = 0; i < bookList.getUsedsize() ; i++) {
System.out.println(bookList.getBook(i));
}
}}
public class DeleteOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("该操作进行中,请输入要删除的书");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int deletesize=0;
for (int i = 0; i < bookList.getUsedsize(); i++) {
if ((bookList.getBook(i).getName()).equals(name)) {
deletesize=i;
break;
}
if(i== bookList.getUsedsize()-1)
{
System.out.println("该书架中不存在该书");
return;
}
}
for (int i = deletesize; i < bookList.getUsedsize()-1 ; i++) {
bookList.setBook(i,bookList.getBook(i+1));
}
bookList.setUsedsize(bookList.getUsedsize()-1);
System.out.println("删除"+name+"成功");
}}
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("系统已退出");
System.exit(0);//作用是结束该程序
}
}
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:管理员 ");
while (true) {
int choice = scanner.nextInt();
if (choice == 1) {
return new NormalUser(name);
} else if (choice == 2) {
return new Administrator(name);
} else {
System.out.println("只能输入1或2,请重新输入");
continue;
}
}
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user =login();
while (true){
int choice= user.menu();
user.useoperation(choice,bookList);
}
}