目录
1.概要
该项目适用于Java基础阶段的联系项目,涉及到了面向对象,集合容器,以及对象数据的本地化存储与读取(序列化和反序列化)等技术
2.设计思路
1.首先 ,根据需求我们要有读者跟管理员两种身份的操作界面
2.然后,要有book类来描述书籍的属性(书名,价格等)
3.其次,要有reader类来描述读者的属性(姓名、密码、id等)
4.最后,要有library类存放图书,并整合管理员跟读者的各种操作。
3.代码实现
3.1Book类
包含了书籍的各个属性,并为其提供了有参无参构造、get() set()方法以及toString()方法
import java.io.Serializable;
public class Book implements Serializable {
private String bookid;
private String name;
private String author;
private double price;
public Book() {
}
public Book(String bookid, String name, String author, double price) {
this.bookid = bookid;
this.name = name;
this.author = author;
this.price = price;
}
public String getBookid() {
return bookid;
}
public void setBookid(String bookid) {
this.bookid = bookid;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "图书信息: 图书编号 = "+bookid+"\t图书名称 = "+name+"\t图书作者 = "+author+"\t图书价格 = "+price;
}
}
3.2Reader类
import java.io.Serializable;
public class Reader implements Serializable {
private String readerid;
private String name;
private String password;
public Reader() {
}
public Reader(String readerid, String name, String password) {
this.readerid = readerid;
this.name = name;
this.password = password;
}
public String getReaderid() {
return readerid;
}
public void setReaderid(String readerid) {
this.readerid = readerid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "读者编号:"+readerid+"读者姓名:"+name+"密码:"+password;
}
}
3.3 ViewManager(主界面类)
import java.io.*;
import java.util.List;
import java.util.Scanner;
public class ViewManager implements Serializable {
Scanner sc = new Scanner(System.in);
static Library library = new Library();
public void ShowMainView() {
while (true) {
System.out.println("======欢迎来到图书馆======");
System.out.println(" 1 查看图书");
System.out.println(" 2 读者登录");
System.out.println(" 3 管理员登陆");
System.out.println(" 4 退出");
System.out.print("请输入你的操作:");
int c = sc.nextInt();
if(c == 1){
showBookView();
}else if (c == 2){
ReaderLoginView();
}else if(c == 3){
showAdminLoginView();
}else if(c == 4){
ObjectOutputStream oos2 = null;
try{
oos2 = new ObjectOutputStream(new FileOutputStream("D://book_reader.txt"));
oos2.writeObject(Library.booklist);
oos2.writeObject(Library.readerList);
oos2.close();
}catch (Exception e ){
e.getCause();
}finally{
// oos2.close();
}
System.out.println("退出成功,欢迎您再次使用");
System.exit(0);
}
}
}
private void showAdminLoginView() {
System.out.println("--------------管理员登录--------------");
System.out.println("请输入管理员的账户");
String name = sc.next();
System.out.println("请输入管理员密码");
String pwd = sc.next();
//从图书馆中验证管理员的合法性
if (library.validAdmin(name, pwd)) {
/*若验证合法,进入管理员操作界面*/
showAdminOpView();
}else{
System.out.println("账号或密码错误,请重新登录");
}
}
private void showAdminOpView() {//管理员操作
while (true) {
System.out.println("--------------管理员操作--------------");
System.out.println("\t1 添加读者");
System.out.println("\t2 删除读者");
System.out.println("\t3 查看读者");
System.out.println("\t4 添加图书");
System.out.println("\t5 删除图书");
System.out.println("\t6 查看所有图书");
System.out.println("\t7 退出");
System.out.println("请输入你的操作");
int c = sc.nextInt();
if (c == 1){
showAddReaderView();//添加读者
}else if(c == 2){
showDeleteReaderView();//删除读者
}else if(c == 3){
showAllReaderView();//查看读者
}else if(c == 4){
showAddBookView();//添加图书
}else if(c == 5){
showAllBookView();//查看所有图书
}else if(c == 6){
showDeleteBookView();//删除图书
}else if(c == 7){
System.out.println("退出成功,欢迎您再次使用");
ShowMainView();
System.exit(0);
}
}
}
public void showBookView(){
Scanner sc = new Scanner(System.in);
System.out.println("-----------图书查询------------");
System.out.print("1 按编号查询");
System.out.println("\t\t\t2 按名称查询");
System.out.println("请输入你的操作:");
int sc1 = sc.nextInt();
if(sc1 == 1){
System.out.println("请输入图书编号");
String sc2 = sc.next();
Book book = library.getBookNameByBookId(sc2);
System.out.println(book);
}
if(sc1 == 2){
System.out.println("请输入图书名称");
String sc2 = sc.next();
List<Book> books = library.getBookByBookName(sc2);
for(Book book: books){
System.out.println(books);
}
}
}
public void ReaderLoginView(){
Scanner sc = new Scanner(System.in);
System.out.println("------读者登录------");
System.out.println("请输入读者编号");
String readerid = sc.next();
System.out.println("请输入读者密码");
String pwd = sc.next();
boolean result = library.validReader(readerid,pwd);
if(result){
showReaderOPView(readerid);//读者操作
}else {
System.out.println("账号密码有误!!!!!!!!!!!!!!!");
}
}
public void showReaderOPView(String readderid){//读者操作方法
System.out.println("------读者操作------");
System.out.println("\t1 查看个人信息");
System.out.println("\t2 修改密码");
System.out.println("\t3 查看借阅信息");
System.out.println("\t4 借书");
System.out.println("\t5 还书");
System.out.println("\t6 退出");
System.out.println("请输入 你的操作");
int c = sc.nextInt();
if (c == 1){
showReaderInfoView(readderid);//查看个人信息
}else if(c == 2){
showModifyPwdView(readderid);//修改密码
}else if(c == 3){
showBorrowInfoView(readderid);//查看借阅信息
}else if(c == 4){
showBorrowBookView(readderid);//借书
}else if(c == 5){
showReturnBookView(readderid);//还书
}else if(c == 6){
System.out.println("退出成功,欢迎您再次使用");
ShowMainView();
System.exit(0);
}
}
private void showReaderInfoView(String readerid) {
System.out.println("============查看个人信息============");
//从图书馆中查找当前读者readerid的信息
Reader reader = library.getReaderById(readerid);
System.out.println(reader);
}
public void showModifyPwdView(String readerid) {
System.out.println("============修改密码============");
System.out.println("请输入密码");
String oldPwd = sc.next();
System.out.println("请输入新密码");
String newPwd = sc.next();
//修改编号为readerid的读者密码
boolean result = library.modifyReaderPwd(readerid, oldPwd, newPwd);
if (result) {
System.out.println("密码修改成功");
//密码修改成功后,重新回到登录页面,用新的密码进行验证登录
ShowMainView();
} else {
System.out.println("密码修改失败");
//查看个人信息功能执行完毕后,再次回到读者登录功能菜单
showReaderOPView(readerid);
}
}
private void showBorrowInfoView(String readerid) {//查看借阅信息
System.out.println("============查看借阅信息============");
/*从图书馆中查找readerId读者编号借阅的图书*/
List<Book> result = library.getBorrowBooksByReaderId(readerid);
if (result == null) {
System.out.println("该读者当前没有借阅记录");
} else {
System.out.println("该读者借阅的图书:");
for (Book book : result) {
System.out.println("\t" + book);
}
}
showReaderOPView(readerid);
}
private void showBorrowBookView(String readerid) {//借书
System.out.println("============借书============");
System.out.println("请输入借阅图书的编号");
String bookid = sc.next();
//先查看该图书是否存在
Book book = library.getBookNameByBookId((bookid));
if (book == null) {
System.out.println("不存在该编号的图书,无法借阅");
showReaderOPView(readerid);
return;
}
//书存在,向图书馆中增加一条借阅记录
if (library.addBorrowInfo(readerid, book)) {
System.out.println("借阅成功");
showReaderOPView(readerid);
} else {
System.out.println("失败");
showReaderOPView(readerid);
}
}
private void showReturnBookView(String readerid) {//还书
System.out.println("============还书============");
System.out.println("请输入 要归还图书的编号");
String bookid = sc.next();
//先查是否存在该书
Book book = library.getBookNameByBookId(bookid);
if (book == null) {
System.out.println("不存在该编号的图书,无需归还");
showReaderOPView(readerid);
return;
}
//存在,向图书管归还这本书
if (library.returnBook(readerid, book)) {
System.out.println("归还成功");
showReaderOPView(readerid);
} else {
System.out.println("归还失败");
showReaderOPView(readerid);
}
}
private void showAddReaderView() {
System.out.println("***************添加读者***************");
System.out.println("请输入读者姓名");
String name = sc.next();
//向图书馆添加读者
if(library.addReader(name)){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
private void showAllReaderView() {//展示读者
System.out.println("***************显示读者***************");
//从图书馆中查看所有读者
List<Reader> result = library.getAllReaders();
for (Reader reader : result) {
System.out.println(reader);
}
}
private void showDeleteReaderView() {//删除视图
System.out.println("***************删除读者***************");
System.out.println("请输入读者编号");
String readerid = sc.next();
//从图书馆中删除指定编号的读者
if(library.deleteReaderById(readerid)){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
private void showAddBookView() {
System.out.println("***************添加图书***************");
System.out.println("请输入图书编号");
String bookid = sc.next();
System.out.println("请输入图书名称");
String name = sc.next();
System.out.println("请输入作者");
String author = sc.next();
System.out.println("请输入价格");
int price = sc.nextInt();
//向图书馆中添加图书信息
if (library.addBook(bookid, name, author, price)) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
private void showAllBookView() {
System.out.println("********显示图书*******");
for(Book book:library.printAllBook()){
System.out.println(book);
}
}
private void showDeleteBookView() {
System.out.println("***************删除图书***************");
System.out.println("请输入图书编号");
String bookid = sc.next();
//从图书馆中删除编号为bookid的图书
if (library.deleteBook(bookid)) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
3.4 Library(整合了读者以及管理员的各种操作)
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Library implements Serializable{
static List<Reader> readerList = new ArrayList<Reader>();
Reader reader = new Reader();
//将图书信息保存到list集合中
static List<Book> booklist = new ArrayList<Book>();
//按名称查询图书
static private Map<String, String> adminMap = new HashMap<String, String>();
static{
readerList.add(new Reader("R001", "lisi", "123"));
readerList.add(new Reader("R002", "wangwu", "456"));
booklist.add(new Book("0789001", "java", "a", 87.5));
booklist.add(new Book("0789002", "C++", "b", 85.5));
adminMap.put("admin","666");
}
public boolean validAdmin(String name, String pwd) {
//验证输入的密码与依据输入的管理员账户在adminMap集合中查询出来的密码是否一样
return pwd.equals(adminMap.get(name));
}
public Library() {
}
//按名称查询
public List<Book> getBookByBookName(String bookname) {
List<Book> list = new ArrayList<Book>();
for (Book book : booklist) {
if (bookname.equals(book.getName())) {
list.add(book);
}
}
return list;
}
//按编号查询
public Book getBookNameByBookId(String bookId) {
for (Book book : booklist) {
if (bookId.equals(book.getBookid())) {
return book;
}
}
return null;
}
public boolean validReader(String readerid, String pwd) {//合法登录
for (Reader reader : readerList) {
if (readerid.equals(reader.getReaderid()) && pwd.equals(reader.getPassword())) {
return true;
}
}
return false;
}
public Reader getReaderById(String readerid) {//获取读者信息
for (Reader reader : readerList) {
if (readerid.equals(reader.getReaderid())) {
return reader;
}
}
return null;
}
public boolean modifyReaderPwd(String readerid, String oldPwd, String newPwd) {
Reader reader = getReaderById(readerid);
if (reader != null) {//读者存在
if (oldPwd.equals(reader.getPassword())) {
//旧密码一样,修改旧密码
reader.setPassword(newPwd);
return true;//修改成功
}
}
return false;//修改失败
}
private Map<String, List<Book>> borrowMap = new HashMap<String, List<Book>>();
public List<Book> getBorrowBooksByReaderId(String readerid) {
return borrowMap.get(readerid);
}
public boolean addBorrowInfo(String readerid, Book book) {
if (borrowMap.containsKey(readerid)) {
List<Book> list = borrowMap.get(readerid);
return list.add(book);
} else {
List<Book> list = new ArrayList<>();
list.add(book);
return borrowMap.put(readerid, list) == null;
}
}
public boolean returnBook(String readerid, Book book) {
//先拿到readerid读者查阅借的图书
List<Book> books = borrowMap.get(readerid);
if (books != null) {
return books.remove(book);//从借阅图书的集合中来删除该书即可
}
return false;
}
public boolean addReader(String name) {//添加读者
//创建Reader读者对象
Reader reader = new Reader();
//设置读者姓名
reader.setName(name);
//把读者密码默认初始化为123
reader.setPassword("123");
//读者编号设置为自动增长
if (readerList.size() == 0) {
//当前添加的是第一个读者
reader.setReaderid("R001");
} else {
//先从readerList集合中取最后一个读者
Reader lastReader = readerList.get(readerList.size() - 1);
//取最后一个读者的编号
String lastReaderidStr = lastReader.getReaderid();//"R002"
//取编号的数字
String lastReaderidNumStr = lastReaderidStr.substring(1);//"002"
//把编号转换为整数
int num = Integer.parseInt(lastReaderidNumStr);//2
//编号加1
num++;//3
//把数字3格式化为003,数字格式化使用DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("000");
String lastReaderId = decimalFormat.format(num);//"003"
reader.setReaderid("R"+lastReaderId);
}
//把读者reader添加到readerList集合中
return readerList.add(reader);
}
public List<Reader> getAllReaders() {
return readerList;
}
public boolean deleteReaderById(String readerid) {//删除读者
//从readerList集合中删除编号为readerid的读者
for (Iterator<Reader> iterator = readerList.iterator(); iterator.hasNext(); ) {
Reader reader = iterator.next();
if (readerid.equals(reader.getReaderid())) {
iterator.remove();
return true;
}
}
return false;
}
public boolean addBook(String bookid, String name, String author, int price) {
//创建book对象
Book book = new Book(bookid, name, author, price);
//把book对象添加到集合中
return booklist.add(book);
}
public List<Book> printAllBook(){
return booklist;
}
public boolean deleteBook(String bookid) {
for (Iterator<Book> iterator = booklist.iterator(); iterator.hasNext(); ) {
Book next = iterator.next();
if (bookid.equals(next.getBookid())) {
iterator.remove();
return true;
}
}
return false;
}
public static void IO() throws Exception{
ObjectOutputStream oos = null;
FileWriter fw = null;
ObjectInputStream ois = null;
FileReader fr = null;
try{
fr = new FileReader(new File("D://count.txt"));
char count=(char) fr.read();
if(count=='0'){
oos = new ObjectOutputStream(new FileOutputStream("D://book_reader.txt"));
ois = new ObjectInputStream(new FileInputStream("D://book_reader.txt"));
fw = new FileWriter(new File("D://count.txt"));
oos.writeObject(Library.booklist);
oos.writeObject(Library.readerList);
Library.booklist = (List<Book>)ois.readObject();
Library.readerList = (List<Reader>) ois.readObject();
count++;
fw.write(count+"");
}else{
ois = new ObjectInputStream(new FileInputStream("D://book_reader.txt"));
fw = new FileWriter(new File("D://count.txt"));
Library.booklist = (List<Book>)ois.readObject();
Library.readerList = (List<Reader>) ois.readObject();
count++;
fw.write(count+"");
}
}catch(Exception e) {
e.getStackTrace();
}finally {
if(fw!=null){
fw.close();
}
if(oos!=null){
oos.close();
}
if(ois!=null){
ois.close();
}
if(fr!=null){
fr.close();
}
}
}
}
3.5 Test测试类
public static void main(String[] args) throws Exception {
Library library = new Library();
library.IO();
ViewManager viewManager = new ViewManager();
viewManager.ShowMainView();
}
4.效果演示
5.总结
该项目最后运行结果截图中的功能都已经实现,就不再做一步步的展示了,感兴趣的小伙伴可以拿走放到Idea中运行一下。然后就是项目中的功能并没有做详细讲解,如果有有疑问的地方可以评论区留言。