分别将书本类和书架类放到book包去,将管理员用户与普通用户放到user包去,将对书本各种操作放到opera包去。
public class Book {
private String name;//书名
private String author;//作者
private double price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出
//重写构造方法
public Book(String name, String author, double price, String type) {
//参数列表中没有isBorrowed是因为布尔类型默认是false,
//对于图书来说,放进书架的书刚开始就是未被借出的
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
//书本类属性的get和set方法
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 isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
//重写toString()
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", "+(isBorrowed==true?"已被借出":"未被借出") +
'}';
}
}
书架类提供存放书本类数组的地方,方便后续在书架类中对书本的操作
代码实现如下:
public class BookList {
private static final int DEFAULT_SIZE=12;
private Book[] books = new Book[DEFAULT_SIZE];
private int usedSize;//记录当前books数组当中有多少多少本书
public BookList() {
books[0]=new Book("三国演义","罗贯中",89,"小说");
books[1]=new Book("西游记","吴承恩",78,"小说");
books[2]=new Book("红楼梦","曹雪芹",49,"小说");
this.usedSize=3;
}
public Book getBooks(int pos) {
return this.books[pos];
}
public void setBooks(Book book){
this.books[usedSize]=book;
}
public void setBooks(int pos,Book book) {
books[pos]=book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
管理员用户和普通用户之间存在着共性,都是用户的分支,所以这两者都是用户的子类
我们首先要做的就是实现User这个父类
管理员用户和普通用户都要用到菜单界面(menu( )),管理员用户类和普通用户类继承用户类的menu( )方法再重写就行,用户类的方法不用实现,用abstract修饰,类中有抽象方法,则该类为抽象类,所以用户类也要用abstract修饰
public abstract class User {
protected String name;
protected IOperation[] iOperations;//IOperation为接下来要讲的的操作类
public User(String name) {this.name = name;}
public abstract int menu();
public void doWork(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
我们对书架类执行的操作有增删查改,这些操作都遵循一定的规范,这些规范由接口来实现
在增删查改的操作中,接口提供自己的方法给实现接口的类,实现接口的类对方法进行重写
public interface IOperation {
//这些操作都是对书架类的对象进行修改
//所以work()的参数为书架类的对象
void work(BookList bookList);
}
因为重写过book的toString()方法了,所以可以直接遍历进行输出即可,
public class ShowOpeartion implements IOperation{
@Override
public void work(BookList bookList) {
int currentSize=bookList.getUsedSize();//通过bookList.getUsedSize()来得到书架中有多少本书
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBooks(i);//遍历书架上的书
System.out.println(book);
}
}
}
System.exit(0)代表程序运行结束
public class Exitoperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
System.out.println("退出成功");
}
}
查找图书要遍历书架
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();
int currentSize=bookList.getUsedSize();
for (int i = 0; i <currentSize ; i++) {
Book book=bookList.getBooks(i);
if(book.getName().equals(name)){
System.out.println("找到了这本书");
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
增加前要查看这本书是否已经在书架里了
public class AddOperation implements IOperation{
@Override
public 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("请输入价格");
double price=scanner.nextDouble();
scanner.nextLine();//这一步为了拿走上一个输入完价格后输入的回车字符
System.out.println("请输入类型");
String type =scanner.nextLine();
Book book=new Book(name,author,price,type);
int currentSize=bookList.getUsedSize();
//遍历书架,看要加入的书是否已经存在
for (int i = 0; i <currentSize ; i++) {
Book tmp=bookList.getBooks(i);
if(tmp.getName().equals(name)){
System.out.println("这本书已存在书架中");
return;
}
}
bookList.setBooks(book);
//加入一本书后,usedSzie+1
bookList.setUsedSize(currentSize+1);
}
public class DelOperation 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.getUsedSize();
int index=-1;
//寻找要删除书本的下标
for (int i = 0; i <currentSize ; i++) {
Book tmp=bookList.getBooks(i);
if(tmp.getName().equals(name)){
index=i;
break;
}
}
//挪动数据,向前覆盖要删除的书
for (int i = index; i < currentSize-1; i++) {
Book book=bookList.getBooks(i+1);
bookList.setBooks(i,book);
}
//因为删除的是对象,所以把最后一个置为null
bookList.setBooks(currentSize,null);
//修改size
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功");
}
}
先要判断想借的书是否已被借出,若没被借出,则将该书的isBorrowed属性改为true
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.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)&&!book.isBorrowed()){
book.setBorrowed(true);
System.out.println("借阅成功");
return;
}
}
}
}
归还与借阅操作差不多,就不赘述了
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.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBooks(i);
if(book.getName().equals(name)&&book.isBorrowed()){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
}
}
管理员用户类继承用户类并重写构造方法和menu( )方法
iOperations的初始化,可以利用接口的多态性来进行实现,
//Uesr类
public abstract class User {
protected String name;
protected IOperation[] iOperations;//IOperation[]是操作数组类型
public User(String name) {this.name = name;}
public abstract int menu();
//通过menu()方法返回的数字就是choice
public void doWork(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
//因为IOperation是接口,增删查改等操作的类对接口进行实现,所以可以通过接口数组来实现多态
this.iOperations=new IOperation[]{
new Exitoperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOpeartion()
};
}
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("==============================");
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);
this.iOperations=new IOperation[]{
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("==============================");
System.out.println("请输入你的操作");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
通过身份输入来实例化login( )方法的返回对象的引用是AdminUser类型还是NormalUser类型
public class Main {
public static User login(){
System.out.println("请输入你的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("输入你的身份: 1-》管理员 0-》普通用户");
int choice = scanner.nextInt();
if(choice==1){
return new AdminUser(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user=login();
while(true) {
int choice = user.menu();
//根据choice 和 user来确定 我到底调用哪个对象的哪个操作
user.doWork(choice, bookList);
}
}
以上就是本篇文章的内容了,很感谢你能看到这里
如果觉得内容对你有帮助的话,不妨点个关注
我会继续更新更高质量的内容,我们一同学习,一同进步!