目录
一.功能展示
在图书管理系统中我给定了两个身份:
1->管理员
0->普通用户
可以看到每个身份所能实现的功能有些不同,但是基本原理都是差不多的。
我们主要是由抽象类,接口,多态,继承等知识来实现,如果有不太理解的地方,可以看看博主的博客哦!
二,具体实现的过程
2.1代码:
我将整个的代码按类型和实现效果分为了三个包来编写:
分别是:book,operation,user
book:主要是于书本本身有关
operatio:主要是来储存想要实现的操作
user:于用户有关
其中各个包由分为了几个类来实现其功能
最后将其所有的功能汇总在Main这个类当中:
2.1.1 具体代码
1.book包
1.1 Book类
主要是用来存放图书的信息,数据的转接等工作
package Book; public class Book { private String name; private String author; private double price; private String type; private boolean isBorrowed;//private可以在不同包的子类调用它 public Book(String name, String author, double price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; //this.isBorrowed = isBorrowed; } 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; } @Override public String toString() { return "图书{" + "名字='" + name + '\'' + ", 作者 ='" + author + '\'' + ", 价格 =" + price + ", 类型 ='" + type + '\'' + ", 借阅情况 =" + ((isBorrowed==true)?"已借出":"未借出") + '}'; } }
1.2 Booklist类
BookList是一个书架,存储书,我们默认是10本书,并且给定三本默认的图书
package Book; /** * Created with IntelliJ IEDA. * Description: * User:86186 * Date:2022-05-23 * Time:21:26 * 书架 */ public class Booklist{ //最多可以放10本书 Book[] books = new Book[10]; private int usedSize;//实时记录当前books里面有多少本书 //构造方法 public Booklist(){ books[0]=new Book("平凡的世界","路遥",19.9,"小说");//0 books[1]=new Book("云边有个小卖部","张嘉佳",29.9,"小说");//1 books[2]=new Book("活着","余华",28.9,"小说");//2 usedSize = 3; } /** * @param pos 的位置一定要合法 * @return 返回的是你想要放的一本书 */ public void setBooks(int pos,Book book) { books[pos] = book; } /** * @param pos 的位置一定要合法 * @return 返回的是你想要借的一本书 */ public Book getBooks(int pos){ return books[pos]; } /** * 实时修改书架上的书的数目 * */ public void setUsedSize(int size){ usedSize = size; } /** * 实时获取书架上的书的数目 * */ public int getUsedSize(){ return usedSize; } }
2. operation 包
2.1 接口IOperation
这接口当中的抽象方法会在后续的实现接口的类中重写!!
package operation; import Book.Booklist; public interface IOperation { void work(Booklist booklist); }
2.2 AddOperation
package operation;
import Book.Book;
import Book.Booklist;
import java.util.Scanner;
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 auther = scanner.nextLine();
System.out.println("请输入新增图书价格:");
double price = scanner.nextDouble();
System.out.println("请输入新增图书类型:");
String bookname = scanner.next();
System.out.println("请输入新增图书数量");
int bookQuantity = scanner.nextInt();
int ret = booklist.getUsedSize();
for (int i = 0; i < bookQuantity; i++) {
Book book = new Book(name,auther,price,bookname);
ret = ret + 1;
booklist.setBooks(ret-1,book);
booklist.setUsedSize(ret);
}
System.out.println("新增图书成功!");
}
}
2.3 BorrowOperation
package operation;
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++) {
if (!(booklist.getBooks(i).isBorrowed())&&(name.equals(booklist.getBooks(i).getName()))){
booklist.getBooks(i).setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("该书借出!");
}
}
2.4 DelOperation
package operation;
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.println("请输入需要删除的图书名字:");
String name = scanner.nextLine();
int nowSize = booklist.getUsedSize();
for (int i = 0; i < nowSize; i++) {
if (name.equals(booklist.getBooks(i).getName())){
for (int j = i; j > 0 ; j--) {
Book book = booklist.getBooks(j-1);
book=booklist.getBooks(j);
}
booklist.setUsedSize(nowSize-1);
System.out.println("删除成功!");
return;
}
}
System.out.println("该图书已被删除!");
}
}
2.5 DisplayOperation
package operation;
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++) {
System.out.println(booklist.getBooks(i));
}
}
}
2.6 ExitOperation
package operation;
import Book.Booklist;
public class ExitOperation implements IOperation{
@Override
public void work(Booklist booklist) {
System.out.println("退出系统");
System.exit(0);//表示程序正常结束
}
}
2.7 FindOperation
package operation;
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.next();
int ret = booklist.getUsedSize();
for (int i = 0; i < ret; i++) {
if (name.equals(booklist.getBooks(i).getName())){
System.out.println("找到了!");
System.out.println(booklist.getBooks(i).toString());
return;
}
}
System.out.println("该图书以及被借出/没有这本书!");
}
}
2.8 ReturnOperation
package operation;
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.next();
int ret = booklist.getUsedSize();
for (int i = 0; i < ret; i++) {
if (name.equals(booklist.getBooks(i).getName())){
booklist.getBooks(i).setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("该书以及归还或不为本书屋的书!");
}
}
3. user包
我们之前说过我们说过在图书管理系统中我给定了两个身份: 管理员,普通用户
两者所具有的功能都是大相径庭的,所以我们需要给定两个不同的类来区分和给定其内容
3.1User类
User是这个包中其他两个类的父类,其他两个类分别继承User
package user;
import Book.Booklist;
import operation.IOperation;
public abstract class User {
protected String name;//用户名
protected IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, Booklist bookList){
this.iOperations[choice].work(bookList);
}
}
3.2 AdminUser
管理员:
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
super.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
public int menu() {
System.out.println("hello "+this.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;
}
}
3.3 NormalUser
普通用户:
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
super.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("hello "+this.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;
}
}
4.Main类
Main主要是用来整合这些功能属性的,同样也是这个整个项目的开端
import Book.Booklist; import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; 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(); user.doOperation(choice,booklist); } } }
2.2 效果展示