目录
实现目标
1、简单的登录
2、管理端
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出
3、用户端
查询书籍
借阅书籍
归还书籍
退出
项目展示
实现过程
类的设计
1. 创建图书相关的类
先创建 package book 创建 Book 类, 表示一本书
package book;
import javax.swing.*;
public class Book {
private String name; //书名
private String author; //作者
private int price; //价格
private String type; // 类型
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;
}
//提供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 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 getBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
//", isBorrowed=" + isBorrowed +
(isBorrowed ? " 已借出": " 未借出")+
'}';
}
}
创建 BookList 类, 用来保存 N 本书.
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
//构造方法 初始化书 先放三本书
public BookList() {
this.books[0] = new Book("三国演义","罗贯中",17,"小说");
this.books[1] = new Book("西游记","吴承恩",20,"小说");
this.books[2] = new Book("水浒传","施耐庵",31,"小说");
this.usedSize = 3;
}
//get和set方法 便于后续得到和设置书的位置、书的数量
public Book getBook(int pos){
return this.books[pos];
}
public void setBook(int pos,Book book){
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
2.创建操作相关的类
先创建 package operation 创建接口
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
接下来创建一组操作类, 每个类对应一个用户的动作.
AddOperation 增加书籍
DelOperation 删除书籍
FindOperation 查找书籍
DisplayOperation 展示书籍
BorrowOperation 借阅书籍
ReturnOperation 归还书籍
ExitOperation 退出系统
先把空类创建好, 不着急实现细节.
抽象出 Operation 的好处: 让操作和操作之间低耦合, 让操作和用户之间低耦合.
3.创建用户相关的类
先创建 package user
创建 User 类, 这是一个抽象类
package user;
// User 类是一个抽象类, 每个子类需要做两件事情
// 1. 初始化对应的操作数组 2. 实现 Menu 菜单
abstract public class User {
protected String name;
protected IOperation[] iOperations;
public User(String name) {
this.name = name;
}
// 显示菜单
abstract public int menu();
// 根据用户选项执行操作
public void doIOperation(int choice, BookList bookList){
this.iOperations[choice].work(bookList);
}
}
创建管理员类, 是 User 的子类.
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{ //操作数组
new ExitOperation(),//0.退出系统
new FindOperation(),//1.查找图书
new AddOperation(), //2.增加图书
new DelOperation(), //3.删除图书
new DisplayOperation()//4.显示图书
};
}
@Override
public int menu(){
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("============================");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;//根据所选的数字 匹配不同的操作功能
}
}
创建普通用户类,是User的子类
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{ //操作数组
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
@Override
public int menu(){
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("============================");
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("请选择你的身份:");
System.out.println("0.管理员 1.普通用户 ");
int who = scanner.nextInt(); //判断身份
if (who == 0){
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.doIOperation(choice,bookList);
}
}
}
5.实现具体的每个 Operation
查找书籍:
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();
int size = bookList.getUsedSize();
for (int i = 0; i < size;i++){
Book book = bookList.getBook(i);
if (book.getName().equals(name)){
System.out.println("找到这本书的信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有这本书!");
}
}
增加图书:
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("新增图书");
System.out.println("请输入图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入图书的类型:");
String type = scanner.nextLine();//输入String类型不能写在int后面
System.out.println("请输入图书的价格:");
int price = scanner.nextInt();
//注意 使用nextLine读取字符串时 不能在前面读取整数 将其写在最后
Book book = new Book(name,author,price,type);
int size = bookList.getUsedSize();
for (int i = 0; i < size;i++){
Book bookCur = bookList.getBook(i);
if (bookCur.getName().equals(name)){
System.out.println("已包含这本书!增加失败");
return;
}
}
bookList.setBook(size,book);
bookList.setUsedSize(size+1);
System.out.println("增加成功!");
}
}
借阅图书:
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowedOperation 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 size = bookList.getUsedSize();
for (int i = 0; i < size;i++){
Book bookCur = bookList.getBook(i);
if (bookCur.getName().equals(name)){
bookCur.setBorrowed(true);
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有此书!借阅失败");
}
}
删除书籍:
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("删除书籍");
System.out.println("请输入图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int size = bookList.getUsedSize();
int i = 0;
int index = 0;
for (; i < size;i++){
Book bookCur = bookList.getBook(i);
if (bookCur.getName().equals(name)){
index = i;//定位到i下标的书
break;
}
}
if (i == size){
System.out.println("没有此书!删除失败");
return;
}
//使用for循环让后一个位置的书前移一位
for (int j = index; j < size-1 ; j++){
Book book = bookList.getBook(j+1);//得到j+1位置的书
bookList.setBook(j,book);//将其设置为j位置
}
bookList.setUsedSize(size-1);
System.out.println("删除成功!");
}
}
归还书籍
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.nextLine();
int size = bookList.getUsedSize();
for (int i = 0; i < size;i++){
Book bookCur = bookList.getBook(i);
if (bookCur.getName().equals(name)){
bookCur.setBorrowed(false);
System.out.println("归还成功!");
return;
}
}
System.out.println("没有此书!归还失败");
}
}
显示书籍
package operation;
import book.BookList;
public class DisplayOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("显示书籍!");
int size = bookList.getUsedSize();
for (int i = 0; i < size ; i++){
System.out.println(bookList.getBook(i));
}
}
}
退出系统
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统");
System.exit(0);
}
}