目录
各操作类都要实现该接口,以便后期组织各操作类的对象:
- package operation;
-
- import Book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
Book包里面存放book类及BookList类,相关代码如下:
- package 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)?", 已被借出":", 未被借出")
- //", isBorrowed=" + isBorrowed+
- +'}';
- }
- }
BookList类是用来实现存放图书的功能的,相当于书架。在这里我设置的书架上最多能存放10本书( int DEFAULT_CAPACITY = 10;),并且书架上已存有三国演义、西游记、红楼梦三本书。
- package Book;
-
- public class BookList {
- private book[] books;
- private int usedSize; //记录当前书架上实际存放的书的数量
- private static final int DEFAULT_CAPACITY = 10;
-
- public BookList() {
-
- this.books = new book[DEFAULT_CAPACITY];
- this.books[0] = new book("三国演义","罗贯中",10,"小说");
- this.books[1] = new book("西游记","吴承恩",9,"小说");
- this.books[2] = new book("红楼梦","曹雪芹",19,"小说");
-
- this.usedSize = 3;
- }
-
- public book getBook(int pos) {
- return books[pos];
- }
-
- public void setBooks(int pos, book book) {
- this.books[pos] = book;
- }
-
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
-
- public book[] getBooks() {
- return books;
- }
- }
operation包里面存放实现对图书进行操作的类,相关代码如下:
该类实现新增图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
- 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("请输入类型:");
- String type = scanner.nextLine();
- System.out.println("请输入价格:");
- int price = scanner.nextInt();
-
- book book = new book(name,author,price,type);
-
- //检查数组中有没有 这本书
- int currentSize = bookList.getUsedSize();
- for(int i = 0;i
- book book1 = bookList.getBook(i);
- if(book1.getName().equals(name)) {
- System.out.println("已有这本书,不再进行存放了!");
- return;
- }
- }
- if(currentSize==bookList.getBooks().length) {
- System.out.println("书架满了!");
- }else {
- bookList.setBooks(currentSize,book);
- bookList.setUsedSize(currentSize+1);
- }
- }
- }
3.2 DelOperation类
该类实现删除图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
- 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 currentSize = bookList.getUsedSize();
- int pos = -1;
- int i = 0;
- for (; i < currentSize; i++) {
- book book = bookList.getBook(i);
- if(book.getName().equals(name)) {
- pos = i;
- break;
- }
- }
- if (i>=currentSize) {
- System.out.println("没有你要删除的图书!");
- return;
- }
- //删除图书
- int j = pos;
- for (;j
1;j++) { - bookList.setBooks(j,bookList.getBook(j+1));
- }
- bookList.setBooks(j,null);
- bookList.setUsedSize(currentSize-1);
- }
- }
3.3 ShowOperation类
该类实现显示图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
- public class ShowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("显示图书");
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- book book = bookList.getBook(i);
- System.out.println(book);
-
- }
- }
- }
3.4 BorrowOperation类
该类实现普通用户借阅图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
- 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();
- int currenSize = bookList.getUsedSize();
- int i=0;
- for (;i
- book book = bookList.getBook(i);
- if(book.getName().equals(name)) {
- book.setBorrowed(true);
- System.out.println("借阅成功!");
- System.out.println(book);
- return;
- }
- }
- if (i == currenSize) {
- System.out.println("没有你要借阅的图书!");
- }
- }
- }
3.5 ReturnOperation类
该类实现普通用户归还图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
-
- 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();
- int currenSize = bookList.getUsedSize();
- int i=0;
- for (;i
- book book = bookList.getBook(i);
- if(book.getName().equals(name)) {
- book.setBorrowed(false);
- System.out.println("归还成功!");
- System.out.println(book);
- return;
- }
- }
- System.out.println("你归还的图书不存在!");
- }
- }
3.6 FindOperation类
该类实现查找图书的功能
- package operation;
- import Book.BookList;
- import Book.book;
- 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 currenSize = bookList.getUsedSize();
- int i = 0;
- for (;i
- book book = bookList.getBook(i);
- if(book.getName().equals(name)) {
- System.out.println("找到了,信息如下:");
- System.out.println(book);
- return;
- }
- }
- if (i >= currenSize) {
- System.out.println("你要找的书不存在!");
- }
- }
- }
3.7 ExitOperation类
该类实现退出程序的功能
- package operation;
- import Book.BookList;
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出程序");
- System.exit(0);
- }
- }
4、创建user包
user包里面装入User类、NomalUser类和AdminUser类, NomalUser类(普通用户)和AdminUser类(管理员用户)分别继承User类。
4.1 User类
- package user;
-
- import Book.BookList;
- import operation.IOperation;
-
- public abstract class User {
- protected String name;
- protected IOperation[] iOperation;
- public User(String name) {
- this.name = name;
- }
- public abstract int menu();
- public void doOperation(int choice, BookList bookList) {
- iOperation[choice].work(bookList);
- }
- }
4.2 NomalUser类
- package user;
- import operation.IOperation;
- import operation.*;
- import java.util.Scanner;
- public class NomalUser extends User{
- public NomalUser(String name) {
- super(name);
- this.iOperation = new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new BorrowOperation(),
- new ReturnOperation()
- };
- }
- public int menu() {
- System.out.println("**********普通用户******");
- 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);
- System.out.println("请输入你的操作:");
- int choice = scanner.nextInt();
-
- return choice;
- }
- }
4.3 AdminUser类
- package user;
- import operation.IOperation;
- import operation.*;
- import java.util.Scanner;
- public class AdminUser extends User{
- public AdminUser(String name) {
- super(name);
- this.iOperation = new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new AddOperation(),
- new DelOperation(),
- new ShowOperation(),
- };
- }
- public int menu() {
- System.out.println("**********管理员用户*****");
- 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);
- System.out.println("请输入你的操作:");
- int choice = scanner.nextInt();
-
- return choice;
- }
- }
三、 主方法的实现
- import Book.BookList;
- import user.AdminUser;
- import user.NomalUser;
- 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:管理员 2:普通用户->");
- int choice = scanner.nextInt();
- if(choice == 1){
- return new AdminUser(name);
- }else {
- return new NomalUser(name);
- }
- }
-
- public static void main(String[] args) {
- BookList bookList = new BookList();
- User user = login();
- while (true) {
- int choice = user.menu();
- // System.out.println("choice:"+choice);
- user.doOperation(choice,bookList);
- }
- }
- }
这个图书管理系统的缺点:
- 没有做到持久化的存储(比如,管理员增加了一本图书后,在下一次运行的时候,增加的图书就不存在了
- 只是用到了数组
-
相关阅读:
Facebook群控:利用代理IP克服多账号关联
一键让你使用录屏
2023-9-30 JZ34 二叉树中和为某一值的路径
C++秋招经验贴
信号量机制的实现
什么是Docker容器?Docker容器和VM有什么区别?
[linux] 安装WEB服务器(tomcat),环境是32位
重学设计模式之-桥接模式
零钱兑换问题
[CISCN 2019初赛]Love Math - RCE(异或绕过)
-
原文地址:https://blog.csdn.net/m0_61876562/article/details/133811874