使用对象IO流简单练习一个图书管理系统
Book类(书类)
package day2024_03_21.pojo;import java.io.Serializable;public class Book implements Serializable {private static final long serialVersionUID = 1L;private Integer BookId;private String BookName;private String BookAuthor;public Book() {}public Book(Integer bookId, String bookName, String bookAuthor) {BookId = bookId;BookName = bookName;BookAuthor = bookAuthor;}public Integer getBookId() {return BookId;}public void setBookId(Integer bookId) {BookId = bookId;}public String getBookName() {return BookName;}public void setBookName(String bookName) {BookName = bookName;}public String getBookAuthor() {return BookAuthor;}public void setBookAuthor(String bookAuthor) {BookAuthor = bookAuthor;}@Overridepublic String toString() {return "书籍编号:" + BookId +"; 书籍名称:" + BookName +"; 书籍作者:" + BookAuthor;}
}
BookUtil(书籍实现类)
package day2024_03_21.util;import day2024_03_21.pojo.Book;import java.io.*;
import java.util.ArrayList;
import java.util.List;public class BookUtil {int bookId = 106;{ArrayList<Book> bookList = new ArrayList<>();bookList.add(new Book(101, "圣经", "多人"));bookList.add(new Book(102, "论语", "孔子"));bookList.add(new Book(103, "十年征服时间的人", "尼古拉斯·尼科尔贝克"));bookList.add(new Book(104, "儒林外史", "吴敬梓"));bookList.add(new Book(105, "孔子将", "论语"));ObjectOutputStream obOutput = null;try {obOutput = new ObjectOutputStream(new FileOutputStream("E://books/book.txt"));obOutput.writeObject(bookList);} catch (IOException e) {e.printStackTrace();} finally {try {obOutput.close();} catch (IOException e) {e.printStackTrace();}}}//查看所有的图书信息public List<Book> bookFillAll() {List<Book> bookList = this.iosBookAll();return bookList;}//模糊查询public List<Book> findCondition(String inputValue) {List<Book> bookList = this.iosBookAll();ArrayList<Book> conditionBook = new ArrayList<>();for (Book book : bookList) {if (book.getBookName().indexOf(inputValue) != -1 || book.getBookAuthor().equals(inputValue)) {conditionBook.add(book);}}return conditionBook;}//新增书籍public Boolean addBook(Book book) {book.setBookId(bookId++);List<Book> bookList = this.iosBookAll();bookList.add(book);Boolean addFlag = this.oosBookWrite(bookList);return addFlag;}//修改书籍信息public Boolean reviseBook(int reviseInput,Book book1) {//所有的书籍信息List<Book> bookList = this.iosBookAll();for (Book book : bookList) {if (book.getBookId().equals(reviseInput)) {book.setBookName(book1.getBookName());book.setBookAuthor(book1.getBookAuthor());break;}}Boolean reviseFlag = this.oosBookWrite(bookList);return reviseFlag;}//根据学生id删除学生public Boolean deleteBook(int deleteInput) {//所有的书籍信息List<Book> bookList = this.iosBookAll();for (Book book : bookList) {if (book.getBookId().equals(deleteInput)) {bookList.remove(book);break;}}Boolean deleteFlag = this.oosBookWrite(bookList);return deleteFlag;}//公共方法读private List<Book> iosBookAll() {List<Book> bookList = null;ObjectInputStream objectInput = null;try {objectInput = new ObjectInputStream(new FileInputStream("E://books/book.txt"));bookList = (ArrayList<Book>) objectInput.readObject();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {throw new RuntimeException(e);} finally {try {objectInput.close();} catch (IOException e) {e.printStackTrace();}}return bookList;}//公共方法写private Boolean oosBookWrite(List<Book> bookList) {ObjectOutputStream obOutput = null;try {obOutput = new ObjectOutputStream(new FileOutputStream("E://books/book.txt"));obOutput.writeObject(bookList);return true;} catch (IOException e) {e.printStackTrace();return false;} finally {try {obOutput.close();} catch (IOException e) {e.printStackTrace();}}}
}
User类(用户类)
package day2024_03_21.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer userId;private String userName;private String userPassWord;public User() {}public User(Integer userId, String userName, String userPassWord) {this.userId = userId;this.userName = userName;this.userPassWord = userPassWord;}public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassWord() {return userPassWord;}public void setUserPassWord(String userPassWord) {this.userPassWord = userPassWord;}@Overridepublic String toString() {return "用户ID:" + userId +"; 用户名称:" + userName + '\'' +"; 用户密码:" + userPassWord;}
}
UserUtil(用户实现类)
package day2024_03_21.util;import day2024_03_21.pojo.User;import java.io.*;
import java.util.ArrayList;public class UserUtil {{ArrayList<User> userList = new ArrayList<>();userList.add(new User(1, "root", "1234"));userList.add(new User(2, "kwj", "1111"));userList.add(new User(3, "aw", "2222"));ObjectOutputStream obOutput = null;try {obOutput = new ObjectOutputStream(new FileOutputStream("E://books/user.txt"));obOutput.writeObject(userList);} catch (IOException e) {e.printStackTrace();} finally {try {obOutput.close();} catch (IOException e) {e.printStackTrace();}}}//登录功能public boolean login(String userNameInput, String userPassInput) {ObjectInputStream objectInput = null;ArrayList<User> userArrayList = null;try {objectInput = new ObjectInputStream(new FileInputStream("E://books/user.txt"));userArrayList = (ArrayList<User>) objectInput.readObject();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {throw new RuntimeException(e);} finally {try {objectInput.close();} catch (IOException e) {e.printStackTrace();}}for (User user : userArrayList) {if (user.getUserName().equals(userNameInput) && user.getUserPassWord().equals(userPassInput)) {return true;}}return false;}
}
BookMain(主菜单类)
package day2024_03_21.main;import day2024_03_21.pojo.Book;
import day2024_03_21.util.BookUtil;
import day2024_03_21.util.UserUtil;import java.util.List;
import java.util.Scanner;public class BookMain {public static void main(String[] args) {Scanner sc = new Scanner(System.in);BookUtil bookUtil = new BookUtil();UserUtil userUtil = new UserUtil();boolean flag = true;System.out.println("请输入用户名:");String userNameInput = sc.next();System.out.println("请输入用户密码:");String userPassInput = sc.next();boolean loginFlag = userUtil.login(userNameInput, userPassInput);if (loginFlag) {System.out.println("登录成功!");while (flag) {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("请输入你的选择:");int select = sc.nextInt();switch (select) {case 1://查看所有的图书信息List<Book> bookList = bookUtil.bookFillAll();for (Book book : bookList) {System.out.println(book);}break;case 2://模糊查询书籍信息System.out.println("请输入查询关键字:");String inputValue = sc.next();List<Book> conditionBook = bookUtil.findCondition(inputValue);for (Book book : conditionBook) {System.out.println(book);}break;case 3://新增书籍信息Book book = new Book();System.out.println("请输入书籍的名字:");book.setBookName(sc.next());System.out.println("请输入书籍的作者:");book.setBookAuthor(sc.next());boolean addFlag = bookUtil.addBook(book);System.out.println(addFlag ? "添加成功!" : "添加失败!");break;case 4://修改书籍信息System.out.println("所有书籍信息如下:");List<Book> bookList1 = bookUtil.bookFillAll();for (Book book1 : bookList1) {System.out.println(book1);}System.out.println("请输入要修改的书籍编号:");int reviseInput = sc.nextInt();Book book1 = new Book();System.out.println("请输入书籍的名称:");book1.setBookName(sc.next());System.out.println("请输入书籍的作者:");book1.setBookAuthor(sc.next());Boolean reviseFlag = bookUtil.reviseBook(reviseInput, book1);System.out.println(reviseFlag ? "修改成功!" : "修改失败!");break;case 5://删除书籍信息System.out.println("所有书籍信息如下:");List<Book> bookList2 = bookUtil.bookFillAll();for (Book book2 : bookList2) {System.out.println(book2);}System.out.println("请输入要删除的书籍编号:");int deleteInput = sc.nextInt();Boolean deleteFlag = bookUtil.deleteBook(deleteInput);System.out.println(deleteFlag ? "删除成功!" : "删除失败!");break;case 6:flag = false;System.out.println("退出系统成功!");break;}}} else {System.out.println("登录失败!");}}
}