项目列表
Book包
Book类内包含book的基本属性
BookList类初始化图书列表并且提供图书的属性方法
User包
Administrator类
common类
operator包
功能接口
新增图书功能
借阅图书功能
删除图书功能
显示图书功能
查找图书功能
归还图书功能
结束释放资源功能
运行逻辑
项目列表
Book包
Book类内包含book的基本属性
package book;
public class BooK {
String name;//图书名
String author;//图书作者
float price;//图书价格
String type;//图书类型
boolean state;//图书状态 借/未借
public BooK(String name, String author, float price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.state = false;
}
@Override
public String toString() {
return "book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((state)?"借出":"未借出")+
'}';
}
}
BookList类初始化图书列表并且提供图书的属性方法
package book;
public class BookList {
private BooK[] Books=new BooK[10];
private int count;
public BookList() {
Books[0]=new BooK("a","a",10,"a");
Books[1]=new BooK("b","b",10,"b");
Books[2]=new BooK("c","c",10,"c");
this.count=3;
}
public int getCount() {
return count;
}
public void SetCount(int i){this.count=i;}
//对书籍进行修改
public void setBooks(int i,BooK books) {
Books[i] = books;
}
public void AddSetBooks(int i,BooK books)
{
Books[i] = books;
this.count++;
}
//获取对应的书
public BooK getPos(int i) {
return Books[i];
}
//获取对应书的名字
public String getBookName(int i)
{
return Books[i].name;
}
//获取书籍的状态
public boolean getBookState(int i)
{
return Books[i].state;
}
public void TrueBookState(int i)
{
Books[i].state=true;
}
public void FalseBookState(int i)
{
Books[i].state=false;
}
}
User包
Administrator类
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class Administrator extends User{
public Administrator(String name) {
super(name);
//接口数组
this.iOperations=new IOperation[]{
new Breakoperation(),
new Findoperation(),
new ADDoperation(),
new Deleteoperation(),
new Showoperation()
};
}
@Override
public void DoOperation(int chick, BookList booklist) {
//接收用户的选择和要操作的图书类
//调用对应的操作和他重写的work方法,并把图书类booklist传入
iOperations[chick].work(booklist);
}
@Override
public int menu() {
System.out.println("1.查找图书\n2.新增图书\n3.删除图书\n4.显示图书\n0.退出系统\n请选择: ");
Scanner scanner=new Scanner(System.in);
int chick=scanner.nextInt();
while(chick<0||chick>4)
{
System.out.println("输入有误,请重新输入");
System.out.println("请选择: ");
chick=scanner.nextInt();
}
return chick;
}
}
common类
package user;
import book.BookList;
import operation.*;
import java.util.Scanner;
public class common extends User{
public common(String name) {
super(name);
this.iOperations=new IOperation[]{
new Breakoperation(),
new Findoperation(),
new Borrowoperation(),
new Returnoperation(),
new Showoperation()
};
}
@Override
public void DoOperation(int chick, BookList booklist) {
//接收用户的选择和要操作的图书类
//调用对应的操作和他重写的work方法,并把图书类booklist传入
iOperations[chick].work(booklist);
}
@Override
public int menu() {
System.out.println("1.查找图书\n2.借阅图书\n3.归还图书\n4.查看图书\n0.退出系统\n请选择: ");
Scanner scanner=new Scanner(System.in);
int chick=scanner.nextInt();
while(chick<0||chick>4)
{
System.out.println("输入有误,请重新输入");
System.out.println("请选择: ");
chick=scanner.nextInt();
}
return chick;
}
}
operator包
功能接口
public interface IOperation {
public void work(BookList books);
}
新增图书功能
public class ADDoperation implements IOperation{
@Override
public void work(BookList books) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入书名");
String name = scanner.next();
System.out.println("请输入作者");
String author = scanner.next();
System.out.println("请输入价格");
float price = scanner.nextInt();
System.out.println("请输入类型");
String type = scanner.next();
BooK book=new BooK(name,author,price,type);
int cur=books.getCount();
//在cur下标添加book书
books.AddSetBooks(cur,book);
System.out.println("添加成功");
}
}
借阅图书功能
public class Borrowoperation implements IOperation{
@Override
public void work(BookList books) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要借阅的书");
String name=scanner.next();
int cur=books.getCount();
for (int i = 0; i < cur; i++) {
if(Objects.equals(name, books.getBookName(i)))
{
if(!books.getBookState(i))
{
books.TrueBookState(i);
System.out.println("成功借阅"+name);
}
else
{
System.out.println("该书已被借出");
}
}
}
}
}
删除图书功能
public class Deleteoperation implements IOperation{
public void work(BookList bookList) {
System.out.println("请输入要删除的书名");
Scanner scanner=new Scanner(System.in);
String name=scanner.next();
int cur=bookList.getCount();
String Bookname;
int index=0;
int i=0;
for (; i < cur; i++)
{
if(Objects.equals(name, Bookname = bookList.getBookName(i)))
{
index=i;
break;
}
}
if(i==cur)
{
System.out.println("未查找到此书");
}
else
{
//0 2
//
for (int j = index; j < cur-1; j++) {
//books[j]=books[j+1]
//获取书籍
BooK book=bookList.getPos(j+1);
bookList.setBooks(j,book);
}
bookList.SetCount(cur-1);
}
}
}
显示图书功能
public class Showoperation implements IOperation{
@Override
public void work(BookList books) {
int cur=books.getCount();
for (int i = 0; i < cur; i++) {
BooK book=books.getPos(i);
System.out.println(book);
}
}
}
查找图书功能
public class Findoperation implements IOperation{
@Override
public void work(BookList books) {
System.out.println("请输入要查找的书名");
Scanner scanner=new Scanner(System.in);
String name=scanner.next();
int cur=books.getCount();
for (int i = 0; i < cur; i++)
{
//用户输入的数据和类内数据进行判断时,不能用==要用equals
if(Objects.equals(name, books.getBookName(i)))
{
BooK book=books.getPos(i);
System.out.println(book);
return;
}
}
System.out.println("未查找到该书");
}
}
归还图书功能
public class Returnoperation implements IOperation {
@Override
public void work(BookList books) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要归还的书");
String name=scanner.next();
int cur=books.getCount();
for (int i = 0; i < cur; i++) {
if(Objects.equals(name, books.getBookName(i)))
{
if(!books.getBookState(i))
{
System.out.println("该书并未被借出");
}
else
{
books.FalseBookState(i);
System.out.println("已成功归还"+name);
}
}
}
}
}
结束释放资源功能
public class Breakoperation implements IOperation{
@Override
public void work(BookList books) {
System.out.println("结束");
int cur=books.getCount();
for (int i = 0; i < cur; i++) {
books.setBooks(i,null);
}
System.exit(0);
}
}
运行逻辑
- 初始化图书列表
- 根据用户选择的身份,创建对应的类,用父类接收。
- menu函数会提示用户选择功能。chick会接收到用户选择的功能对应数字
- 这时User(已经是用户选择的身份Admi或comm)调用对应的DoOperation函数 传入对应的选择和图书列表
- DoOperation函数会根据接口数组调用对应的功能,并且在对应的booklist内修改数据