运用JavaSE知识实现图书管理系统

目录

  • 一.Main函数
  • 二.用户类
  • 三.普通用户类
  • 四.管理员类
  • 五.图书类
  • 六.书架类
  • 七.操作类
    • 1.操作接口
    • 2.增加操作
    • 3.删除操作
    • 4.查找操作
    • 5.展示操作
    • 6.借阅操作
    • 7.归还操作
    • 8.退出系统
  • 总结

这篇图书管理系统是对JavaSE知识总结复习的一个小作业,检测自己对知识的掌握程度。

一.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.管理员 2.普通用户");System.out.println("输入你的身份");int choice = scanner.nextInt();if (choice == 1){return new Admin(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);}}
}

二.用户类

public abstract class User {private String name;protected IOperation[] iOperations;public User(String name){this.name = name;IOperation[] iOperations;}public abstract int menu();public void doOperation(int choice,BookList bookList){IOperation iOperation = iOperations[choice];iOperation.work(bookList);}
}

三.普通用户类

public class NormalUser extends User{public NormalUser(String name) {super(name);this.iOperations =new IOperation[]{new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};}@Overridepublic 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);int choice = scanner.nextInt();return choice;}
}

四.管理员类

public class Admin extends User{public Admin(String name) {super(name);this.iOperations =new IOperation[] {new ExitOperation(),new FindOperation(),new AddOperation(),new DeleOperation(),new ShouwOperation()};}@Overridepublic 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);int choice = scanner.nextInt();return choice;}
}

五.图书类

public class Book{//书名 作者 价格 类型 是否借出private String name;private String author;private int price;private String type;private Boolean isBorrowed = false;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;}public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +", isBorrowed=" + isBorrowed +'}';}
}

六.书架类

public class BookList {public Book[] books;public int usedSize;public BookList(){this.books = new Book[10];books[0] = new Book("三国演义","罗贯中",46,"小说");books[1] = new Book("水浒传","施耐庵",37,"小说");books[2] = new Book("红楼梦","曹雪芹",76,"小说");books[3] = new Book("西游记","吴承恩",88,"小说");usedSize = 4;}public Book getBook(int pos) {return books[pos];}public void setBook(Book book,int pos) {this.books[pos] = book;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}
}

七.操作类

1.操作接口

public interface IOperation {void work(BookList bookList);Scanner scanner = new Scanner(System.in);
}

2.增加操作

public class AddOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("请输入你要增加图书的书名");String name = scanner.nextLine();System.out.println("请输入你要添加书的作者");String author = scanner.nextLine();System.out.println("请输入你要添加书的价格");int price = scanner.nextInt();System.out.println("请输入你要添加书的类型");scanner.nextLine();String type = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {if(bookList.getBook(i).getName().equals(name)){System.out.println("该书已经存在,无法添加!");return;}}//书不存在 进行添加操作Book book = new Book(name,author,price,type);bookList.setBook(book,currentSize);bookList.setUsedSize(currentSize+1);}
}

3.删除操作

public class DeleOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("请输入你要删除书的书名:");String name = scanner.nextLine();int index = -1;int i =0;int currentSize = bookList.getUsedSize();for ( i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)){index = i;break;}}if (i >= currentSize){System.out.println("要删除的书不存在!");return;}for (int j = index; j < currentSize-1; j++) {Book book = bookList.getBook(j+1);bookList.setBook(book,j);}bookList.setBook(null,currentSize);bookList.setUsedSize(currentSize-1);System.out.println("删除成功!!!");}
}

4.查找操作

public class FindOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("请输入你要查找的书的书名:");String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)){System.out.println(book);return;}}System.out.println("图书馆没有你要寻找的书!");}
}

5.展示操作

public class ShouwOperation implements IOperation{@Overridepublic 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);}}
}

6.借阅操作

public class BorrowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("图书馆的书如下:");int currentSzie = bookList.getUsedSize();for (int i = 0; i < currentSzie; i++) {Book book = bookList.getBook(i);System.out.println(book.getName());}System.out.println("请输入你要外借的图书的书名:");String name = scanner.nextLine();for (int i = 0; i < currentSzie; i++) {Book book = bookList.getBook(i);if(book.getName().equals(name)){if (book.getBorrowed()){System.out.println("该书已被借出,换本书吧!");return;}else {book.setBorrowed(true);System.out.println("借阅成功");System.out.println("该书的信息如下");System.out.println(book);return;}}}System.out.println("你想借阅的图书不存在!");}
}

7.归还操作

public class ReturnOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("归还图书");System.out.println("请输入你要归还的图书的书名");String name = scanner.nextLine();int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)){book.setBorrowed(false);System.out.println("归还成功!");return;}}System.out.println("不存在你要归还的图书!");}
}

8.退出系统

public class ExitOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统!");System.exit(0);}
}

总结

以上就是图书管理系统的所有知识,有些地方还是需要思考去巧妙地设计使得文章更加简洁明了。冰冻三尺,非一日之寒。水滴石穿,非一日之功。坚持把每一个知识点搞清楚,并进行总结,形成属于自己的知识框架,会让你的学习更加轻松。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310493.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[C++STL]C++实现string容器

代码如下: #pragma once #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <assert.h> #include <cstring> using namespace std;class String { public:String(const char *str ""){assert(str ! nullptr);_size strlen(str);_s…

ASP.NET Core 日志框架:Serilog

在 ASP.NET Core 日志模型 中对日志整体实现方式进行了介绍&#xff0c;通过使用内置日志记录器来实现日志的输出路径。而在实际项目开发中&#xff0c;使用第三方日志框架来记录日志也是非常多的&#xff0c;首先一般基础的内置日志记录器在第三方日志框架中都有实现&#xff…

[C++STL]C++实现vector容器

代码如下: #pragma once #include <iostream> #include <assert.h> using namespace std;template<typename T> class Vector { public:typedef T* iterator;typedef const T* const_iterator;Vector() :_start(nullptr), _finish(nullptr), _endOfStorage(…

数据结构与算法--简单栈实现及其应用

栈 栈&#xff08;Stack&#xff09;是一种限制插入和删除只能在一个位置上进行的表&#xff0c;改位置是表的末端&#xff0c;叫做栈顶top。栈的基本操作有push &#xff08;进栈&#xff09;pop&#xff08;出栈&#xff09;栈又叫做LIFO&#xff08;后进先出&#xff09;表…

树莓派销量突然猛增

树莓派基金会负责人 Eben Upton 近日在推特上公布&#xff0c;树莓派在三月份的销量达到 640,000 套&#xff0c;三月也成为有史以来销售量第二高的时期。Eben 认为&#xff0c;这大概是由于受 COVID-19 冠状病毒肺炎影响&#xff0c;在居家隔离期间&#xff0c;树莓派能够以低…

[C++STL]C++实现list容器

代码如下&#xff1a; #pragma once #include <iostream> using namespace std;template<typename T> struct ListNode {T _data;ListNode<T> *_next;ListNode<T> *_prev;ListNode(const T & val T()) :_data(val), _next(nullptr), _prev(nullp…

Java动态追踪技术--BTrace

Java动态追踪技术 需求翻译官的日常工作不是在的改bug&#xff0c;发布代码&#xff0c;就是在加日志查找bug的路上。查BUG的过程是痛苦的&#xff0c;我们总是在不停的看代码&#xff0c;修改代码&#xff0c;添加日志&#xff0c;从而帮助我们发现问题&#xff0c;这种形式是…

C#黔驴技巧之实现统计结果排名

本节是实现统计排名的一点技巧&#xff0c;可能有一部分童鞋在实现排名统计绕了一大圈&#xff0c;最后还不如两行代码就搞定&#xff0c;代码清晰而可读性强&#xff0c;接下来我们来一起来看看。我们知道在SQL Server中可以利用Row_Number、Rank等函数进行排名&#xff0c;在…

数据结构与算法--二叉树实现原理

二叉树 二叉树&#xff08;binary tree&#xff09;是一棵树&#xff0c;其中每个节点都不能有多于两个的子节点二叉树的一个性质是一颗平均二叉树的深度要比节点个数N小得多&#xff08;重点&#xff09;&#xff0c;对二叉树的分析得出其平均深度为O(N\sqrt NN​)&#xff0…

[C++STL]C++实现stack容器适配器

代码如下: #include <iostream> #include <deque> using namespace std;template<typename T,typename Con deque<T>> class Stack { public:Stack() {}void Push(const T &x) {_c.push_back(x);}void Pop(){_c.pop_back();}T & Top(){retur…

Istio Pilot架构解析

本文节选自 ServiceMesher 社区联合编写的《Istio Handbook——Istio 服务网格进阶实战》。本书地址&#xff1a;https://github.com/servicemesher/istio-handbook/在应用从单体架构向微服务架构演进的过程中&#xff0c;微服务之间的服务发现、负载均衡、熔断、限流等服务治理…

数据结构与算法--二叉查找树实现原理

二叉查找树 二叉树的一个重要应用就是他在查询中的使用&#xff0c;假设书中每个节点存储一项数据。在我们的案例中&#xff0c;任意复杂的项在java中都容易处理&#xff0c;但为了简单还是假设都是整数。还假设他们都是不重复的整数&#xff0c;使二叉树称为二叉查找树的性质…

[C++STL]C++实现queue容器适配器

代码如下: #include <iostream> #include <deque>using namespace std;template<typename T,typename Con deque<T>> class Queue { public:Queue(){}void Push(const T & x){_c.push_back(x);}void Pop(){_c.pop_front();}T &Back(){return…

当模板方法遇到了委托函数,你的代码又可以精简了

现如今当你翻看一些开源项目源码的时候&#xff0c;你会发现现在到处充斥着委托函数&#xff0c;如Func,Action,Predicate&#xff0c;确实现在的C#在函数式编程 的路上越来越成为主流&#xff0c;越来越显示威力&#xff0c;曾经的一些经典设计模式写法&#xff0c;在函数式下…

数据结构与算法--面试必问AVL树原理及实现

数据结构与算法–AVL树原理及实现 AVL&#xff08;Adelson-Velskii 和landis&#xff09;树是带有平衡条件的二叉查找树&#xff0c;这个平衡条件必须容易实现&#xff0c;并且保证树的深度必须是O(logN)。因此我们让一棵AVL树中每个节点的左子树和右子树的高度最多相差1&…

C语言 实现堆

代码如下: #include <stdio.h> #include <assert.h> #include <stdlib.h> #include <string.h>typedef int HDataType;//堆的定义 typedef struct heap {HDataType *data;int size;int capacity; }heap;//交换 void Swap(int *a, int *b) {int temp …

MySQL8.0新特性

在这之前Mysql的版本是5.7&#xff0c;也是目前使用最广泛的一个版本。现在新版本跳过了6和7直接来到了8&#xff0c;那么V6和V7版本去哪里了呢&#xff1f;比较靠谱的说法是v6用作了内部的其他用途而v7的话是因为mysql有个产品叫做clusterdb他有7这个版本&#xff0c;所以这个…

数据结构与算法--B树原理及实现

B树 前几篇文中讨论的数据结构我们都是假设所有的数据都存储在计算机的主存中。可说总要那么海量的数据需要通过个中数据结构去存储&#xff0c;我们不可能有这么多内存区存放这些数据。那么意味着我们需要将他们放磁盘。所以这个时候范问时间复杂度O决定了他是否能适合存储磁盘…

[C++STL]C++实现priority_queue容器适配器

代码如下: #pragma once #include <iostream> #include <vector> using namespace std;template<typename T> struct Less {bool operator()(const T &a, const T &b){return a < b;} };template<typename T> struct Greater {bool operat…

为什么要用内插字符串代替string.format

知道为什么要用内插字符串&#xff0c;只有踩过坑的人才能明白&#xff0c;如果你曾今使用string.format超5个以上占位符&#xff0c;那其中的痛苦我想你肯定是能够共鸣的。一&#xff1a;痛苦经历先上一段曾今写过的一段代码&#xff0c;大家来体会一下&#xff1a;LogHelper.…