一、operations包
我们的图书管理系统有很多具体的操作,为了后面方便多态,以及检验错误,所以我们实现一个具体的IOperation接口,每一个具体的操作去实现这个接口.
1. IOperation接口
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
2. AddOperation
增加图书:
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@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("请输入要新增的图书的价格: ");
int price = scanner.nextInt();
System.out.println("请输入要新增的图书的类型: ");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//1.获取当前书存放的位置
int curSize = bookList.getUsedSize();
//2.把书放在指定位置
bookList.setBooks(book,curSize);
//3.更新书的个数
bookList.setUsedSize(curSize+1);
}
}
3. BorrowOperation
借阅图书:
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书! ");
System.out.println("请输入要借阅的图书的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
if(book.isBorrowed()) {
System.out.println("该书已经被借出! ");
}else {
book.setBorrowed(true);
System.out.println("借阅图书成功! ");
return;
}
}
}
System.out.println("没有你要借阅的书! ");
}
}
4. DelOperation
删除图书:
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书! ");
System.out.println("请输入要删除的图书: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
//查找图书是否有此图书,记录下标
int index = -1;
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
index = i;
break;
}
}
if(index == -1) {
System.out.println("没有 "+name+"这本书!");
return;
}
for (int i = index; i < bookList.getUsedSize()-1; i++) {
Book book = bookList.getPos(i+1);
bookList.setBooks(book,i);
}
//删除的书,要置空
bookList.setBooks(null, bookList.getUsedSize()-1);
bookList.setUsedSize(bookList.getUsedSize()-1);
}
}
5. DisplayOperation
显示图书:
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书! ");
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
System.out.println(book);
}
}
}
6. ExitOperation
退出图书:
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统! ");
System.exit(0);
}
}
7. FindOperation
查找图书:
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
//查找图书
System.out.println("查找图书! ");
System.out.println("请输入要查找的图书: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
System.out.println("找到了! ");
System.out.println(book);
return;
}
}
System.out.println("没有这本书! ");
}
}
8. ReturnOperation
归还图书:
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书! ");
System.out.println("请输入要归还的图书的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
Book book = bookList.getPos(i);
if(name.equals(book.getName())) {
book.setBorrowed(false);
System.out.println("归还图书成功! ");
return;
}
}
System.out.println("没有你要归还的书! ");
}
}