💕”生命中的每个人都是一个故事,而每个故事都值得被讲述“💕
🎆作者:不能再留遗憾了🎆
🐼专栏:Java学习🐼
🚗该文章主要内容:用Java实现简单的图书管理系统
前言
各位朋友们大家好,今天我为大家分享是可以被称为小项目的图书管理系统,这个小项目运用了我们前面学习的JavaSe的基本上所有的知识点,所以当我们学习JavaSe到一定程度时,我们可以通过实现一个图书管理系统来检测自己的JavaSe语法掌握的到底怎么样。那么废话不多说,大家就跟着我一起来看看吧。
基本思路
1.因为是图书管理系统,所以我们主要针对的对象是书架,书架上面有不同的书,所以我们首先需要创建出书这个对象,然后就是书架来存放我们的书。
2.用户分为管理员和普通用户,不同的身份所具有的权力也不同,管理员可以查找、新增、删除、显示图书,而普通用户则有查找、借阅、归还的功能,并且这些对象是作用在书架上的书上的。
我们可以先看看管理员身份的图书管理系统
普通用户的图书馆管理系统
书和书架
我们首先创建一个Book包来存放书Book类和书架BookList类
书Book类
我们一本书有书名、作者、类型和价格等属性。我们将书的这些属性都设置为private,防止被别人拿到数据然后恶意修改,只需要提供公共的接口供用户使用数据,增强安全性。
public class Book {
//这里我们成员的属性都设置为private,防止被人恶意修改。增强安全性
private String name;
private String author;
private String type;
private int price;
private boolean isBorrowed;
//构造方法
public Book(String name, String author, String type, int price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
//我们这里重写toString方法,方便我们后面打印
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
(isBorrowed ? " 被借出" : " 未被借出") +
'}';
}
}
书架BookList类
我们使用Java提供的ArrayList链表来充当书架。
public class BookList {
private ArrayList<Book> books = new ArrayList<>();
//默认书架上有三本书
public BookList() {
books.add(new Book("水浒传","罗贯中","小说",89));
books.add(new Book("三国演义","施耐庵","小说",79));
books.add(new Book("西游记","吴承恩","小说",49));
}
public Book getBook(int pos) {
return books.get(pos);
}
public void setBook(int pos,Book book) {
books.set(pos,book);
}
//方便我们获取到书架
public ArrayList<Book> getBooks() {
return books;
}
}
用户身份User
User包创建AdminUser类和NormalUser类
将书和书架完成后,我们就可以继续进行登陆的功能,我们实现的功能需要根据你输入的身份来确定你有哪些权限,所以我们需要创建出AdminUser管理员类和NormalUser普通用户类。不同的身份有共同的特性,所以我们创建出User作为父类,将管理员身份和普通用户具有的共性都提取到User中,降低代码的复用性。
父类User
public class User {
String name;
public User(String name) {
this.name = name;
}
}
子类AdminUser管理员身份
public class AdminUser extends User{
//构造方法
public AdminUser(String name) {
super(name);
}
}
子类NormalUser普通用户身份
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
}
}
登录
当我们创建了管理员身份和普通用户身份后,我们就需要根据你输入的身份来决定创建什么身份的对象。我们在login这个方法中决定你的身份,但是我们方法的返回值该是什么呢?AdminUser?NormalUser?这当然不可以,因为我们不知道用户到底是什么身份,那么我们应该怎么解决呢?
这里就用到了我们前面学到的向上转型,我们在login方法中实现登录功能,login方法的返回值是User类型,因为你的AdminUser和NormalUser都继承了User,所以当你返回AdminUser类和NormalUser类都会发生向上转型,然后我们再用User类型的引用来接收返回值,这样就很好的解决了怎样创建不同身份的对象的问题。
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 NormalUser(name);
}
}
public static void main(String[] args) {
BookList booklist = new BookList();
//用User类型来接收我们的返回值
User user = login();
}
}
菜单
当我们登陆进去后,我们就出现一个菜单提示用户输入,因为就算两种身份功能不同,但是他们都会被提示看到菜单,所以我们同样将menu方法抽取到父类User中,但是我们想一个问题,我们可以在User类中实现方法体吗?
答案是可以的,但是可以是可以,如果你实现的是AdminUser的menu方法,你的子类NormalUser中必须得重写这个方法,你如果实现的是Normaluser也是如此。其实还是比较麻烦的,所以不如直接把User设置成抽象类,将menu设置成抽象方法,不在User中实现方法体,你的不同身份有什么方法,你在对应类中就实现哪些。
User抽象类
public abstract class User {
String name;
public User(String name) {
this.name = name;
}
public abstract int menu();
}
AdminUser类
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
NormalUser类
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
进行具体操作
我们创建出operation包来实现各种功能。
但是我们该如何根据用户输入的数字来实现不同的功能呢?其实我们可以想想之前C语言的计算器,在计算器中,我们将函数地址放在数组中,然后通过数组下标来调用不同的功能?我们用Java也是同样的思想,不同的是我们在数组中存放的不是函数,而是对象。在知道大概的思路后,我们又该怎么定义这个数组的类型呢?你的每个对象的类型都不同。这里就体现了Java
接口的好处了,如果大家不清楚,大家可以去回顾回顾Java抽象类和接口。接口有着一种特性,只要你不同的类实现了同一个接口,即使你们类类型不同,你们同样可以用这个接口的类型来接收。所以我们就创建一个IOperation接口,接口中有work方法,然后不同的功能去实现这个接口,重写work方法。
我们将IOeration数组定义在父类User中,但是我们只是声明,不初始化,让子类AdminUser和Normaluser分别实现这个IOperation数组。
User包补充
User
public abstract class User {
String name;
//创建ioperations这个成员变量
public IOperation[] ioperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
}
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 ShowOperation()
};
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
NormalUser
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
operation包
IOperation接口
public interface IOperation {
void work(BookList bookList);
}
AddOperation类
public class AddOperation implements IOperation{
public void work(BookList bookList) {
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,type,price);
//直接使用ArrayList提供的add方法添加
bookList.getBooks().add(book);
System.out.println("添加成功");
}
}
BorrowedOperation类
public class BorrowedOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("请输入你要借的书的书名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
//判断是否被借出
if(!bookList.getBook(i).isBorrowed()) {
System.out.println("借阅成功");
//将该书籍标记为借出
bookList.getBook(i).setBorrowed(true);
}else {
System.out.println("此书籍已被借出");
}
return;
}
}
System.out.println("未找到该书籍");
}
}
DelOperation类
public class DelOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要删除的书的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
//同样使用ArrayList提供的remove方法
bookList.getBooks().remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("书库中没有此书籍");
}
}
ExitOperation类
public class ExitOperation implements IOperation{
public void work(BookList bookList) {
//将所有的书都置为null
for (int i = 0; i < bookList.getBooks().size(); i++) {
bookList.setBook(i,null);
}
System.exit(0);
}
}
FindOperation类
public class FindOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的书籍的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
//这里就是我们在Book类中重写toString方法的原因
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("未找到此书籍");
}
}
ReturnOperation类
public class ReturnOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要归还的书籍的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
bookList.getBook(i).setBorrowed(false);
return;
}
}
}
}
ShowOperation类
public class ShowOperation implements IOperation{
public void work(BookList bookList) {
for(int i = 0; i < bookList.getBooks().size(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
整体代码
Main类
import Book.BookList;
import 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 NormalUser(name);
}
}
public static void main(String[] args) {
BookList booklist = new BookList();
User user = login();
while(true) {
int ret = user.menu();
user.ioperations[ret].work(booklist);
}
}
}
package Book
Book类
package Book;
public class Book {
private String name;
private String author;
private String type;
private int price;
private boolean isBorrowed;
public Book(String name, String author, String type, int price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
(isBorrowed ? " 被借出" : " 未被借出") +
'}';
}
}
BookList类
package Book;
import java.util.ArrayList;
public class BookList {
private ArrayList<Book> books = new ArrayList<>();
public BookList() {
books.add(new Book("水浒传","罗贯中","小说",89));
books.add(new Book("三国演义","施耐庵","小说",79));
books.add(new Book("西游记","吴承恩","小说",49));
}
public Book getBook(int pos) {
return books.get(pos);
}
public void setBook(int pos,Book book) {
books.set(pos,book);
}
public ArrayList<Book> getBooks() {
return books;
}
}
package User
User类
package User;
import Operation.IOperation;
public abstract class User {
String name;
public IOperation[] ioperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
}
AdminUser类
package User;
import Operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation()
};
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
NormalUser类
package User;
import Operation.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowedOperation(),
new ReturnOperation()
};
}
public int menu() {
System.out.println("==================================");
System.out.println("hello " + 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();
}
}
package operation
IOperation接口
package Operation;
import Book.BookList;
public interface IOperation {
void work(BookList bookList);
}
AddOperation类
package Operation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
public void work(BookList bookList) {
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,type,price);
bookList.getBooks().add(book);
System.out.println("添加成功");
}
}
BorrowedOperation类
package Operation;
import Book.BookList;
import java.util.Scanner;
public class BorrowedOperation implements IOperation{
public void work(BookList bookList) {
System.out.println("请输入你要借的书的书名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
if(!bookList.getBook(i).isBorrowed()) {
System.out.println("借阅成功");
bookList.getBook(i).setBorrowed(true);
}else {
System.out.println("此书籍已被借出");
}
return;
}
}
System.out.println("未找到该书籍");
}
}
DelOperation
package Operation;
import Book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要删除的书的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
bookList.getBooks().remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("书库中没有此书籍");
}
}
ExitOperation
package Operation;
import Book.BookList;
public class ExitOperation implements IOperation{
public void work(BookList bookList) {
for (int i = 0; i < bookList.getBooks().size(); i++) {
bookList.setBook(i,null);
}
System.exit(0);
}
}
FindOperation类
package Operation;
import Book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查找的书籍的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
System.out.println(bookList.getBook(i));
return;
}
}
System.out.println("未找到此书籍");
}
}
ReturnOperation类
package Operation;
import Book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
public void work(BookList bookList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要归还的书籍的书名");
String name = scanner.nextLine();
for (int i = 0; i < bookList.getBooks().size(); i++) {
if(bookList.getBook(i).getName().equals(name)) {
bookList.getBook(i).setBorrowed(false);
return;
}
}
}
}
ShowOperation类文章来源:https://www.toymoban.com/news/detail-437916.html
package Operation;
import Book.BookList;
public class ShowOperation implements IOperation{
public void work(BookList bookList) {
for(int i = 0; i < bookList.getBooks().size(); i++) {
System.out.println(bookList.getBook(i));
}
}
}
提示
别忘记导入对应的包哦文章来源地址https://www.toymoban.com/news/detail-437916.html
到了这里,关于万字超详细的Java图书管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!