定义了一些常用的属性和方法
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) ? ",已借出" : ",未借出" )+
//三目运算符,当书被借出时,显示已借出,否则显示未借出
'}';
}
}
用于放一些书,以及获取书的信息,设置书,设置书的个数,获取书的个数
public class BookList {
private Book[] books=new Book[10];
private int useSize ;
public BookList() {
books[0]=new Book("西游记","吴承恩",12,"小说");
books[1]=new Book("三国演义","罗贯中",15,"小说");
books[2]=new Book("红楼梦","曹雪芹",10,"小说");
this.useSize=3;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
public Book getBook(int pos){
return books[pos];
}
public void setBook(int pos,Book book){
books[pos]=book;
}
}
用户类,定义了用户的姓名属性,和对图书进行操作数组的属性,
public abstract class User {
private String name;
public Operation[] operations;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice , BookList bookList){
Operation operation1=this.operations[choice];
operation1.work(bookList);//根据传入的choice进行函数调用
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations=new Operation[]{
new ExitOperation(),
new ShowOperation(),
new AddOperation(),
new DelOperation(),
new FindOperation()
};
}
@Override
public int menu() {
System.out.println("*****管理员界面*****");
System.out.println("*****0.退出系统****");
System.out.println("*****1.显示图书****");
System.out.println("*****2.新增图书****");
System.out.println("*****3.删除图书****");
System.out.println("*****4.查找图书****");
System.out.println("请输入你的操作: ");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();//输入数字,根据数字调用相应的方法
return choice;
}
}
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
operations=new Operation[]{
new ExitOperation(),
new BorrowOperation(),
new FindOperation(),
new ReturnOperation(),
new ShowOperation()
};
}
public int menu() {
System.out.println("*****普通用户界面*****");
System.out.println("*****0.退出系统****");
System.out.println("*****1.借阅图书****");
System.out.println("*****2.查找图书****");
System.out.println("*****3.归还图书****");
System.out.println("*****4.显示图书****");
System.out.println("请输入你的操作: ");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
public class Test {
public static User login(){
System.out.println("请输入你的姓名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请选择你的身份: 0:普通用户,1:管理员");
int choice =scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}
return new NormalUser(name);
}
public static void main(String[] args) {
BookList bookList=new BookList();
User user=login();
while(true) {
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}
代码实现:
public int menu() {
System.out.println("*****管理员界面*****");
System.out.println("*****0.退出系统****");
System.out.println("*****1.显示图书****");
System.out.println("*****2.新增图书****");
System.out.println("*****3.删除图书****");
System.out.println("*****4.查找图书****");
System.out.println("请输入你的操作: ");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
思路:
我们要将所有的图书信息显示出来,那么我们将遍历书架上所有的书即可.
代码实现:
public void work(BookList bookList) {
System.out.println("*****显示图书*****");
int size=bookList.getUseSize();//获得书的个数
for (int i = 0; i < size; i++) {//遍历书
Book book=bookList.getBook(i);//获得该书的信息
System.out.println(book.toString());//打印信息
}
思路:
在这里手动添加书名,书的作者,类型,价格后,创建一个书的对象,
然后采取尾插法:将新增加的图书信息添加到最后,然后让计数器+1.
public class AddOperation implements Operation{
@Override
public void work(BookList bookList) {
System.out.println("*****新增图书*****");
System.out.println("请输入新增的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入作者:");
String author=scanner.nextLine();
System.out.println("请输入类型");
String type=scanner.nextLine();
System.out.println("请输入价格");
int price=scanner.nextInt();
Book book=new Book(name,author,price,type);
int size=bookList.getUseSize();
bookList.setBook(size,book);
bookList.setUseSize(size+1);
System.out.println("新增成功,以下是该书的信息");
System.out.println(book);
}
}
思路:
根据书名寻找要删除的图书信息:
通过遍历书的信息,找到要删除的图书,然后让该图书后面的图书依次成为前面图书,最后再让图书个数-1;
代码实现:
public void work(BookList book) {
System.out.println("请输入要删除的书名");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int size=book.getUseSize();
int i=0;
int pos=0;
for (; i < size; i++) {
if(book.getBook(i).getName().equals(name)){
pos=i;
break;
}
}
if(i==size){
System.out.println("要删除的书不存在");
}else{
for (int j = pos; j <size-1 ; j++) {
book.setBook(j,book.getBook(j+1));
}
book.setUseSize(size-1);
System.out.println("删除成功");
}
}
思路:
根据书名查找图书,并打印该图书的信息
通过遍历图书,找到要查找的图书,找到了便打印信息,并返回
代码实现:
public void work(BookList bookList) {
System.out.println("请输入要查找的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int size=bookList.getUseSize();
for (int i = 0; i <size ; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了,该书信息如下:");
System.out.println(book);
return;
}
}
System.out.println("未查到此书");
}
代码实现:
public int menu() {
System.out.println("*****普通用户界面*****");
System.out.println("*****0.退出系统****");
System.out.println("*****1.借阅图书****");
System.out.println("*****2.查找图书****");
System.out.println("*****3.归还图书****");
System.out.println("*****4.显示图书****");
System.out.println("请输入你的操作: ");
Scanner scanner=new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
思路:
根据书名借阅图书:
通过遍历图书,找到要借阅的图书,并将其的是否借出属性改为true即可,然后返回.
代码实现:
public void work(BookList bookList) {
System.out.println("请输入要借阅的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int size=bookList.getUseSize();
for (int i = 0; i < size; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
System.out.println("该书不存在");
}
}
思路:
根据书名查找书的信息:
通过遍历书的信息,找到书的信息(未找到,直接打印"未查到此书"并返回即可),然后打印书的信息即可.
代码实现
public void work(BookList bookList) {
System.out.println("请输入要查找的书名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
int size=bookList.getUseSize();
for (int i = 0; i <size ; i++) {
Book book=bookList.getBook(i);
if(book.getName().equals(name)){
System.out.println("找到了,该书信息如下:");
System.out.println(book);
return;
}
}
System.out.println("未查到此书");
}