Java图书管理代码
大家好,我是晓星航。今天为大家带来的是Java语言图书馆里系统的相关的讲解!😀
一:简介
利用所学的知识点:类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习。
二:核心需求
1、简单的登录
2、管理端
- 整理书籍(该功能为可扩展功能)
- 查阅书籍
- 增加书籍
- 删除书籍
- 打印书籍列表
- 退出
3、用户端
- 查询书籍
- 借阅书籍
- 归还书籍
- 退出
整体功能概括图:
三: 类的设计
1. 创建图书相关的类
先创建 package book
创建 Book 类, 表示一本书
public class Book { private String name;//书名 private String author;//作者 private int price;//价格 private String type;//类型 private boolean isBorrowed;//是否借出 public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } 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 isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed == true) ? "已经被借出" : "未被借出") + '}'; } }
创建 BookList 类, 用来保存 N 本书.
public class BookList { private Book[] books = new Book[10]; private int usedSize; public BookList() { books[0] = new Book("三国演义","罗贯中",17,"小说"); books[1] = new Book("西游记","吴承恩",47,"小说"); books[2] = new Book("水浒传","施耐庵",37,"小说"); this.usedSize = 3; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } /** * 获取到pos位置的一本书 * @param pos * @return */ public Book getPos(int pos) { return this.books[pos]; } /** * 设置pos下标为一本书->[添加一本书] * @param pos * @param book */ public void setBook(int pos,Book book) { this.books[pos] = book; } }
2. 创建操作相关的类
先创建 package Ioperation 接口便于拓展我们接下来的功能
public interface IOperation { void work(BookList bookList); }
接下来创建一组操作类, 每个类对应一个用户的动作.
AddOperation
DelOperation
FindOperation
DisplayOperation
BorrowOperation
ReturnOperation
ExitOperation
先把空类创建好, 不着急实现细节.
抽象出 Operation 的好处: 让操作和操作之间低耦合, 让操作和用户之间低耦合.
将每一个功能单独拿出来在operation包中新建一个类,来面向对象编程 具体实现如下:
3. 创建用户相关的类
先创建 package user
创建 User 类, 这是一个抽象类
// User 类是一个抽象类, 每个子类需要做两件事情 // 1. 初始化对应的操作数组 // 2. 实现 Menu 菜单 public abstract class User { protected String name; protected IOperation[] iOperations; public User(String name) { this.name = name; } public abstract int menu(); public void doWork(int choice, BookList bookList) { iOperations[choice].work(bookList); } }
创建普通用户类(NormalUser), 是 User 的子类.
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"+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; } }
创建管理员用户类(AdminUser)
public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations = new IOperation[] { new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } 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; } }
4. 进行整合
创建 Main 类和 main 方法, 搭建整体逻辑
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.doWork(choice,bookList); } } }
可以先测试下代码的基本框架是否存在问题
经检验代码没有问题,可以成功运行。
5. 实现具体的每个 Operation
接下来我们为大家讲解每一个功能的构建:
1、addOperation(增加图书功能):
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(); System.out.println("请输入新增图书价格:"); int price = scanner.nextInt(); Book book = new Book(name,author,price,type); int size = bookList.getUsedSize(); bookList.setBook(size,book); bookList.setUsedSize(size + 1); System.out.println("新增图书成功!"); } }
注意:我们增加完成后要记得给我们的图书数量size+1
2、BorrowOperation(借出图书功能)
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(); int size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getPos(i); if (name.equals(book.getName())) { book.setBorrowed(true); System.out.println("借阅成功!"); System.out.println(book); return; } } System.out.println("没有你要借阅的这本书!"); } }
3、DelOperation(删除图书功能)
public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("删除图书!"); //1.根据书名找到书的位置 index System.out.println("请输入你要删除的图书的名字:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); int index = 0;//存储找到的下标 int i = 0; for (; i < currentSize; i++) { Book book = bookList.getPos(i); if(book.getName().equals(name)) { index = i; break; } } if (i >= currentSize) { System.out.println("没有你要删除的这本书!"); return; } //2.进行删除 for (int j = index; j < currentSize - 1; j++) { //bookList[j] = bookList[j + 1] Book book = bookList.getPos(j+1); bookList.setBook(j,book); } bookList.setBook(currentSize,null); bookList.setUsedSize(currentSize - 1); System.out.println("删除成功!"); } }
4、DisplayOperation(打印图书功能)
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++) { Book book = bookList.getPos(i); System.out.println(book); } } }
5、ExitOperation(退出图书管理系统)
public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("退出系统!"); System.exit(0); } }
6、FindOperation(查找图书功能)
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 size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getPos(i); if (book.getName().equals(name)) { System.out.println("找到了这本书,信息如下:"); System.out.println(book); return; } } System.out.println("没有找到这本书!"); } }
7、ReturnOperation(归还图书功能)
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 book = bookList.getPos(i); if (name.equals(book.getName())) { book.setBorrowed(false); System.out.println("归还成功!"); System.out.println(book); return; } } System.out.println("没有你要归还的这本书!"); } }
注意:在我们使用next和nextLine时要注意区分,如果使用nextLine要记得不要输入空白字符串,不然nextLine也会将空格字符直接录入。且Scanner最好随用随定义,不要在接口中定义再用nextLine,不然我们读取的第一个元素会自动变成空白!!!
感谢各位读者的阅读,本文章有任何错误都可以在评论区发表你们的意见,我会对文章进行改正的。如果本文章对你有帮助请动一动你们敏捷的小手点一点赞,你的每一次鼓励都是作者创作的动力哦!😘