文章目录
- 一、ReadMe
- 二、项目代码
- 1.Database类
- 2.Book类
- 3.BookManageService类
- 4.Main类
- 三、运行截图
- 1.查看所有书籍
- 2.添加书籍信息
- 3.修改书籍信息
- 4.删除书籍信息
- 5.借阅书籍
- 6.归还书籍
- 7.退出系统
一、ReadMe
本文项目内容是Java图书管理系统(一)的另一版本,不使用接口及其实现类,利用类完成对图书的增删改查与借阅归还等。
二、项目代码
项目框架结构:
1.Database类
package bookmanage1.cn.edu.just.dataBase;
import bookmanage1.cn.edu.just.model.Book;
import java.util.ArrayList;
import java.util.HashMap;
public class Database {
public static HashMap<String, ArrayList<Book>> initBookMap() {
HashMap<String, ArrayList<Book>> bookMap = new HashMap<>();
//创建书籍对象
Book book1 = new Book("西游记后传", "名著", 19,false);
Book book2 = new Book("水浒传后传", "名著", 29,true);
Book book3 = new Book("红楼梦后传", "名著", 39,false);
Book book5 = new Book("java入门精通", "it", 99,true);
Book book6 = new Book("php入门精通", "it", 88,false);
Book book7 = new Book("jsp入门精通", "it", 88,false);
//创建书籍List集合
ArrayList<Book> bookList1 = new ArrayList<>();
ArrayList<Book> bookList2 = new ArrayList<>();
// List集合添加书籍
bookList1.add(book1);
bookList1.add(book2);
bookList1.add(book3);
bookList2.add(book5);
bookList2.add(book6);
bookList2.add(book7);
//HashMap添加键值对
bookMap.put(book1.getType(), bookList1);
bookMap.put(book6.getType(), bookList2);
return bookMap;
}
}
2.Book类
package bookmanage1.cn.edu.just.model;
public class Book {
//书名
private String name;
//类型
private String type;
//价格
private double price;
//是否借出
private boolean isBorrowed;
public Book() {
}
public Book(String name, String type, double price) {
this.name = name;
this.type = type;
this.price = price;
}
public Book(String name, String type, double price, boolean isBorrowed) {
this.name = name;
this.type = type;
this.price = price;
this.isBorrowed = isBorrowed;
}
public boolean isBorrowed() {
return isBorrowed ;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", price=" + price +
", isBorrowed=" + (isBorrowed == true ? " 已借出 ":" 未借出 ") +
'}';
}
}
3.BookManageService类
package bookmanage1.cn.edu.just.service;
import bookmanage1.cn.edu.just.model.Book;
import bookmanage1.cn.edu.just.dataBase.Database;
import java.util.*;
/**图书管理系统优化点
1 在修改书籍功能中 如果要修改的书籍类型或书名不存在的情况下 直接进行提示操作
2 在修改书籍功能中 增加修改已有书籍的类型的功能
3 在删除书籍功能中 如果删除的书籍类型和或书名不存在的情况下 直接进行提示操作
在优化完的基础上 新增两个功能
1 借书功能
2 还书功能
*/
public class BookManageService {
private static Database database=new Database();
public static void login(HashMap<String, ArrayList<Book>> bookMap, Scanner scanner, boolean isRunning) {
while (isRunning) {
System.out.println();
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("7.归还书籍");
System.out.println("请输入您的选择(输入1-7的数字):");
//获取键盘录入的数字
int chooseNum = scanner.nextInt();
switch (chooseNum) {
case 1:
//查看所有书籍
getBooksInfo(bookMap);
break;
case 2:
//添加书籍信息
addBookInfo(bookMap,scanner);
break;
case 3:
// 修改书籍信息
updateBookInfo(bookMap,scanner);
break;
case 4:
//删除书籍信息
deleteBookInfo(bookMap,scanner);
break;
case 5:
//退出系统
// isRunning = false;
System.out.println("退出书籍管理系统!");
System.exit(0);
// break;
case 6:
//借阅书籍信息
borrowBook(bookMap,scanner);
break;
case 7:
//归还书籍信息
returnBook(bookMap,scanner);
break;
default:
// 输入非1-5的数字
System.out.println("您输入的命令有误,请重新输入");
break;
}
}
}
/**
* 查看所有书籍信息
* *@parambookMap
**/
private static void getBooksInfo(HashMap<String, ArrayList<Book>> bookMap) {
System.out.println("类型\t\t书名\t\t价格\t\t是否借出");
//对bookMap进行遍历并打印
Set<Map.Entry<String, ArrayList<Book>>> entries = bookMap.entrySet();
for (Map.Entry<String, ArrayList<Book>> book : entries) {
//获取bookMap中的key(书籍类型)
String bookType = book.getKey();
System.out.println(bookType);
//获取bookMap中的value(书籍List集合)
ArrayList<Book> bookList = book.getValue();
//对书籍List集合进行遍历
for (Book bookInfo : bookList) {
System.out.println("\t\t"+bookInfo.getName()+"\t\t"+bookInfo.getPrice()+"\t\t"+(bookInfo.isBorrowed()==true ? "已借出" : "未借出"));
}
}
}
/**
*添加书籍信息
*@param bookMap 书籍集合
**param scanner健盘录入对象
**/
private static void addBookInfo (HashMap<String,ArrayList<Book>> bookMap, Scanner scanner){
System.out.println("");
System.out.println("请输入您要添加的书籍类型:");
String bookType = scanner.next();
System.out.println("请输入您要添加的书名:");
String bookName = scanner.next();
System.out.println("请输入您要添加的书籍价格:");
double bookPrice = scanner.nextDouble();
//创建书籍对象
Book addBook = new Book (bookName, bookType, bookPrice);
//添加书籍存在两种情况
//1新添加的书籍类型在bookMap中已存在如果是这样那么就直接将新书添加到对应类型的书籍List集合中
//2新添加的书籍类型在bookMap中不存在如果这样需要新建键值对完成bookMap中的元素添加
//根据书籍类型查询是否有对应的书籍List集合
ArrayList<Book>bookArrayList = bookMap.get(bookType) ;
if (bookArrayList != null){
// 情况1
bookArrayList.add(addBook);
}else {
//情况2
//新建List集合并添加书籍
ArrayList<Book> bookList = new ArrayList<>();
bookList.add(addBook);
// bookMap新添加键值对
bookMap.put (bookType,bookList);
}
System.out.println("添加"+bookName+"成功!");
}
/**获取图书原价格
* @param bookMap
* @param bookName
* @return
*/
/**
* 获取图书原价格
*
* @param bookMap
* @param bookName
* @return
*/
public static double getBookPrice(HashMap<String, ArrayList<Book>> bookMap, String bookName) {
Set<Map.Entry<String, ArrayList<Book>>> entries = bookMap.entrySet();
double price = 88;
for (Map.Entry<String, ArrayList<Book>> book : entries) {
// //获取bookMap中的key(书籍类型)
// String bookType = book.getKey();
// System.out.println(bookType);
//获取bookMap中的value(书籍List集合)
ArrayList<Book> bookList = book.getValue();
//对书籍List集合进行遍历
for (Book bookInfo : bookList) {
if (bookInfo.getName().equals(bookName)) {
price = bookInfo.getPrice();
break;
}
}
}
return price;
}
public static boolean getBookIsBorrowed(HashMap<String, ArrayList<Book>> bookMap, String bookName) {
Set<Map.Entry<String, ArrayList<Book>>> entries = bookMap.entrySet();
boolean isBorrowed=false;
for (Map.Entry<String, ArrayList<Book>> book : entries) {
ArrayList<Book> bookList = book.getValue();
//对书籍List集合进行遍历
for (Book bookInfo : bookList) {
if (bookInfo.getName().equals(bookName)) {
isBorrowed = bookInfo.isBorrowed();
break;
}
}
}
return isBorrowed;
}
/** 1 在修改书籍功能中 如果要修改的书籍类型或书名不存在的情况下 直接进行提示操作
2 在修改书籍功能中 增加修改已有书籍的类型的功能
*/
private static void updateBookInfo (HashMap<String, ArrayList<Book>> bookMap, Scanner scan) {
System.out.println("");
// Scanner scan=new Scanner(System.in);
System.out.println("请选择:");
System.out.println("1->修改书籍类型 2->修改书籍名称及价格 3->返回上一层");
int choose=scan.nextInt();
if(choose==1){
System.out.println("请输入该书籍原类型");
String originalBookType = scan.next();
isExitByType(bookMap, originalBookType);
System.out.println("请输入要修改的书名");
String modifyBookName = scan.next();
isExitByName(bookMap,originalBookType, modifyBookName);
System.out.println("请输入修改后的书籍类型");
String newBookType = scan.next();
double bookPrice=getBookPrice(bookMap,modifyBookName);
boolean bookIsBorrowed=getBookIsBorrowed(bookMap,modifyBookName);
Book addBook1 = new Book (modifyBookName, newBookType, bookPrice,bookIsBorrowed);
ArrayList<Book> bookArrayList = bookMap.get(newBookType);
if (bookArrayList != null){
bookArrayList.add(addBook1);
System.out.println("更改书籍"+modifyBookName+"成功!");
ArrayList<Book> bookArrayListRemove = bookMap.get(originalBookType);
for (int i = 0; i < bookArrayListRemove.size(); i++) {
Book book = bookArrayListRemove.get(i);
String name = book.getName();
if (name.equals(modifyBookName)) {
//要删除的书名存在进行删除
bookArrayListRemove.remove(i);
// System.out.println("删除" + name + "成功!");
//删除书籍后需要判断该书所在ListJ集合长度是否为0
if (bookArrayListRemove.size() == 0) {
//移除该键值对
bookMap.remove(originalBookType);
}
}
}
}else {
ArrayList<Book> bookList = new ArrayList<>();
bookList.add(addBook1);
// bookMap新添加键值对
bookMap.put(newBookType, bookList);
System.out.println("更改书籍"+modifyBookName+"成功!");
ArrayList<Book> bookArrayListRemove = bookMap.get(originalBookType);
for (int i = 0; i < bookArrayListRemove.size(); i++) {
Book book = bookArrayListRemove.get(i);
String name = book.getName();
if (name.equals(modifyBookName)) {
//要删除的书名存在进行删除
bookArrayListRemove.remove(i);
//删除书籍后需要判断该书所在ListJ集合长度是否为0
if (bookArrayListRemove.size() == 0) {
//移除该键值对
bookMap.remove(originalBookType);
}
}
}
}
updateBookInfo(bookMap,scan);
}
if (choose==2){
System.out.println("请输入该书籍类型");
String bookType = scan.next();
isExitByType(bookMap, bookType);
//
System.out.println("请输入要修改的书名");
String oldBookName = scan.next();
isExitByName(bookMap,bookType, oldBookName);
System.out.println("请输入新的书名");
String newBookName = scan.next();
System.out.println("请输入新的价格");
double newBookPrice = scan.nextDouble();
ArrayList<Book> bookArrayList = bookMap.get(bookType);
for (Book book : bookArrayList) {
String name = book.getName();
if (name.equals(oldBookName)) {
book.setName(newBookName);
book.setPrice(newBookPrice);
System.out.println("书籍<<"+newBookName+">>的信息修改完成!");
updateBookInfo(bookMap,scan);
}
}
}
if (choose==3){
boolean isRun = true;
login(bookMap, scan, isRun);
}
else{
System.out.println("输入有误,重新输入");
updateBookInfo(bookMap,scan);
}
}
private static void isExitByType(HashMap<String, ArrayList<Book>> bookMap, String bookType) {
ArrayList<Book> bookArrayList1 = bookMap.get(bookType);
if (bookArrayList1 == null || bookArrayList1.size() == 0) {
System.out.println("您要修改的书籍类型不存在");
Scanner scanner = new Scanner(System.in);
boolean isRunning=true;
login(bookMap,scanner,isRunning);
}
}
private static void isExitByName(HashMap<String, ArrayList<Book>> bookMap, String bookType,String bookName) {
//根据书籍类型获取书籍List集合
ArrayList<Book> bookArrayList = bookMap.get(bookType);
//判读要修改的书籍是否存在
boolean isExist = false;
for (Book book : bookArrayList) {
String name = book.getName();
if (name.equals(bookName)) {
isExist = true;
break;
}
}
if (!isExist) {
System.out.println("您要修改的书名:" + bookName + "在系统里不存在");
Scanner scanner = new Scanner(System.in);
boolean isRunning=true;
login(bookMap,scanner,isRunning);
}
}
/**删除书籍信息
*param bookMap书籍信息集合
**param scanner键盘录入对象
**/
private static void deleteBookInfo(HashMap<String,ArrayList<Book>> bookMap, Scanner scanner) {
System.out.println("");
System.out.println("请输入要删除的书籍类型");
String deleteBookType = scanner.next();
isExitByType(bookMap,deleteBookType);
System.out.println("请输入要删除的书名");
String deleteBookName = scanner.next();
isExitByName(bookMap,deleteBookType, deleteBookName);
ArrayList<Book> bookArrayList = bookMap.get(deleteBookType);
for (int i = 0; i < bookArrayList.size(); i++) {
Book book = bookArrayList.get(i);
String name = book.getName();
if (name.equals(deleteBookName)) {
//要删除的书名存在进行删除
bookArrayList.remove(i);
System.out.println("删除" + name + "成功!");
//删除书籍后需要判断该书所在ListJ集合长度是否为0
if(bookArrayList.size() == 0) {
//移除该键值对
bookMap.remove(deleteBookType);
}
// isExist = true;
break;
}
}
// if(!isExist){
// System.out.println("您要删除的书名:"+deleteBookName+"在系统中不存在!");
// }
}
//借阅图书
public static void borrowBook(HashMap<String,ArrayList<Book>> bookMap, Scanner scan){
// HashMap<String, ArrayList<Book>> bookMap = database.initBookMap();
System.out.println("--------借阅图书-----------");
System.out.println("请输入要借阅的书籍类型:");
String borrowType=scan.next();
isExitByType(bookMap,borrowType);
System.out.println("请输入要借阅的书籍名称:");
String borrowBookName=scan.next();
isExitByName(bookMap,borrowType,borrowBookName);
ArrayList<Book> bookArrayList = bookMap.get(borrowType);
for (Book book : bookArrayList) {
String name = book.getName();
if (name.equals(borrowBookName)&&book.isBorrowed()==false) {
book.setBorrowed(true);
System.out.println("借阅成功");
System.out.println("该书籍的信息为:");
System.out.println(book.toString());
break;
}
if (name.equals(borrowBookName) && book.isBorrowed()==true){
System.out.println("该书已被借出");
break;
}
}
}
public static void returnBook(HashMap<String,ArrayList<Book>> bookMap, Scanner scan) {
System.out.println("-----------归还图书--------------");
// Scanner scan=new Scanner(System.in);
System.out.println("请输入要归还的书籍类型:");
String returnBookType=scan.next();
isExitByType(bookMap,returnBookType);
System.out.println("请输入要归还的书籍名称:");
String returnBookName=scan.next();
// isExitByName(bookMap,returnBookType,returnBookName);
isExitByName(bookMap,returnBookType,returnBookName);
ArrayList<Book> bookArrayList = bookMap.get(returnBookType);
for (Book book : bookArrayList) {
String name = book.getName();
if (name.equals(returnBookName)&&book.isBorrowed()==true ) {
book.setBorrowed(false);
System.out.println("归还成功");
System.out.println("该书籍的信息为:");
System.out.println(book.toString());
break;
}
if (name.equals(returnBookName)&&book.isBorrowed()==false){
System.out.println("该书已被归还");
}
}
}
}
4.Main类
package bookmanage1.cn.edu.just.test;
import bookmanage1.cn.edu.just.dataBase.Database;
import bookmanage1.cn.edu.just.model.Book;
import bookmanage1.cn.edu.just.service.BookManageService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Database database = new Database();
BookManageService user = new BookManageService();
HashMap<String, ArrayList<Book>> bookMap = database.initBookMap();
//创建键盘录入对象
Scanner scanner = new Scanner(System.in);
//图书管理服务是否运行
boolean isRunning = true;
user.login(bookMap, scanner, isRunning);
}
}
三、运行截图
1.查看所有书籍
2.添加书籍信息
添加书籍存在两种情况
1 新添加的书籍类型在bookMap中已存在如果是这样那么就直接将新书添加到对应类型的书籍List集合中
2 新添加的书籍类型在bookMap中不存在如果这样需要新建键值对完成bookMap中的元素添加
3.修改书籍信息
4.删除书籍信息
5.借阅书籍
6.归还书籍
7.退出系统
使用接口及其实现类的另一版本:Java图书管理系统(一)