目录
详解:
BookList类:
InOperation接口
User类(父类) 和 Main类(这俩要一起看)
完整代码
book包
Book类
BookList类
operation包
AddBook类
BorrowBook类
DeleteBook类
FindBook类
PrintBook类
ReturnBook类
Exit类
InOperation接口
user包
Domestic类
Administrator类
User类
Main类
详解:
按照上图逻辑来写, 因为要运行,所以要有个main函数
接下来我们来串联整个思路
接下来细说BookList类,InOperation接口和User类
BookList类:
1.先创建一个Book类 类型的数组(private 封装)
2.再创建个书的个数的变量 (private 封装),然后创建构造方法,初始化成员(图书)(实例化BookList时直接就运行书架里的内容)(实例化一个Book对象,有参类型,所以Book类哪里要先创建有参的构造方法)
既然封装了就要有Getter和Setter (下面系统自动生成步骤)
当然自动生成的和我们要的还是有点差别的,我们要稍微改动一下
InOperation接口
因为每一个操作功能都要有对应的工作,所以干脆创建个接口 ,对代码更好的规范
User类(父类) 和 Main类(这俩要一起看)
各个包类都创建好了,但是他们如果串联运行呢,要有main才能运行起来,怎么搞,往下看
(上面可能看不太清,可以对着下面看一下,或者对着代码看)
剩下的方法实现起来都蛮简单的,对比着代码看看就能懂,不多说了 😊😊
完整代码
book包
Book类
package book;public class Book {private String name;private String author;private double price;private String type;private boolean borrowed;//默认falsepublic Book(String name, String author, double 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 double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return borrowed;}public void setBorrowed(boolean borrowed) {this.borrowed = borrowed;//借出true//未被借出false}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((borrowed == false)?" 未被借出":" 已借出") +'}';}
}
BookList类
package book;public class BookList {//创建数组存放书private Book[] books = new Book[10];private int book_size;//书的个数//构造方法,初始化成员(图书)(示例化BookList时直接就运行书架里的内容)public BookList() {this.books[0] = new Book("三国演义", "罗贯中", 23.1, "小说");//实例化一个Book对象,有参类型,所以Book类哪里要先创建有参的构造方法this.books[1] = new Book("西游记", "吴承恩", 45.1, "小说");this.books[2] = new Book("红楼梦", "曹雪芹", 33.2, "小说");this.books[3] = new Book("水浒传", "施耐庵", 23.2, "小说");this.book_size = 4;}public Book getBooks(int pos) {return books[pos];}public void setBooks(int pos, Book book) {this.books[pos] = book;//新增图书需要}public int getBook_size() {return book_size;}public void setBook_size(int book_size) {this.book_size = book_size;}}
operation包
AddBook类
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class AddBook implements InOperation{@Overridepublic 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();scanner.nextLine();System.out.println("请输入图书的类型:>");String type = scanner.nextLine();Book book = new Book(name, author, price, type);//实例化Book对象,存新增的书int currentSize = bookList.getBook_size();//当前剩下的书的数量for(int i = 0; i < currentSize; i++){Book book1 = bookList.getBooks(i);if(book1.getName().equals(name)){System.out.println("书架已有此书,不必添加");}return;}bookList.setBooks(currentSize, book);bookList.setBook_size(currentSize + 1);System.out.println("添加成功!!!");}
}
BorrowBook类
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class BorrowBook implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书");System.out.println("请输入所借图书名字");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getBook_size();for(int i = 0; i < currentSize; i++){Book book = bookList.getBooks(i);if(book.getName().equals(name)){if(book.isBorrowed() == false) {book.setBorrowed(true);System.out.println("存在该书,欢迎借阅");}else {System.out.println("该书不存在,对不起");}return;}}}
}
DeleteBook类
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class DeleteBook implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("删除图书");System.out.println("请输入你要删除的图书名字:>");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getBook_size();if(currentSize == 0){System.out.println("书架为空");}int flag = -1;for(int i = 0; i < currentSize; i++){Book book = bookList.getBooks(i);if(book.getName().equals(name)){flag = i;break;}}if(flag == -1){System.out.println("该书不存在");return;}//虽然这样访问不到最后一本书,但是书的数量减一了,这样就自然没了最后一本书for(int i = flag; i < currentSize - 1; i++) {bookList.setBooks(flag, bookList.getBooks(i + 1));}bookList.setBook_size(currentSize - 1);System.out.println("删除成功");}
}
FindBook类
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class FindBook implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("寻找图书");System.out.println("输入图书名字;>");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getBook_size();int index = -1;for(int i = 0; i < currentSize; i++){Book book = bookList.getBooks(i);if(book.getName().equals(name)) {index = i;}}if(index == -1){System.out.println("该书不存在");return;}Book book = bookList.getBooks(index);System.out.println(book);}
}
PrintBook类
package operation;import book.Book;
import book.BookList;public class PrintBook implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("显示全部图书");int currentSize = bookList.getBook_size();for(int i = 0; i < currentSize; i++){Book book = bookList.getBooks(i);System.out.println(book);}}
}
ReturnBook类
package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class ReturnBook implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("归还图书");System.out.println("请输入图书名字");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getBook_size();for (int i = 0; i < currentSize; i++){Book book = bookList.getBooks(i);if(book.getName().equals(name)){book.setBorrowed(false);System.out.println("归还成功");return;}}System.out.println("没这本书");}
}
Exit类
package operation;import book.BookList;public class Exit implements InOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统");int currentSize = bookList.getBook_size();for(int i = 0; i < currentSize; i++){bookList.setBooks(i,null);}bookList.setBook_size(0);System.exit(0);}
}
InOperation接口
package operation;import book.BookList;public interface InOperation {void work(BookList bookList);//因为这些操作都是在书架上,所以传参位BookList类型
}
user包
Domestic类
package user;import operation.*;import java.util.Scanner;import book.BookList;public class Domestic extends User{public Domestic(String name) {super(name);this.inOperations = new InOperation[]{new Exit(),new BorrowBook(),new ReturnBook(),new FindBook()//记得都要实例化对象才能用,因为他们都是类};}@Overridepublic int menu() {System.out.println("**********当当当!!!欢迎来到普通用户的菜单~~ *********");System.out.println("1.借阅图书 2.归还图书 ");System.out.println(" 3.查找图书 ");System.out.println(" 0.退出系统 ");System.out.println("请输入你想要进行的操作所对应的数字~~");Scanner s = new Scanner(System.in);int input = s.nextInt();return input;}
}
Administrator类
package user;
import operation.*;import java.util.Scanner;
public class Administrator extends User{public Administrator(String name) {super(name);this.inOperations = new InOperation[]{new Exit(),new AddBook(),new DeleteBook(),new FindBook(),new PrintBook()};}@Overridepublic int menu() {System.out.println("**********当当当!!!欢迎来到管理员的菜单~~ *********");System.out.println("1.新增图书 2.删除图书 ");System.out.println("3.查找图书 4.显示全部图书 ");System.out.println(" 0.退出系统 ");System.out.println("请输入你想要进行的操作所对应的数字~~");Scanner s = new Scanner(System.in);int input = s.nextInt();return input;}
}
User类
package user;import book.BookList;
import operation.InOperation;public abstract class User {private String name;public InOperation[] inOperations;//用public哟,以便其他地方使用public User(String name) {this.name = name;}public abstract int menu();public void doOperation(int choice, BookList bookList){inOperations[choice].work(bookList);//找到了对应的操作功能 调用相应的work方法}
}
Main类
import book.BookList;
import user.Administrator;
import user.Domestic;
import user.User;import java.util.Scanner;
public class Main {public static User login(){System.out.println("********** ~~欢迎来到图书管理系统~~ **********");System.out.println("请登录/注册");System.out.println("请选择你的身份 1 ---> 管理员 0 ---> 普通用户");Scanner s = new Scanner(System.in);int choice = s.nextInt();System.out.println("请输入你的名字");String name = s.next();if(choice == 1){return new Administrator(name);}else {return new Domestic(name);}}public static void main(String[] args) {BookList bookList = new BookList();//实例化对象User user = login();//这个确定身份(管理员或者普通用户)while(true){int choice = user.menu();//这个确定进行什么操作user.doOperation(choice, bookList);}}
}