将这三个方向做成三个包来分别实现
下面让我们进行更加具体的思路讲解:
因为功能类本质都是对数组进行操作,只是各自功能不同,所以创建一个IOperation接口,里面放一个work方法,让每个功能类都implements这个接口,对其中的work方法进行重写
package book;
public class Book {
private String name;//书的名字
private String author;//书的作者
private int price;//书的价格
private String type;//书的类型
private boolean state;//是否被借出
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.state = state;
}
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;
}
@Override
public String toString() {
return super.toString();
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
}
package book;
//书架类
public class BookList {
private Book[] books=new Book[10];//书架能放十本书
public int useSize;//存储当前书的个数
//实现通过构造方法初始化的时候,给数组里存三本书 useSize=3
public BookList(){
books[0]= new Book("《三国演义》","罗贯中",98,"小说");
books[1]= new Book("《西游记》","吴承恩",78,"小说");
books[2]= new Book("《红楼梦》","曹雪芹",89,"小说");
this.useSize=3;
}
//这里本来应该写一些操作这个数组的方法,但我们不这么做,换一种方法
//把这些操作都写在一个接口里
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
//你给我一个位置,我给你返回一个该位置的书
public Book getBooks(int pos){
return books[pos];
}
//存放一本书到指定位置
//这里的指定位置是当前最后一本书的下一个位置
public void setBooks(Book book,int pos) {
books[pos] = book;
}
}
package operations;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
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();
scanner.nextLine();//读取回车键
System.out.println("请输入你要新增图书的类型: ");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
//1.获取当前可以放书的位置
int currentSize = bookList.getUseSize();
//2.把书放进指定位置
bookList.setBooks(book,currentSize);
//3.书的有效个数加 1
bookList.setUseSize(currentSize+1);
}
}
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("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的书名:");
String name = scanner.nextLine();
//获取书架中现在有几本书
int usedSize = bookList.getUseSize();
int i=0;
//根据书名找到这个本书的下标
for (i = 0; i < usedSize; i++) {
Book book = bookList.getBooks(i);
if (book.getName().equals(name)) {
break;
}
}
if (i == usedSize) {
System.out.println("删除失败,未找到该书。");
return;
}
//通过前移将书删除
for (i = 0; i < usedSize - 1; i++) {
bookList.setBooks(bookList.getBooks(i+1),i);
}
bookList.setUseSize(bookList.getUseSize()-1);//书架中图书数量-1
System.out.println("删除成功。");
}
}
package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
public void work(BookList bookList){
System.out.println("查找图书");
System.out.println("请输入你要查找图书的名字: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if (name.equals(book.getName())){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
package operations;
import book.Book;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示所有图书");
int currentSize = bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
System.out.println(book);
}
}
}
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("借阅书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要借阅书籍的名称: ");
String name = scanner.nextLine();
//获取当前书架上书的个数
int currentSize = bookList.getUseSize();
//查找有没有要借阅的书
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
//判断书有没有被借走
if (book.getName().equals(name)){
if (!book.isState()){
System.out.println("借阅成功!");
book.setState(true); //将书的状态改为已借出
return;
}else {
System.out.println("借阅失败,该书已被借走");
return;
}
}
}
System.out.println("借阅失败,未找到您要找的书籍");
}
}
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("归还图书");
Scanner scanner = new Scanner(System.in);
System.out.println("输入要归还书的名称:");
String name = scanner.nextLine();
//获取书架上书的个数
int currentSize = bookList.getUseSize();
for (int i = 0; i < currentSize; i++) {
//查找书架上有没有这本书
Book book = bookList.getBooks(i);
if (book.equals(name)){
//判断书是否被借走
if (book.isState()){
System.out.println("成功归还");
book.setState(false);//将该书状态设置为未被借阅
return;
}else {
System.out.println("归还失败,该书已被借阅");
return;
}
}
}
System.out.println("未找到该书");
return;
}
}
package operations;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}
package user;
import book.BookList;
import operations.IOperation;
import javax.swing.plaf.SpinnerUI;
//All使用者类
public abstract class User {
public void doOperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
protected IOperation[] iOperations;//只是定义了数组,没有初始化
protected String name;
public User(String name) {
this.name = name;
}
public abstract int menu();
}
package user;
import operations.*;
import java.util.Scanner;
//管理员类
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{ //AdminUser接口数组
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu(){
System.out.println("********************************");
System.out.println("hello " + name +"欢迎来到图书小练习");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("********************************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
package user;
import operations.*;
import java.util.Scanner;
//普通用户类
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{ //NormalUser接口数组
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
};
}
public int menu(){
System.out.println("********************************");
System.out.println("hello " + name +"欢迎来到图书小练习");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("********************************");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}