目录
- 1 简介
- 2 核心需求
- 3 类的设计
- 3.1 创建图书相关的类
- 3.2 创建操作相关的类
- 3.3 创建用户相关的类
- 3.4 进行整合
- 3.5 实现具体的每个 Operation
1 简介
本节主要是,利用前面所学的知识点:类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习。
2 核心需求
1. 简单的登录
2. 管理端
整理书籍(该功能为可扩展功能)
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出
3. 用户端
查询书籍
借阅书籍
归还书籍
退出
3 类的设计
3.1 创建图书相关的类
先创建 package book
创建 Book 类, 表示一本书:
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean status;//false
//private boolean isBorrowed = false;
/*构造方法*/
public Book(String name, String author, int price, String type){
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
/*getter、setter、toString方法*/
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;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
/*", status=" + status +*/
((status == true)?"借出":"未借出")+
'}';
}
}
创建 BookList 类, 用来保存 N 本书,表示书架:
package book;
/*书架*/
public class BookList {
private Book[] books;
private int usedSize = 0;
public BookList() {
this.books = new Book[10];
books[0] = new Book("三国演义", "罗贯中", 100,
"小说");
books[1] = new Book("水浒传", "施耐庵", 100,
"小说");
books[2] = new Book("西游记", "吴承恩", 100,
"小说");
this.usedSize = 3;
}
//尾插法
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
public Book getBook(int pos) {
return this.books[pos];
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
3.2 创建操作相关的类
先创建 package operation
再创建一个对书操作的接口IOperation:
package operation;
import book.BookList;
public interface IOperation {
void work(BookList booklist);
}
接下来创建一组操作类, 每个类对应一个用户的动作:
AddOperation
DelOperation
FindOperation
DisplayOperation
BorrowOperation
ReturnOperation
ExitOperation
先把空类创建好, 不着急实现细节。
抽象出 Operation 的好处: 让操作和操作之间低耦合, 让操作和用户之间低耦合。
3.3 创建用户相关的类
先创建 package user
创建 User 类, 这是一个抽象类:
// User 类是一个抽象类, 每个子类需要做两件事情
// 1. 初始化对应的操作数组
// 2. 实现 Menu 菜单
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
public String name;
public IOperation[] operations;
public User(String name){
this.name = name;
}
// 显示菜单
public abstract int menu();
// 根据用户选项执行操作
public void doOperation(int choice, BookList bookList) {
//看这个数组当中存的到底是哪些操作
this.operations[choice].work(bookList);
}
}
创建普通用户类,NormalUser类,是 User 的子类:
package user;
import operation.*;
import java.util.Scanner;
/*普通用户*/
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("普通用户的菜单!");
System.out.println("=============================");
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("=============================");
System.out.println("请输入您的选择: ");
int choice = scanner.nextInt();
return choice;
}
}
创建管理员用户类,AdminUser类,也是 User 的子类:
package user;
import operation.*;
import java.util.Scanner;
/*管理员*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.operations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
/*返回你要执行的对应的操作*/
@Override
public int menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("管理员的菜单!");
System.out.println("=============================");
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("=============================");
System.out.println("请输入您的选择: ");
//Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
//scanner.close();
return choice;
}
}
3.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() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.next();
System.out.println("请输入您的身份(1 表示管理员, 0 表示普通用户):");
int who = scanner.nextInt();
if (who == 1) {
return new AdminUser(name);
}
return new NormalUser(name);
}
public static void main(String[] args) {
// 1. 准备基本的数据
BookList bookList = new BookList();
// 2. 创建用户
//1.登录user会引用一个对象,这个对象可能是AdminUser,也可能是NormalUser
User user = login();
//会调用user引用的对象的menu方法,返回对应的选择
// user.menu();
// 3. 进入主循环
while (true) {
//拿choice来调用对应类的work方法
//这个work方法,调用谁的,怎么调用,交给doOperation
int choice = user.menu();
user.doOperation(choice, bookList);
}
}
}
在这里可以先测试下代码的基本框架是否存在问题。
3.5 实现具体的每个 Operation
本文的目的主要是熟悉类与类之间的关系,掌握抽象类,接口,继承,封装等知识点的应用,所以这部分我们仅进行了简单描述。
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 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);
int currentSize = booklist.getUsedSize();//3
booklist.setBook(currentSize,book);
booklist.setUsedSize(currentSize+1);
}
}
BorrowOperation:
package operation;
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();
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
//说明有这本书!
//逻辑:把这本书的借阅状态修改为true
book.setStatus(true);
System.out.println(book);
return;
}
}
System.out.println("没有你要借阅的图书!");
}
}
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 i = 0;
for (; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
break;
}
}
if(i >= booklist.getUsedSize()){
System.out.println("没有要删除的这本书!");
return;
}
int pos = i;
for (int j = pos; j <booklist.getUsedSize()-1 ; j++) {
Book book = booklist.getBook(j+1);
booklist.setBook(j,book);
}
int currentSize = booklist.getUsedSize();
booklist.setUsedSize(currentSize-1);
System.out.println("删除书籍完毕!");
}
}
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++){
Book book = booklist.getBook(i);
System.out.println(book);
}
}
}
ExitOperation:
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("退出系统");
System.exit(1);
}
}
FindOperation:
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList booklist) {
System.out.println("查找书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名");
String name = scanner.nextLine();
for (int i = 0;i<booklist.getUsedSize();i++){
Book book = booklist.getBook(i);
if(book.getName().equals(name)){
System.out.println("有这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
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("归还书籍");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的书名:");
String name = scanner.nextLine();
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = booklist.getBook(i);
if (book.getName().equals(name)) {
book.setStatus(false);
System.out.println(book);
return;
}
}
System.out.println("没有你要归还的这本书!");
}
}