图书管理系统案例练习
1.date数据包
package date;
import java.io.File;
import java.io.Serializable;
import entity.*;
public class DateBook implements Serializable{
private static final long serialVersionUID = -406169565854901850L;
public static Book[] book = new Book[100];
public static BookPersonAccount[] baa = new BookPersonAccount[100];
public static BookBorrowList[] bpbr = new BookBorrowList[100];
public static File fileBook = new File("fileBook.ini");
public static File BookPersonAccount = new File("BookPersonAccount.ini");
public static File BookBorrowList = new File("BookBorrowList.ini");
}
2.entity实体类包
package entity;
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 4617879176992662261L;
private String dvdName;
private int dvdNums;
private int dvdBNums;
private double dvdPrice;
public Book() {
super();
}
public Book(String dvdName, int dvdNums, double dvdPrice) {
super();
this.dvdName = dvdName;
this.dvdNums = dvdNums;
this.dvdBNums = 0;
this.dvdPrice = dvdPrice;
}
public String getDvdName() {
return dvdName;
}
public void setDvdName(String dvdName) {
this.dvdName = dvdName;
}
public int getDvdNums() {
return dvdNums;
}
public void setDvdNums(int dvdNums) {
this.dvdNums = dvdNums;
}
public int getDvdBNums() {
return dvdBNums;
}
public void setDvdBNums(int dvdBNums) {
this.dvdBNums = dvdBNums;
}
public double getDvdPrice() {
return dvdPrice;
}
public void setDvdPrice(double dvdPrice) {
this.dvdPrice = dvdPrice;
}
}
package entity;
import java.io.Serializable;
import java.util.Date;
public class BookBorrowList implements Serializable{
private static final long serialVersionUID = -3653504482410226115L;
private String personAccount;
private String bookName;
private Date borrowDate;
private Date returnDate;
public BookBorrowList() {
super();
}
public BookBorrowList(String personAccount, String bookName, Date borrowDate, Date returnDate) {
super();
this.personAccount = personAccount;
this.bookName = bookName;
this.borrowDate = borrowDate;
this.returnDate = returnDate;
}
public String getPersonAccount() {
return personAccount;
}
public void setPersonAccount(String personAccount) {
this.personAccount = personAccount;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Date getBorrowDate() {
return borrowDate;
}
public void setBorrowDate(Date borrowDate) {
this.borrowDate = borrowDate;
}
public Date getReturnDate() {
return returnDate;
}
public void setReturnDate(Date returnDate) {
this.returnDate = returnDate;
}
}
package entity;
import java.io.Serializable;
public class BookPersonAccount implements Serializable{
private static final long serialVersionUID = -3442924235778915049L;
private String personAccount;
private String pwg;
private String name;
public String getName() {
return name;
}
public BookPersonAccount(String personAccount, String pwg, String name) {
super();
this.personAccount = personAccount;
this.pwg = pwg;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public BookPersonAccount() {
super();
}
public String getPersonAccount() {
return personAccount;
}
public void setPersonAccount(String personAccount) {
this.personAccount = personAccount;
}
public String getPwg() {
return pwg;
}
public void setPwg(String pwg) {
this.pwg = pwg;
}
}
3.service逻辑包
package service;
import java.util.Date;
import date.DateBook;
import entity.Book;
import entity.BookBorrowList;
import entity.BookPersonAccount;
import util.DateUtil;
public class BookService {
public void showAllBookList() {
System.out.println("名字\t\t库存数量\t借阅数量\t借阅价格");
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
String str = String.format("%s\t\t%s\t%s\t%s\t", DateBook.book[i].getDvdName(),DateBook.book[i].getDvdNums(),DateBook.book[i].getDvdBNums(),DateBook.book[i].getDvdPrice());
System.out.println(str);
}
}
}
public Book bookNameExist(String name) {
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
if(DateBook.book[i].getDvdName().equals(name)) {
return DateBook.book[i];
}
}
}
return null;
}
public void borrowBook(Book book,BookPersonAccount account) {
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
if(DateBook.book[i].getDvdName().equals(book.getDvdName())) {
DateBook.book[i].setDvdNums(DateBook.book[i].getDvdNums() - 1);
DateBook.book[i].setDvdBNums(DateBook.book[i].getDvdBNums() + 1);
borrowBookList(DateBook.book[i],account);
break;
}
}
}
}
private void borrowBookList(Book book,BookPersonAccount account) {
for(int i = 0;i < DateBook.bpbr.length;i++) {
if(DateBook.bpbr[i] == null) {
BookBorrowList db = new BookBorrowList();
db.setPersonAccount(account.getPersonAccount());
db.setBookName(book.getDvdName());
db.setBorrowDate(new Date());
DateBook.bpbr[i] = db;
break;
}
}
}
public void showMyBookBorrowList(BookPersonAccount account) {
System.out.println("借阅人账户\t借阅书籍名称\t借阅日期\t\t归还日期");
for(int i = 0 ;i < DateBook.bpbr.length;i++) {
if(DateBook.bpbr[i] != null) {
if(DateBook.bpbr[i].getPersonAccount().equals(account.getPersonAccount())) {
String str = String.format("%s\t%s\t\t%s\t\t%s\t", DateBook.bpbr[i].getPersonAccount(),DateBook.bpbr[i].getBookName(),DateUtil.showDate(DateBook.bpbr[i].getBorrowDate()),DateUtil.showDate(DateBook.bpbr[i].getReturnDate()));
System.out.println(str);
}
}
}
}
public void showAllBookBorrowList() {
System.out.println("借阅人账户\t借阅书籍名称\t借阅日期\t归还日期");
for(int i = 0 ;i < DateBook.bpbr.length;i++) {
if(DateBook.bpbr[i] != null) {
String str = String.format("%s\t%s\t\t%s\t\t%s\t", DateBook.bpbr[i].getPersonAccount(),DateBook.bpbr[i].getBookName(),DateUtil.showDate(DateBook.bpbr[i].getBorrowDate()),DateUtil.showDate(DateBook.bpbr[i].getReturnDate()));
System.out.println(str);
}
}
}
public void returnBook(Book book, BookPersonAccount account) {
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
if(DateBook.book[i].getDvdName().equals(book.getDvdName())) {
DateBook.book[i].setDvdNums(DateBook.book[i].getDvdNums() + 1);
DateBook.book[i].setDvdBNums(DateBook.book[i].getDvdBNums() - 1);
returnBookList(DateBook.book[i],account);
break;
}
}
}
}
private void returnBookList(Book book,BookPersonAccount account) {
for(int i = 0;i < DateBook.bpbr.length;i++) {
if(DateBook.bpbr[i] != null) {
if(DateBook.bpbr[i].getPersonAccount().equals(account.getPersonAccount())) {
Date returnDate = new Date();
DateBook.bpbr[i].setReturnDate(returnDate);
Date borrowDate = DateBook.bpbr[i].getBorrowDate();
long time = returnDate.getTime() - borrowDate.getTime();
double price = (time/(1000*3600*24))*book.getDvdPrice();
System.out.println("还书成功,应付:" + price);
break;
}
}
}
}
public void addBook(String name ,int bookNum,double bookPrice) {
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
if(DateBook.book[i].getDvdName().equals(name)) {
DateBook.book[i].setDvdNums(DateBook.book[i].getDvdNums() + bookNum);
DateBook.book[i].setDvdPrice(bookPrice);
return;
}
}
}
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] == null) {
Book book = new Book(name,bookNum,bookPrice);
DateBook.book[i] = book;
break;
}
}
}
public void deleteBook(String name) {
for(int i = 0;i < DateBook.book.length;i++) {
if(DateBook.book[i] != null) {
if(DateBook.book[i].getDvdName().equals(name)) {
DateBook.book[i] = null;
break;
}
}
}
}
}
package service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import entity.Book;
import entity.BookBorrowList;
import entity.BookPersonAccount;
public class DateReadToFile {
public static Book[] readBook(File fileBook) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileBook));
Book[] book = (Book[])ois.readObject();
ois.close();
return book;
}
public static BookPersonAccount[] readBookAdminAccount(File fileBookAdminAccount) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileBookAdminAccount));
BookPersonAccount[] bookAdminAccount = (BookPersonAccount[])ois.readObject();
ois.close();
return bookAdminAccount;
}
public static BookBorrowList[] readBookPersonBorrowReturn(File fileBookPersonBorrowReturn) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileBookPersonBorrowReturn));
BookBorrowList[] bookPersonBorrowReturn = (BookBorrowList[])ois.readObject();
ois.close();
return bookPersonBorrowReturn;
}
}
package service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import entity.Book;
import entity.BookBorrowList;
import entity.BookPersonAccount;
public class DateSaveToFile {
public static void save(Book[] book,File fileBook) throws FileNotFoundException, IOException {
ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(fileBook));
ois.writeObject(book);
ois.close();
}
public static void save(BookPersonAccount[] baa,File fileBookAdminAccount) throws FileNotFoundException, IOException {
ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(fileBookAdminAccount));
ois.writeObject(baa);
ois.close();
}
public static void save(BookBorrowList[] bpbr,File fileBookPersonBorrowReturn) throws FileNotFoundException, IOException {
ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(fileBookPersonBorrowReturn));
ois.writeObject(bpbr);
ois.close();
}
}
package service;
import date.DateBook;
import entity.BookPersonAccount;
public class PersonAccountService {
public BookPersonAccount personAccountExist(String personAccount) {
for(int i = 0;i < DateBook.baa.length;i++) {
if(DateBook.baa[i] != null) {
if(DateBook.baa[i].getPersonAccount().equals(personAccount)) {
return DateBook.baa[i];
}
}
}
return null;
}
public boolean personPwgTrueOrFalse(String personInputPwg,String personSavePwg) {
return personInputPwg.equals(personSavePwg);
}
public void personLogin(String personAccount, String personPwg1,String personName) {
for(int i = 0;i < DateBook.baa.length;i++) {
if(DateBook.baa[i] == null) {
BookPersonAccount bpa = new BookPersonAccount(personAccount,personPwg1,personName);
DateBook.baa[i] = bpa;
break;
}
}
}
public void personChangePwg(BookPersonAccount pae) {
for(int i = 0;i < DateBook.baa.length;i++) {
if(DateBook.baa[i].getPersonAccount().equals(pae.getPersonAccount())) {
DateBook.baa[i] = pae;
break;
}
}
}
}
4.start启动包
package start;
import ui.BookMainUI;
public class StartApp {
public static void main(String[] args) {
BookMainUI bmui = new BookMainUI();
bmui.dvdMainUI();
}
}
5.ui界面包
package ui;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import date.DateBook;
import service.DateReadToFile;
import service.DateSaveToFile;
public class BookMainUI {
Scanner input = new Scanner(System.in);
PersonAccountUI pau = new PersonAccountUI();
public void dvdMainUI() {
try {
DateBook.book = DateReadToFile.readBook(DateBook.fileBook);
DateBook.baa = DateReadToFile.readBookAdminAccount(DateBook.BookPersonAccount);
DateBook.bpbr = DateReadToFile.readBookPersonBorrowReturn(DateBook.BookBorrowList);
} catch (FileNotFoundException e1) {
System.out.println("文件找不到!");
} catch (ClassNotFoundException e1) {
System.out.println("类找不到!");
} catch (IOException e1) {
System.out.println("读取失败!");
}
while (true) {
System.out.println("======================================================");
System.out.println("-------------------欢迎使用DVD租赁系统--------------------");
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("======================================================");
int choice = 0;
try {
choice = input.nextInt();
} catch (Exception e) {
System.out.println("您输入的指令类型不正确!请重新输入:");
}
if (choice == 1) {
pau.personOnloadUI();
} else if (choice == 2) {
pau.personLoginUI();
} else if (choice == 3) {
pau.personExistChangePwg();
} else if (choice == 4) {
break;
} else {
System.out.println("您输入的指令不匹配,请输入正确的数字!");
}
}
try {
DateSaveToFile.save(DateBook.book, DateBook.fileBook);
DateSaveToFile.save(DateBook.baa, DateBook.BookPersonAccount);
DateSaveToFile.save(DateBook.bpbr, DateBook.BookBorrowList);
} catch (FileNotFoundException e) {
System.out.println("文件找不到!");
} catch (IOException e) {
System.out.println("写入失败!");
}
}
}
package ui;
import java.util.Scanner;
import entity.Book;
import entity.BookPersonAccount;
import service.BookService;
import service.PersonAccountService;
public class PersonAccountUI {
private PersonAccountService pas = new PersonAccountService();
private Scanner input = new Scanner(System.in);
private BookService bs = new BookService();
public void personOnloadUI(){
System.out.println("======================================================");
System.out.println("请输入你的账号:");
String personAccount = input.next();
System.out.println("请输入你的密码:");
String personPwg = input.next();
BookPersonAccount pae = pas.personAccountExist(personAccount);
if(pae != null) {
boolean pptof = pas.personPwgTrueOrFalse(personPwg, pae.getPwg());
if(pptof) {
System.out.println("登陆成功!");
if(pae.getPersonAccount().equals("admin")) {
adminUI(pae);
}else {
personBorrowUI(pae);
}
}else {
System.out.println("您输入的密码不匹配,请重新输入!");
personOnloadUI();
}
}else {
System.out.println("您输入的账户不正确,请重新输入!");
System.out.println("是否继续登录?输入1.是(输入任意数字返回上一层)");
int choice = input.nextInt();
if(choice == 1) {
personOnloadUI();
}
}
}
public void adminUI(BookPersonAccount pae) {
while(true) {
System.out.println("======================================================");
System.out.println("1.查看列表2.添加书籍3.删除书籍4.查看所有借阅记录5.退出");
int choice = input.nextInt();
if(choice == 1) {
bs.showAllBookList();
}else if(choice == 2) {
addBookUI();
}else if(choice == 3) {
deleteBookUI();
}else if(choice == 4) {
bs.showAllBookBorrowList();
}else if(choice == 5) {
break;
}else {
System.out.println("请输入正确的数字!");
}
}
}
public void addBookUI() {
System.out.println("======================================================");
System.out.println("请输入你要添加的书名:");
String name = input.next();
System.out.println("请输入添加书籍的数量:");
int bookNum = input.nextInt();
System.out.println("请输入添加书籍的价格:");
double bookPrice = input.nextDouble();
bs.addBook(name, bookNum,bookPrice);
}
public void deleteBookUI() {
System.out.println("======================================================");
System.out.println("请输入要删除书籍的名称:");
String name = input.next();
bs.deleteBook(name);
}
public void personLoginUI() {
System.out.println("======================================================");
System.out.println("请输入你的昵称:");
String personName = input.next();
System.out.println("请输入你的账号:");
String personAccount = input.next();
System.out.println("请输入你的密码:");
String personPwg1 = input.next();
System.out.println("请第二次输入你的密码:");
String personPwg2 = input.next();
BookPersonAccount pae = pas.personAccountExist(personAccount);
if(pae == null) {
if(personPwg1.equals(personPwg2)) {
pas.personLogin(personAccount,personPwg1,personName);
System.out.println("注册成功");
}else {
System.out.println("您输入的密码不相同请重新输入!");
personLoginUI();
}
}else {
System.out.println("您输入的账户已存在,请重新输入!");
System.out.println("是否继续登录?输入1.是(输入任意数字返回上一层)");
int choice = input.nextInt();
if(choice == 1) {
personOnloadUI();
}
}
}
public void personExistChangePwg() {
System.out.println("======================================================");
System.out.println("请输入你要修改密码的账户:");
String personAccount = input.next();
System.out.println("请输入原来的密码:");
String personPwg = input.next();
BookPersonAccount pae = pas.personAccountExist(personAccount);
if(pae != null) {
boolean pptof = pas.personPwgTrueOrFalse(personPwg, pae.getPwg());
if(pptof) {
personChangePwg(pae);
}else {
System.out.println("您输入的密码不匹配,请重新输入!");
personExistChangePwg();
}
}else {
System.out.println("您输入的账户不正确,请重新输入!");
System.out.println("是否继续修改密码?输入1.是(输入其他任意数字返回上一层)");
int choice = input.nextInt();
if(choice == 1) {
personOnloadUI();
}
}
}
public void personChangePwg(BookPersonAccount pae) {
System.out.println("======================================================");
System.out.println("请输入你的新密码");
String newPwg1 = input.next();
System.out.println("请再次输入你的新密码");
String newPwg2 = input.next();
if(newPwg1.equals(newPwg2)) {
pae.setPwg(newPwg1);
pas.personChangePwg(pae);
System.out.println("密码修改成功");
}else {
System.out.println("前后输入的密码不一样!");
personChangePwg(pae);
}
}
public void personBorrowUI(BookPersonAccount account) {
while(true) {
System.out.println("======================================================");
System.out.println("1.查看图书列表2.借阅书籍3.查看我的借阅记录4.归还书籍5.退出");
int choice = input.nextInt();
if(choice == 1) {
bs.showAllBookList();
}else if(choice == 2){
bookBorrowUI(account);
}else if(choice == 3){
bs.showMyBookBorrowList(account);
}else if(choice == 4){
personReturnUI(account);
}else if(choice == 5){
break;
}else {
System.out.println("请输入正确的数字指令!");
}
}
}
private void bookBorrowUI(BookPersonAccount account) {
System.out.println("======================================================");
System.out.println("请输入你要借阅书籍的名字:");
String name = input.next();
Book book = bs.bookNameExist(name);
if(book != null) {
bs.borrowBook(book,account);
System.out.println("借阅成功!");
}else {
System.out.println("你输入的书名不存在,请重新选择!");
}
}
private void personReturnUI(BookPersonAccount account) {
System.out.println("======================================================");
System.out.println("请输入你要归还书籍的名字:");
String name = input.next();
Book book = bs.bookNameExist(name);
if(book != null) {
bs.returnBook(book,account);
System.out.println("归还成功!");
}else {
System.out.println("你输入的书名不存在,请重新选择!");
}
}
}
6.util工具包
package util;
public enum DateType {
FORMATS1,
FORMATS2,
FORMATS3
}
package util;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil implements Serializable{
private static final long serialVersionUID = 4217769403151410614L;
private static Date date = new Date();
private static final String FORMATS1 = "dd-MM-yyyy";
private static final String FORMATS2 = "ddMMyyyy";
private static final String FORMATS3 = "dd年MM月yyyy日";
public static String BorrowDate(DateType formate) {
SimpleDateFormat sdf = null;
switch (formate) {
case FORMATS1:
sdf = new SimpleDateFormat(FORMATS1);
break;
case FORMATS2:
sdf = new SimpleDateFormat(FORMATS2);
break;
case FORMATS3:
sdf = new SimpleDateFormat(FORMATS3);
break;
}
return sdf.format(date);
}
public static Date strToDate(String date , DateType formate) throws ParseException {
SimpleDateFormat sdf = null;
switch (formate) {
case FORMATS1:
sdf = new SimpleDateFormat(FORMATS1);
break;
case FORMATS2:
sdf = new SimpleDateFormat(FORMATS2);
break;
case FORMATS3:
sdf = new SimpleDateFormat(FORMATS3);
break;
}
return sdf.parse(date);
}
public static int dateCale(Date date1, Date date2) {
long time = date1.getTime() - date2.getTime();
int day = (int)(time/(1000 * 3600 * 24));
return day;
}
public static String showDate(Date date) {
if(date != null) {
SimpleDateFormat sdf = new SimpleDateFormat(FORMATS1);
String str = sdf.format(date);
return str;
}
return "";
}
}