目录
这是一个对于Java中知识点的类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习,对于实际的图书管理系统还需要一点的优化
我们先来看看效果
首先是我们的管理员端:
再下是我们的普通用户端:
我们先试着模块化出来功能合计
1.简单的登录
2.管理端
·查阅图书
·增加图书
·删除图书
·打印图书
·退出系统
3.用户端
·查阅图书
·借阅图书
·归还图书
·退出
我们先将类中的功能创建好
IOperation为我们所有功能的接口 很多童鞋就疑惑为何要这么做,我们直接将其定义到一个类中不可以吗,答案是可以的!,但是我们为了链接知识点 所以将其进行分化!
我们先来定义一个书的类,首先一本书有书名、作者、价格、类型以及是否已被借出
-
- public class Book {
- private String name;//书名
- private String author;//作者
- private int price;//价格
- private String type;//类型
- private boolean isBorrwed;//是否借出
- }
-
-
因为这些都是private类型 所以我们还需要提供一个方法来提供给使用者来调用
教大家快捷方法为Alt+Insert
-
- 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 isBorrwed() {
- return isBorrwed;
- }
-
- public void setBorrwed(boolean borrwed) {
- isBorrwed = borrwed;
- }
-
- @Override
- public String toString() {
- return "book{" +
- " 书名='" + name + '\'' +
- ", 作者='" + author + '\'' +
- ", 价格=" + price +
- ", 类型='" + type + '\'' +
- ((isBorrwed==true)?", 已被借出":", 未被借出") +
- '}';
- }
-
-
我们创建一个BookList类当做书架,用来存放我们的书籍
-
- public class BookList {
- private Book[] books=new Book[10];
- private int BookSize;
- public BookList(){
- books[0]=new Book("三国演义","罗贯中",17,"小说");
- books[1]=new Book("西游记","吴承恩",47,"小说");
- books[2]=new Book("水浒传","施耐庵",37,"小说");
- this.BookSize=3;
- }
-
- public int getBookSize() {
- return BookSize;
- }
-
- public void setBookSize(int bookSize) {
- BookSize = bookSize;
- }
-
- /**
- * 获取到pos位置的一本书
- * @param pos
- * @return
- */
- public Book getpos(int pos){
- return books[pos];
- }
-
- /**
- * 新加入一本书
- * @param pos
- * @param book
- */
- public void setBooks(int pos,Book book){
- this.books[pos]=book;
- }
- }
-
-
我们现在可以看到已经对书架进行了初始化,现在书架上的书有三国演义、西游记、水浒传
我们要使用User来实现多态继承
- 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);
- }
- }
管理员要实现的功能为查找、新增、删除、打印、退出系统
- 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("Hai!"+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);
- return scanner.nextInt();
- }
- }
AdminUser类继承了User用户类,并且实现了管理员对应的功能
普通用户要实现的功能为查找、借阅、归还、退出系统
- public class NormalUser extends User {
- public NormalUser(String name){
- super(name);
- this.iOperations =new IOperation[]{
- new Exitoperation(),
- new Findoperation(),
- new Borrowoperation(),
- new Retoperation()
-
- };
- }
- public int menu(){
- System.out.println("=======用户菜单=======");
- System.out.println("Hai!"+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);
- return scanner.nextInt();
- }
- }
- public interface IOperation {
- Scanner scanner=new Scanner(System.in);
-
- public void work(BookList bookList);
- }
我们此功能实现是非常简单的,保存好用户要查找的图书并且使用类name.equals()来查找图书即可
- public class Findoperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("查找图书");
- System.out.println("请输入你要查找的书名");
- String name=scanner.nextLine();
- int size=booklist.getBookSize();
- for (int i = 0; i < size; i++) {
- Book book= booklist.getpos(i);
- if(name.equals(book.getName())){
- System.out.println("找到了你要查找的书 信息如下");
- System.out.println(book);
- return ;
- }
- }
- System.out.println("没有找到你要查找的书");
- }
- }
我们要给已存在的书架加入一本书,将新书存放到顺序表之后即可
- public class Addoperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("添加图书");
- 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 size=booklist.getBookSize();
- booklist.setBooks(size,book);
- booklist.setBookSize(++size);
- System.out.println("新增图书成功");
- }
- }
需要注意的地方是我们一定要切记添加图书后记得要改变bookSize的值
此功能的实现与查找类似,在查找的基础上增加了删除,需要用到的知识点为顺序表的删除,删除需要将后续的书前移,并且将最后一个置为 空
- public class Deloperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("删除图书");
- System.out.println("请输入要删除的图书的名字");
- String name=scanner.nextLine();
- int size= booklist.getBookSize();
- for (int i = 0; i < size; i++) {
- Book book= booklist.getpos(i);
- if(name.equals(book.getName())){
- while(i
1){ - Book tmp=booklist.getpos(i+1);
- booklist.setBooks(i,tmp);
- i++;
- }
- booklist.setBooks(size,null);
- booklist.setBookSize(size-1);
- System.out.println("删除成功");
- return ;
- }
- }
- System.out.println("没有找到你要删除的图书");
- }
- }
切记将书架的最后一位置为空避免泄露,需要注意到的还是要将bookSize的值进行修改
首先利用查找的原理找到我们要寻找的书,找到后修改其状态即可
- public class Borrowoperation implements IOperation {
- public void work(BookList booklist) {
- System.out.println("借阅图书");
- System.out.println("请输入你要借的书名");
- String name=scanner.nextLine();
- int size=booklist.getBookSize();
- for (int i = 0; i
- Book book=booklist.getpos(i);
- if(name.equals(book.getName())){
- book.setBorrwed(true);
- System.out.println("借阅成功!");
- System.out.println(book);
- return ;
- }
- }
- System.out.println("没有此书");
- }
- }
需要注意的是记得将要借阅的书类型修改为true
DisOperation 打印图书
按照顺序表原理依次打印即可
- public class Displayoperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("打印图书");
- int size= booklist.getBookSize();
- for (int i = 0; i < size; i++) {
- Book book= booklist.getpos(i);
- System.out.println(book);
- }
- }
- }
RetOperation归还图书
首先要查找书库是否存在此书,如果存在将其类型修改为false
- public class Retoperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("归还图书");
- System.out.println("请输入你要归还的书名");
- String name=scanner.nextLine();
- int size=booklist.getBookSize();
- for (int i = 0; i
- Book book=booklist.getpos(i);
- if(name.equals(book.getName())){
- book.setBorrwed(false);
- System.out.println("归还成功!");
- System.out.println(book);
- return ;
- }
- }
- System.out.println("没有此书");
- }
-
- }
ExitOperation退出系统
退出系统
- public class Exitoperation implements IOperation{
- public void work(BookList booklist) {
- System.out.println("退出系统");
- System.exit(0);
- }
- }
整合
我们需要使用Main来将所有的功能进行串联
- public class Main {
- public static User login(){
- System.out.println("请输入你的姓名");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.next();
- System.out.println("请输入你的身份 1--》管理员 0--》普通用户");
- int choice=scanner.nextInt();
- if(choice==1){
- return new AdminUser(name);
- }else if(choice==0) {
- return new NormalUser(name);
- }
- else {
- System.out.println("输入错误,退出系统");
- return null;
- }
- }
- public static void main(String[] args) {
- BookList bookList=new BookList();
- User user=login();//发生了向上转型
- while(true){
- int choice=user.menu();//发生了动态绑定 多态
- //根据choice调用合适的操作
- user.dowork(choice,bookList);
- }
- }
- }
我们来看一下代码写完之后的所有文件
总结
我们现在实现的还是比较简易的,后期将结合MySQL来进行优化,并且在代码实现中也有很多业务上的处理没有实现,后期将会进行修正,感谢大家的支持
升级
我们对本图书馆进行了升级,升级内容如下
管理员系统:
用户系统:
升级后的目录
对于其他的升级都是次要的 ,来重点讲解一下使用类型排序,首先我们分析到对于数据的排序是使用到方法Arrays.sort(),但是对于我们Book的排序呢,首先我深入研究了一下sort排序内部,发现 他是在依靠c来实现排序
以下代码为JDK内置排序原码
- public static
void sort(T[] a, int fromIndex, int toIndex, - Comparator super T> c) {
- if (c == null) {
- sort(a, fromIndex, toIndex);
- } else {
- rangeCheck(a.length, fromIndex, toIndex);
- if (LegacyMergeSort.userRequested)
- legacyMergeSort(a, fromIndex, toIndex, c);
- else
- TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);
- }
- }
我们进入Comparator来观察一下
我们发现它是一个接口 我来给大家解读一下
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。
我们可以得到 这个接口为一个公共接口比较器,所以我们可以得到,通过类接入接口我们就可以做一个属于自己的比较器!有了以上分析 开搞
- class A implements Comparator
{ -
- @Override
- public int compare(Book o1, Book o2) {
- int a= o1.getType().compareTo(o2.getType());
- return a;
- }
- }
搞好这个比较器类型就可以实现一下我们的图书整理功能了(使用书的其他类型也可以实现,可以加入多个比较器)
- public class Finishing implements IOperation{
-
- @Override
- public void work(BookList booklist) {
- A a=new A();
- System.out.println("整理图书(按照类型)");
- Arrays.sort(booklist.books,0,booklist.getBookSize(),a);
- System.out.println("整理完成!");
- }
- }
这样便实现了我们的图书整理功能,再接入给管理员用户就可以使用啦
ps:其他功能实现起来非常简单,稍加分析即可写入 就不做讲解了,很多老铁想让我发一下所有源码,满足老铁们
源码 (更新后的)(绿色为包名,紫色为类,橙色为接口)
BookList
Book
- package BoolList;
-
- public class Book {
- private String name;//书名
- private String author;//作者
- private String type;//类型
- private int price;//价格
- private boolean isBorrwed;//状态
-
- public Book(String name, String author, int price , String type ) {
- this.name = name;
- this.author = author;
- this.type = type;
- this.price = price;
- }
-
- public String getType() {
- return type;
- }
-
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public boolean isBorrwed() {
- return isBorrwed;
- }
-
- public void setBorrwed(boolean borrwed) {
- isBorrwed = borrwed;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", type='" + type + '\'' +
- ", price=" + price +
- (isBorrwed==true?"已被借出":"未被借出")+
- '}';
- }
- }
BookList
- package BoolList;
-
- public class BookList {
- public Book[] books=new Book[10];
- private int BookSize=0;
-
- public BookList() {
- books[0]=new Book("三国演义","罗贯中",17,"小说");
- books[1]=new Book("西游记","吴承恩",47,"小说");
- books[2]=new Book("水浒传","施耐庵",37,"小说");
- BookSize=3;
- }
-
- public void setBooks(int pos,Book books) {
- this.books[pos]=books;
- }
-
- public Book getBooks(int pos) {
- return books[pos];
- }
-
- public int getBookSize() {
- return BookSize;
- }
-
- public void setBookSize(int bookSize) {
- BookSize = bookSize;
- }
-
- }
Main
Main
- package Main;
-
- import BoolList.Book;
- import BoolList.BookList;
- import User.*;
-
- import java.util.Scanner;
-
-
- public class Main {
-
- public static User func(){
- System.out.println("欢迎来到冰激凌图书馆!");
- System.out.println("请问怎么称呼您?");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- System.out.println("请问您的身份为? 1-》管理员 0-》普通用户");
- int usersize=scanner.nextInt();
- if(usersize==1){
- return new Administratoruser(name);
- }else {
- return new Ordinaryuser(name);
- }
- }
-
- public static void main(String[] args) {
- Needtoknow();
- BookList bookList=new BookList();
- User user=func();
- while(true){
- int count=user.mune();
- user.dowork(count,bookList);
- }
- }
- public static void Needtoknow(){
- System.out.println("**************************");
- System.out.println(" 入馆需知(新)");
- System.out.println("本图书馆开放时间为9.00-24.00");
- System.out.println("**************************");
-
- }
- }
Operation
IOperation
- import BoolList.BookList;
-
- public interface IOperation {
- public void work(BookList booklist);
- }
AddOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.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();
- System.out.println("请输入图书的价格");
- int price=scanner.nextInt();
- Book book=new Book(name,author,price,type);
- int size=booklist.getBookSize();
- booklist.setBooks(size,book);
- booklist.setBookSize(++size);
- System.out.println("新增图书成功");
-
- }
- }
BorrowOperation
- package Operation;
-
- import BoolList.BookList;
-
- import java.util.Scanner;
-
- 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 siz=booklist.getBookSize();
- for (int i = 0; i < siz; i++) {
- if(name.equals(booklist.getBooks(i).getName())){
- booklist.getBooks(i).setBorrwed(true);
- System.out.println("成功借阅 ,现在图书信息如下:");
- System.out.println(booklist.getBooks(i));
- return ;
- }
- }
- System.out.println("未找到你要查找的书!!!");
- }
- }
DelOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.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();
- for (int i = 0; i < booklist.getBookSize();i++) {
- if(name.equals(booklist.getBooks(i).getName())){
- while(i
1){ - booklist.setBooks(i, booklist.getBooks(i+1));
- i++;
- }
- booklist.setBooks(i,null);
- booklist.setBookSize(i);
- System.out.println("删除成功!");
- return ;
- }
- }
- System.out.println("未找到你要删除的书!");
- }
- }
DisOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.BookList;
-
- public class DisOperation implements IOperation{
- @Override
- public void work(BookList booklist) {
- System.out.println("打印图书");
- int ret= booklist.getBookSize();
- for (int i = 0; i
- System.out.println(booklist.getBooks(i));
- }
- }
- }
ExitOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.BookList;
-
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList booklist) {
- System.out.println("退出登录");
- System.exit(0);
- }
- }
FindOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.BookList;
-
- import java.util.Scanner;
-
- 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();
- for (int i = 0; i
- if(name.equals(booklist.getBooks(i).getName())){
- System.out.println("找到了你要查找的书,信息如下:");
- System.out.println(booklist.getBooks(i));
- return ;
- }
- }
- System.out.println("未找到你要查找的书!!!");
- }
-
- }
FindType
- package Operation;
-
- import BoolList.BookList;
-
- import java.util.Scanner;
-
- public class FindType 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 flg=0;
- System.out.println("查找信息如下:");
- for (int i = 0; i
- if(name.equals(booklist.getBooks(i).getType())){
- flg++;
- System.out.println(booklist.getBooks(i));
- }
- }
- if(flg==0){
- System.out.println("暂无该类型的书!!!");
- }
- }
- }
Notice
- package Operation;
-
- import BoolList.BookList;
-
- import java.util.Scanner;
-
- public class Notice implements IOperation{
- @Override
- public void work(BookList booklist) {
- System.out.println("*****************************");
- System.out.println("图书馆通知栏:");
- System.out.println("热烈祝贺二十大的召开!");
- System.out.println("国家图书馆第十八届文津图书奖正式启动");
- System.out.println("本图书馆开放时间为9.00-24.00");
- System.out.println("*****************************");
-
- }
- }
RetOperation
- package Operation;
-
- import BoolList.Book;
- import BoolList.BookList;
-
- import java.util.Scanner;
-
- public class RetOperation implements IOperation{
- @Override
- public void work(BookList booklist) {
- System.out.println("归还图书");
- System.out.println("请输入你要归还的书名 例如:三国演义");
- Scanner scanner=new Scanner(System.in);
- String name=scanner.nextLine();
- for (int i = 0; i
- if(name.equals(booklist.getBooks(i).getName())){
- booklist.getBooks(i).setBorrwed(false);
- System.out.println("归还成功!信息如下:");
- System.out.println(booklist.getBooks(i));
- return ;
- }
- }
- System.out.println("本图书馆没有此书,归还失败");
- }
- }
Finishing
- package Operation;
-
- import BoolList.Book;
- import BoolList.BookList;
-
- import java.util.Arrays;
- import java.util.Comparator;
-
- class A implements Comparator
{ -
- @Override
- public int compare(Book o1, Book o2) {
- int a= o1.getType().compareTo(o2.getType());
- return a;
- }
- }
- public class Finishing implements IOperation{
-
- @Override
- public void work(BookList booklist) {
- A a=new A();
- System.out.println("整理图书(按照类型)");
- Arrays.sort(booklist.books,0,booklist.getBookSize(),a);
- System.out.println("整理完成!");
- }
- }
user
User
- package User;
-
-
- import BoolList.Book;
- import BoolList.BookList;
- import Operation.IOperation;
-
- public abstract class User {
- protected String name;
- protected IOperation[] iOperations;
- public User(String name) {
- this.name = name;
- }
-
- public abstract int mune();
-
- public void dowork(int pos, BookList book){
- iOperations[pos].work(book);
- }
- }
Administratoruser
- package User;
-
-
-
-
- import Operation.*;
-
- import java.util.Scanner;
-
- public class Administratoruser extends User{
-
- public Administratoruser(String name) {
- super(name);
- this.iOperations= new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new FindType(),
- new AddOperation(),
- new DelOperation(),
- new DisOperation(),
- new Finishing()
- };
- }
-
-
-
- @Override
- public int mune() {
- System.out.println("hello 尊敬的管理员:"+this.name+" 欢迎来到冰激凌图书馆");
- System.out.println("======================");
- System.out.println("1.查阅图书");
- System.out.println("2.类型查找(新)");
- System.out.println("3.增加图书");
- System.out.println("4.删除图书");
- System.out.println("5.显示图书");
- System.out.println("6.整理图书(新)");
- System.out.println("0.退出登录");
- System.out.println("======================");
- System.out.println("请输入您的操作");
- Scanner scanner=new Scanner(System.in);
- int input=scanner.nextInt();
- return input;
- }
- }
Ordinaryuser
- package User;
-
- import Operation.*;
-
- import java.util.Scanner;
-
- public class Ordinaryuser extends User {
-
- public Ordinaryuser(String name){
- super(name);
- this.iOperations=new IOperation[]{
- new ExitOperation(),
- new FindType(),
- new BorrowOperation(),
- new RetOperation(),
- new Notice(),
- new DisOperation()
- };
- }
-
- @Override
- public int mune() {
- 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("5.查看书架(新)");
- System.out.println("0.退出登录");
- System.out.println("====================");
- System.out.println("请输入您的操作:");
- Scanner scanner=new Scanner(System.in);
- return scanner.nextInt();
- }
-
-
- }
好了 !再次感谢各位支持
-
相关阅读:
6 个可解锁部分 GPT-4 功能的 Chrome 扩展(无需支付 ChatGPT Plus 费用)
强迫症SuppressWarnings各场景的使用
接着聊聊如何从binlog文件恢复误delete的数据,模拟Oracle的闪回功能
hive数据库将非分区表数据迁移到分区表
删除链表结点类问题
WLAN网络配置,vlan内漫游
这么牛的代码 你能看懂吗
echarts 图片导入到 word pdf
day27-完全平方数(背包问题)
分层化网络设计:核心层,汇聚层,接入层
-
原文地址:https://blog.csdn.net/m0_69996872/article/details/127589425