湖南建设工程竣工备案表查询网站,wordpress页脚计时,太原便宜做网站的公司,平台期一般持续多久#x1f937;♀️#x1f937;♀️#x1f937;♀️ 今天给大家分享一下Java实现一个简易的图书管理系统#xff01; 清风的个人主页#x1f389;✏️✏️ #x1f302;c/java领域新星创作者 #x1f389;欢迎#x1f44d;点赞✍评论❤️收藏 #x1f61b;♀️♀️♀️ 今天给大家分享一下Java实现一个简易的图书管理系统 清风的个人主页✏️✏️ c/java领域新星创作者 欢迎点赞✍评论❤️收藏 希望我的文章能对你有所帮助有不足的地方还请各位看官多多指教大家一起学习交流 动动你们发财的小手点点关注点点赞在此谢过啦哈哈哈 目录 一、找到抽象化的对象
1.书类
2.书架类
二、管理员与普通用户登录
三、实现的功能
1.查找图书
2.新增图书(管理员功能)
3.删除图书(管理员功能)
4.显示图书信息
5.退出系统
6.借阅图书(普通用户功能)
7.归还图书(普通用户功能)
四、main方法 图书管理系统源码链接-满船清梦压星河的Gitee 一、找到抽象化的对象
1.书类
经过分析我们可以知道书可以抽象成一个类型。它的属性包括书名作者价格书的类型等等...我们就先以这些为例。为了保持封装性我们把这些属性都设置成private修饰的。
下面是书类的定义代码 这段代码包括一些构造函数以及设置书的属性、重写String函数等。
public class Book {private String name;private String author;private int price;private String type;private boolean isBorrowed;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;}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 \ ((isBorrowedtrue)?已借出:未借出) };}
}
2.书架类
我们可以利用一个数组来存放这些书籍并记录当前存放书籍的数量为后续的增删查改做准备同时初始化有三本书籍。
下面是代码
public class BookList {private Book[] books;private int usedSize;//记录当前书架上实际存放的书的数量public BookList(){this.booksnew Book[10];this.books[0]new Book(三国演义,罗贯中,18,小说);this.books[1]new Book(西游记,吴承恩,28,小说);this.books[2]new Book(红楼梦,曹雪芹,35,小说);this.usedSize3;}//获取当前存放书籍数量public int getUsedSize() {return usedSize;}//设置存放书籍数量public void setUsedSize(int usedSize) {this.usedSize usedSize;}//返回下标为pos的书籍public Book getBook(int pos){return books[pos];}//设置下标为pos位置的书籍为bookpublic void setBook(int pos,Book book){books[pos]book;}//返回书籍这个数组public Book[] getBooks(){return books;}
}
二、管理员与普通用户登录
首先定义一个用户抽象类再定义管理员与普通用户去继承抽象类并重写菜单方法。
下面是用户抽象类代码
abstract public class User {protected String name;protected IOPeration[] ioPerations;public User(String name) {this.name name;}public abstract int menu();public void doOperation(int choice, BookList bookList){ioPerations[choice].work(bookList);}
}
管理员类代码
public class AdiminUser extends User{public AdiminUser(String name){super(name);this.ioPerationsnew 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 scannernew Scanner(System.in);System.out.println(请输入你的选择:);int choicescanner.nextInt();return choice;}
}
普通用户类代码
public class NormalUser extends User{public NormalUser(String name){super(name);this.ioPerationsnew IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowedOperation(),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 scannernew Scanner(System.in);System.out.println(请输入你的选择:);int choicescanner.nextInt();return choice;}
}
三、实现的功能
实现以下几个功能可以定义一个接口方便后续的相关操作。
public interface IOPeration {void work(BookList bookList);
}
1.查找图书
public class FindOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(查找图书:);System.out.println(请输入要查找的书:);Scanner scannernew Scanner(System.in);String namescanner.nextLine();//遍历这个数组int currentSizebookList.getUsedSize();for (int i 0; i currentSize; i) {Book bookbookList.getBook(i);if(book.getName().equals(name)){System.out.println(该书信息如下:);System.out.println(book);return;}}System.out.println(无此书!!!);}
}
2.新增图书(管理员功能)
public class AddOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(新增图书:);int cunrrentSizebookList.getUsedSize();if (cunrrentSizebookList.getBooks().length){System.out.println(书架已满);return;}Scanner scannernew Scanner(System.in);System.out.println(输入要新增书籍:);String namescanner.nextLine();//检查数组当中有没有这本书for (int i 0; i cunrrentSize ; i) {Book book1bookList.getBook(i);if(book1.getName().equals(name)){System.out.println(该书已存放无需新增!!!);return;}}System.out.println(输入书籍作者:);String authorscanner.nextLine();System.out.println(输入书籍类型:);String typescanner.nextLine();System.out.println(输入书籍价格:);int pricescanner.nextInt();Book booknew Book(name,author,price,type);bookList.setBook(cunrrentSize,book);bookList.setUsedSize(cunrrentSize1);System.out.println(新增书籍成功!!!);}
}
3.删除图书(管理员功能)
public class AddOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(新增图书:);int cunrrentSizebookList.getUsedSize();if (cunrrentSizebookList.getBooks().length){System.out.println(书架已满);return;}Scanner scannernew Scanner(System.in);System.out.println(输入要新增书籍:);String namescanner.nextLine();//检查数组当中有没有这本书for (int i 0; i cunrrentSize ; i) {Book book1bookList.getBook(i);if(book1.getName().equals(name)){System.out.println(该书已存放无需新增!!!);return;}}System.out.println(输入书籍作者:);String authorscanner.nextLine();System.out.println(输入书籍类型:);String typescanner.nextLine();System.out.println(输入书籍价格:);int pricescanner.nextInt();Book booknew Book(name,author,price,type);bookList.setBook(cunrrentSize,book);bookList.setUsedSize(cunrrentSize1);System.out.println(新增书籍成功!!!);}
}
4.显示图书信息
public class ShowOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(显示图书:);int currentSizebookList.getUsedSize();for (int i 0; i currentSize; i) {Book bookbookList.getBook(i);System.out.println(book);}}
}
5.退出系统
public class ExitOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(退出系统:);System.exit(0);}
}
6.借阅图书(普通用户功能)
public class BorrowedOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(借阅图书:);/*** 1.你要借阅哪本书* 2.你借阅的书存在吗* 借阅的方式是什么*/Scanner scannernew Scanner(System.in);System.out.println(输入要借阅书籍:);String namescanner.nextLine();int currentSizebookList.getUsedSize();int i 0;for (; i currentSize ; i) {Book bookbookList.getBook(i);if(book.getName().equals(name)){book.setBorrowed(true);System.out.println(借阅成功!!!);return;}}if(icurrentSize){System.out.println(该书不存在无法借阅!!!);}}
}
7.归还图书(普通用户功能)
public class ReturnOperation implements IOPeration{Overridepublic void work(BookList bookList) {System.out.println(归还图书:);Scanner scannernew Scanner(System.in);System.out.println(输入要归还书籍:);String namescanner.nextLine();int currentSizebookList.getUsedSize();int i 0;for (; i currentSize ; i) {Book bookbookList.getBook(i);if(book.getName().equals(name)){book.setBorrowed(false);System.out.println(归还成功!!!);return;}}if(icurrentSize){System.out.println(该书不存在无需归还!!!);}}
}
四、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.普通用户);int choice scanner.nextInt();if (choice 1) {//管理员return new AdiminUser(name);} else {//普通用户return new NormalUser(name);}}public static void main(String[] args) {BookList bookList new BookList();//user指向哪个对象就看返回值是什么User user login();while (true) {int choice user.menu();System.out.println(choice: choice);//根据choice决定调用的是哪个方法user.doOperation(choice, bookList);}}
} 好啦今天的分享就到这里 ✨创作不易还希望各位大佬支持一下 点赞你的认可是我创作的动力 ⭐收藏你的青睐是我努力的方向 ✏️评论你的意见是我进步的财富