package week6.bookManagement;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
/**
*
* @author quanby
*
*/
public class BookUtil {
/**
* 书籍帮助类
*/
public BookUtil() {
super();
// TODO Auto-generated constructor stub
}
/**
* 判断输入的用户名和密码是否正确
*
* @param userId
* @param userPWD
* @return 账号密码匹配的布尔值
*/
public boolean checkPass(String userId, String userPWD) {
boolean result = false;//
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./src/week6/bookManagement/user.properties"));
if (userId.equals(properties.get("userId")) && userPWD.equals(properties.get("userPWD"))) {
result = true;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 显示书籍,当isAll为true时,显示全部书籍,否则显示可借书籍
*
* @param books
* 一个数量大于零的书籍列表
* @param isAll
* 是否显示全部的书籍列表
*/
public void showBooks(ArrayList<Book> books, boolean isAll) {
int result = 0;// 将可出借的书籍数量初始为0
Book book;
for (int i = 0; i < books.size(); i++) {
book = books.get(i);
if (!isAll) {
if (book.getStatus() == 0) {
result++;
System.out.println("书籍id:" + book.getBookID() + "\t" + "书名:" + book.getBookName() + "\t" + "作者:"
+ book.getAuthor() + "\t" + "价格:" + book.getPrice());
}
if (result == 0) {
System.out.println("对不起,无法借书,全部书籍已借出!");
}
} else {
System.out.println("书籍id:" + book.getBookID() + "\t" + "书名:" + book.getBookName() + "\t" + "作者:"
+ book.getAuthor() + "\t" + "价格:" + book.getPrice());
}
}
}
/**
* 修改一本书的状态
*
* @param books
* 一个书籍列表
* @param bookID
* 要修改的书籍id
* @param status
* 要修改的书籍状态,0--归还;1--表示借出
* @return 修改后的书籍列表
*/
public ArrayList<Book> changeStaus(ArrayList<Book> books, String bookID, int status) {
int result = 0;
Book book;
for (int i = 0; i < books.size(); i++) {
book = books.get(i);
// 存在书籍且状态与传递的状态不一致时才执行操作
if (book.getBookID().equals(bookID) && book.getStatus() != status) {
book.setStatus(status);
books.set(i, book);
writeLog(book, status);
result = 1;
}
}
if (result == 1) {
System.out.println("操作成功!");
} else {
System.out.println("操作失败!");
}
return books;
}
/**
* 对书籍进出记录日志
*
* @param book
* 书籍
* @param status
* 书籍原来的状态,0--正在归还;1--正在借出
*/
private void writeLog(Book book, int status) {
File file = new File("./src/week6/bookManagement/bookLog.txt");
FileOutputStream stream;
Calendar date = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
String time = sdf.format(date.getTime());
String t = "出借";
if (status == 0) {
t = "归还";
}
try {
stream = new FileOutputStream(file, true);
byte[] bytes = (time+t+"<<" + book.getBookName() + ">>" + "\n").getBytes();
stream.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}