使用IO技术将图书数据存储到文件中了, 文件存储图书信息后, 可以在每次启动应用时读取文件中的内容,从而实现程序数据的一直存在。
图书管理系统要求 =>
管理员登陆
图书管理
2.1图书新增
2.2图书修改
2.3图书删除
2.4根据图书名称模糊查找图书
2.5查看所有图书(三种排序)
- 价格从高到低排序
- 价格从低到高排序
- 新旧排序(出版日期排序)
package com.java.librarymanagement.dao;
import com.java.librarymanagement.bean.Admin;
import com.java.librarymanagement.bean.Book;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.TreeSet;
/**
* @author Aleo阿乐
*/
public class LibraryDao {
private Admin admin = new Admin();
private ArrayList<Book> bookList = new ArrayList<Book> ();
public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}
/**
* 判断是否登陆成功
* @param a
* @return
*/
public boolean login(Admin a){
//利用admin自带的equals方法快捷很多
if(admin.equals(a)){
return true;
}
System.out.println("登录失败! 请重新登录! ");
return false;
}
/**
* 添加书籍方法
* @param b
* @return
*/
public boolean add(Book b) {
bookList.add(b);
return true;
}
/**
* 通过书籍进行查找
* @param name
* @return
*/
public Book findByName(String name) {
for (Book book :bookList) {
String name1 = book.getBookName();
if(name1.equals(name)){
return book;
}
}
return null;
}
/**
* 书籍模糊查找功能
* @param fname
* @return
*/
public LinkedList fuzzySearch(String fname){
LinkedList<Book> fuzzylist = new LinkedList<Book> ();
for (Book book: bookList) {
String name = book.getBookName();
if(name.contains(fname)){
fuzzylist.add(book);
}
}
if(fuzzylist.size()==0){
return null;
}
return fuzzylist;
}
/**
* 无实际作用,但为了体现其持久化操作, 还是给了这么一个方法
* @param oldBook
* @param newBook
*/
public void update(Book oldBook, Book newBook) {
delete(oldBook);
add(newBook);
}
/**
* 删除操作
* @param b
*/
public void delete(Book b) {
for (Book book : bookList) {
if (book.equals(b)){
bookList.remove(book);
break;
}
}
}
/**
* 返回所有书籍列表操作
* @return
*/
public ArrayList<Book> findAll() {
return bookList;
}
/**
* 利用TreeSet集合对书籍从低到高排序
* @param bookList
* @return
*/
public TreeSet<Book> lowToHigh(ArrayList<Book> bookList){
TreeSet<Book> set = new TreeSet<Book>(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
if(o1.getPirce()<o2.getPirce()){
return -1;
}else if(o1.getPirce()>o2.getPirce()){
return 1;
}else {
return o1.getBookName().compareTo(o2.getBookName());
}
}
});
for (Book book : bookList) {
set.add(book);
}
return set;
}
/**
* 书籍从高到低排序
* @param bookList
* @return
*/
public TreeSet<Book> highToLow(ArrayList<Book> bookList){
TreeSet<Book> set = new TreeSet<Book>(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
if(o1.getPirce()>o2.getPirce()){
return -1;
}else if(o1.getPirce()<o2.getPirce()){
return 1;
}else {
return o1.getBookName().compareTo(o2.getBookName());
}
}
});
for (Book book : bookList) {
set.add(book);
}
return set;
}
/**
* 书籍按照发布日期进行排序
* @param bookList
* @return
*/
public TreeSet<Book> sortByPublish(ArrayList<Book> bookList){
TreeSet<Book> set = new TreeSet<Book>(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
if (o1.getPublishYear()<o2.getPublishYear()){
if(o1.getPublishMonth()<o2.getPublishMonth()){
return -1;
}else if (o1.getPublishMonth()>o2.getPublishMonth()){
return 1;
}else {
return o1.getBookName().compareTo(o2.getBookName());
}
}else if (o1.getPublishYear() > o2.getPublishYear()){
return 1;
}else if(o1.getPublishYear() == o2.getPublishYear()){
if(o1.getPublishMonth()<o2.getPublishMonth()){
return -1;
}else if (o1.getPublishMonth()>o2.getPublishMonth()){
return 1;
}else {
return o1.getBookName().compareTo(o2.getBookName());
}
}else {
return 0;
}
}
});
for (Book book : bookList) {
set.add(book);
}
return set;
}
}
package com.java.librarymanagement.view;
import com.java.librarymanagement.bean.Admin;
import com.java.librarymanagement.bean.Book;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Scanner;
public class Views {
private Scanner input = new Scanner(System.in);
/**
* 欢迎视图
*/
public void welcome() {
System.out.println("欢迎使用图书管理系统~");
}
/**
* 退出视图
*/
public void bye() {
System.out.println("欢迎下次使用~");
}
/**
* 登陆界面
* @return
*/
public Admin loginView() {
System.out.println("请先登录: ");
System.out.println("请输入账户名: ");
String count = input.nextLine();
System.out.println("请输入密码: ");
String password = input.nextLine();
System.out.println("请输入账户名:");
Admin admin = new Admin();
admin.setAdmin(count);
admin.setPassword(password);
return admin;
}
/**
* 主菜单界面
* @return
*/
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("5. 查看所有图书");
System.out.println("6. 保存图书数据到本地");
System.out.println("0. 退出");
//nextLine, 接收一行, 包容性很大 (且nextLine可以接收空格)
String text = input.nextLine();
int num = -1;
//可能发生不输入数字类型的情况抛出错误
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (num < 0 || num > 6) {
System.out.println("请输入以上数字哦!!");
return menu();
}
return num;
}
/**
* 插入图书显示的文字
* @return
*/
public Book insert() {
System.out.println("添加图书, 请输入图书信息: ");
System.out.println("请输入图书名称: ");
String name = input.nextLine();
System.out.println("请输入图书价格: ");
String price1 = input.nextLine();
double price = -1;
try {
price = Double.parseDouble(price1);
} catch (NumberFormatException e) {
System.out.println("输入有误, 请重新输入~");
}
boolean judge = true;
while (judge) {
System.out.println("请输入图书出版的年份:");
String y = input.nextLine();
System.out.println("请输入图书出版的月份:");
String m = input.nextLine();
int year = -1;
int month = -1;
try {
year = Integer.parseInt(y);
month = Integer.parseInt(m);
} catch (NumberFormatException e) {
System.out.println("输入有误, 请重新输入~");
return insert();
}
Calendar date = Calendar.getInstance();
//返回一个Integer对象, 然后自动拆箱
int sysYear = Integer.valueOf(date.get(Calendar.YEAR));
int sysMonth = Integer.valueOf(date.get(Calendar.MONTH));
//判断输入年月是否在合理范围
if ((year>1000 && year<sysYear) && (month>0 && month<=12)) {
judge = true;
} else if (year == sysYear && (month > 0 && month <= sysMonth)) {
judge = true;
}else{
System.out.println("输入的年份/月份不合理请重新操作");
continue;
}
if(judge = true) {
Book b = new Book();
b.setBookName(name);
b.setPirce(price);
b.setPublishYear(year);
b.setPublishMonth(month);
return b;
}
}
return null;
}
/**
* 打印书籍视图
* @param b
*/
public void printBook(Book b) {
System.out.println("图书信息如下:");
System.out.println(b.toString());
}
/**
* 更新图书信息的操作, 输入新的图书信息, 直接对已知对象进行操作
*/
public void update(Book b) {
System.out.println("请输入新的图书名称: ");
String name = input.nextLine();
System.out.println("请输入新的图书价格: ");
String p= input.nextLine();
System.out.println("请输入新的图书出版的年份:");
String y = input.nextLine();
System.out.println("请输入新的图书出版的月份:");
String m = input.nextLine();
double price = Double.parseDouble(p);
int year = Integer.parseInt(y);
int month = Integer.parseInt(m);
b.setBookName(name);
b.setPirce(price);
b.setPublishYear(year);
b.setPublishMonth(month);
}
/**
* 删除操作确认
*/
public int delete() {
System.out.println("是否确认删除 ?");
System.out.println("1. 确认删除");
System.out.println("2. 取消操作");
System.out.println("0. 退出");
String text = input.nextLine();
int num = -1;
//因为有可能发生异常将其分为两行, 设置一个默认值, 虽然我觉得好像可以写在一起, 但可能是为了更好的确定异常范围叭~
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (num < 0 || num > 2) {
System.out.println("输入有误, 请重新输入");
return delete();
}
return num;
}
/**
* 打印所有书籍视图
* @param bo
*/
public void printAll(LinkedList<Book> bo) {
int count = 0;
for (Book book :bo) {
if(book != null){
count++;
printBook(book);
}
}
if(count == 0){
System.out.println("暂无图书信息~");
}
}
/**
* 判断书籍是否存在视图
*/
public void bookExist() {
System.out.println("此单号在快递柜中已存在, 请不要再存储");
}
/**
* 操作成功返回语句
*/
public void success() {
System.out.println("操作成功");
}
/**
* 价格排序视图
* @return
*/
public int sortMenu(){
System.out.println("请选择排序方式:");
System.out.println("1. 价格从高到低排序 ");
System.out.println("2. 价格从低到高排序 ");
System.out.println("3. 出版日期排序");
System.out.println("0. 返回上一级");
//nextLine, 可以接收一行, 包容性大
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (num < 0 || num > 3) {
System.out.println("请输入以上数字哦!!");
return sortMenu();
}
return num;
}
/**
* 通过书籍名字进行查找的打印视图
* @return
*/
public String findByFname() {
System.out.println("请根据提示, 输入要操作的图书信息: ");
System.out.println("请输入要操作的图书名字: ");
String name = input.nextLine();
return name;
}
}
package com.java.librarymanagement.util;
import com.java.librarymanagement.bean.Book;
import java.io.*;
import java.util.ArrayList;
public class BookTools {
/**
* 将存储图书的集合存储至本地Book.txt文件中
* @param bookList
* @throws IOException
*/
public void storeBook(ArrayList<Book> bookList) throws IOException {
FileOutputStream fos = new FileOutputStream("D://Book.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(bookList);
oos.close();
fos.close();
System.out.println("保存图书数据到本地成功");
}
/**
* 将序列化后的文件反序列化后使用对象
* @return
* @throws IOException
*/
public ArrayList<Book> loadBook() throws IOException {
ArrayList<Book> bookList = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("D://Book.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
try (fis; ois) {
bookList = (ArrayList<Book>) ois.readObject();
System.out.println("载入数据成功");
} catch (IOException | ClassNotFoundException e) {
System.out.println("载入数据失败");
}
return bookList;
} catch (FileNotFoundException fileNotFoundException) {
System.out.println("未找到文件");
return bookList;
} catch (EOFException eofException) {
System.out.println("数据为空");
return bookList;
}
}
}
package com.java.librarymanagement.main;
import com.java.ExpressManager.bean.Express;
import com.java.librarymanagement.bean.Admin;
import com.java.librarymanagement.bean.Book;
import com.java.librarymanagement.dao.LibraryDao;
import com.java.librarymanagement.util.BookTools;
import com.java.librarymanagement.view.Views;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.TreeSet;
public class Main {
private static Views v = new Views();
private static LibraryDao dao = new LibraryDao();
private static BookTools bookTool = new BookTools();
public static void main(String[] args) throws IOException {
//登陆界面
v.welcome();
Admin admin = v.loginView();
if (dao.login(admin)) {
v.success();
} else {
v.loginView();
}
//登陆成功之后加载数据
ArrayList<Book> list = bookTool.loadBook();
dao.setBookList(list);
int menu = 0;
m:
while (true) {
menu = v.menu();
switch (menu) {
case 1:
addBook();
break;
case 2:
editBook();
break;
case 3:
deleteBook();
break;
case 4:
String name = v.findByFname();
LinkedList<Book> bsList;
bsList = dao.fuzzySearch(name);
if (bsList == null) {
System.out.println("不好意思, 没有查找到图书~");
v.menu();
}
v.printAll(bsList);
break;
case 5:
bookSort();
break;
case 6:
ArrayList<Book> data = dao.findAll();
bookTool.storeBook(data);
break;
case 0:
break m;
}
}
v.bye();
}
/**
* 书籍排序整合
*/
private static void bookSort() {
while (true) {
int num = v.sortMenu();
switch (num) {
case 1:
TreeSet<Book> set1 = dao.highToLow(dao.findAll());
for (Book book : set1) {
v.printBook(book);
}
return;
case 2:
TreeSet<Book> set2 = dao.lowToHigh(dao.findAll());
for (Book book : set2) {
v.printBook(book);
}
return;
case 3:
TreeSet<Book> set3 = dao.sortByPublish(dao.findAll());
for (Book book : set3) {
v.printBook(book);
}
return;
case 0:
return;
default:
}
}
}
/**
* 添加书籍整合
*/
private static void addBook() {
Book b1 = v.insert();
Book b2 = dao.findByName(b1.getBookName());
if(b2 == null){
dao.add(b1);
v.printBook(b1);
}else {
v.bookExist();
}
}
/**
* 编辑书籍整合
*/
private static void editBook() {
String bookName = v.findByFname();
Book b = dao.findByName(bookName);
Book b2 = b;
if (b == null) {
System.out.println("图书不存在, 请检查输入!");
} else {
v.printBook(b);
v.update(b);
dao.update(b, b2);
v.printBook(b2);
}
}
/**
* 删除书籍整合
*/
private static void deleteBook() {
String bookName = v.findByFname();
Book b = dao.findByName(bookName);
if (b == null) {
System.out.println("图书不存在, 请检查输入!");
} else {
v.printBook(b);
int type = v.delete();
if (type == 1) {
dao.delete(b);
v.success();
}
}
}
}
package com.java.librarymanagement.bean;
import java.io.Serializable;
import java.util.Objects;
public class Book implements Serializable {
private String bookName;
private double pirce;
private int publishMonth;
private int publishYear;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Book book = (Book) o;
return Objects.equals(bookName, book.bookName);
}
@Override
public int hashCode() {
return Objects.hash(bookName);
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", pirce=" + pirce +
", publishMonth=" + publishMonth +
", publishYear=" + publishYear +
'}';
}
public Book() {
}
public Book(String bookName, double pirce, int publishMonth, int publishYear) {
this.bookName = bookName;
this.pirce = pirce;
this.publishMonth = publishMonth;
this.publishYear = publishYear;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPirce() {
return pirce;
}
public void setPirce(double pirce) {
this.pirce = pirce;
}
public int getPublishMonth() {
return publishMonth;
}
public void setPublishMonth(int publishMonth) {
this.publishMonth = publishMonth;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
}