一、前言
當(dāng)前,我還沒(méi)有學(xué)到數(shù)據(jù)庫(kù)相關(guān)的知識(shí),想要完全通過(guò)Java來(lái)完成一個(gè)可以存儲(chǔ)數(shù)據(jù)的圖書(shū)館管理系統(tǒng)就只能使用I/O流的知識(shí),將內(nèi)容都保存到文件中,再進(jìn)行增刪改查的操作,這就很考驗(yàn)我們的Java基礎(chǔ)知識(shí)掌握能力。
二、項(xiàng)目介紹
- Java基本語(yǔ)法的掌握
- 流程控制語(yǔ)句的熟練使用
- 面向?qū)ο笏枷氲睦斫?/li>
- 能否熟練使用封裝、繼承、多態(tài)
- 接口、異常的熟悉
- 集合的使用
- 是否熟悉掌握I/O流相關(guān)操作
三、項(xiàng)目說(shuō)明
- 傳統(tǒng)的人工管理圖書(shū)館流程繁瑣,我們需求設(shè)計(jì)一個(gè)圖書(shū)館管理系統(tǒng)來(lái)方便學(xué)生借書(shū)以及管理員管理書(shū)籍
- 系統(tǒng)功能分為:讀者信息管理模塊、圖書(shū)信息管理模塊、圖書(shū)借閱管理模塊、基礎(chǔ)信息維護(hù)模塊和用戶管理模塊。
- 讀者信息管理:能對(duì)讀者基本信息管理,如:新增讀者、信息修改、查詢
- 圖書(shū)信息管理:能管理圖書(shū)基本信息,如新增圖書(shū)、刪除圖書(shū)、查詢圖書(shū)等
- 圖書(shū)借閱信息管理:能對(duì)借閱信息進(jìn)行記錄,包括讀者信息、圖書(shū)信息、借閱時(shí)間等。
- 圖書(shū)歸還信息管理:能對(duì)圖書(shū)歸還信息進(jìn)行判斷,是否超出借閱時(shí)間,罰金為多少
四、項(xiàng)目實(shí)現(xiàn)
代碼部分如下:
Person類(lèi):
package User;
public class Person {
public String name;//姓名
public String sex;//性別
public int age;//年齡
public Person() {
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
Book類(lèi):
package Book;
public class Book {
private String bookName; //書(shū)名
private String author; //作者
public double price; //價(jià)格
private String category; //分類(lèi)
public String press;//出版社
private String state; //狀態(tài) true-已借出 false-未借出
private String borrowName;
private String borrowTime;
public Book() {
}
public Book(String bookName, String author, double price, String category, String press, String state, String borrowName, String borrowTime) {
this.bookName = bookName;
this.author = author;
this.price = price;
this.category = category;
this.press = press;
this.state = state;
this.borrowName = borrowName;
this.borrowTime = borrowTime;
}
public String getName() {
return bookName;
}
public void setName(String bookName) {
this.bookName = bookName;
}
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;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getBorrowName() {
return borrowName;
}
public void setBorrowName(String borrowName) {
this.borrowName = borrowName;
}
public String getBorrowTime() {
return borrowTime;
}
public void setBorrowTime(String borrowTime) {
this.borrowTime = borrowTime;
}
@Override
public String toString() {
return bookName + "," + author + "," + price + "," + category + "," + press + "," + state + "," + borrowName + "," + borrowTime;
}
}
Library類(lèi):
package Library;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import User.User;
import User.Admin;
public class Library {
Scanner sc = new Scanner(System.in);
private String operator;
//用戶注冊(cè)登錄
public void Interface() throws IOException, ParseException {
boolean a = true;
File file = new File("用戶信息.txt");
try {
while (a) {
System.out.println("==================圖書(shū)管理系統(tǒng)==================");
System.out.println("請(qǐng)登錄:1.普通用戶 2.管理員登錄 3.注冊(cè) 4.退出系統(tǒng)");
int i = sc.nextInt();
if (i < 1 || i > 4) {
System.out.println("到底能不能行?能不能看明白了再輸!");
} else if (i == 4) {
a = false;
} else if (i == 1) {
//普通用戶登錄
System.out.println("請(qǐng)輸入您的用戶名:");
operator = sc.next();
System.out.println("請(qǐng)輸入您的密碼:");
String password = sc.next();
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
//判斷輸入的用戶名是否存在
boolean flag = false;
String[] userinf = new String[5];
for (String user : list) {
userinf = user.split(",");
if (operator.equals(userinf[0])) { //判斷用戶名是否存在
flag = true;
break;
}
}
//如果存在,判斷密碼是否正確
if (flag == true) {
if (password.equals(userinf[1])) {
System.out.println("登陸成功");
//寫(xiě)日志
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file2 = new File("日志.txt");
FileWriter fw1 = new FileWriter(file2, true);
fw1.write(returnTime + " " + operator + "登錄了");
fw1.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw1.close();
//登錄
try {
User user = new User();
user.operation(userinf, operator);
} catch (Exception e) {
System.out.println("到底能不能行?能不能看明白了再輸!");
User user = new User();
user.operation(userinf, operator);
}
} else {
System.out.println("密碼錯(cuò)誤");
}
} else {
System.out.println("用戶名不存在");
}
intput.close();
reader.close();
} else if (i == 2) {
//管理員登錄
System.out.println("請(qǐng)輸入管理員密碼!");
String rootPassword = sc.next();
if (rootPassword.equals("88888888")) {
//管理員登錄成功
System.out.println("管理員登陸成功!");
try {
Admin admin = new Admin();
admin.admin();
} catch (Exception e) {
System.out.println("到底能不能行?能不能看明白了再輸!");
Admin admin = new Admin();
admin.admin();
}
} else {
System.out.println("你是不是管理員心里沒(méi)個(gè)ACD數(shù)嗎?");
}
} else if (i == 3) {
//注冊(cè)
System.out.println("請(qǐng)輸入用戶名:");
String name = sc.next();
//查找是否有同名用戶
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
String[] userinf = new String[5];
boolean flag = false;
for (String user : list) {
userinf = user.split(",");
if (name.equals(userinf[0])) { //判斷用戶名是否存在
flag = true;
System.out.println("該用戶名已存在,請(qǐng)重新擬取名稱");
break;
}
}
if (flag == false) {
System.out.println("請(qǐng)輸入密碼:");
String password = sc.next();
String sex;
while (true) {
System.out.println("請(qǐng)輸入您的性別:");
sex = sc.next();
if (sex.equals("男") || sex.equals("女")) {
break;
} else {
System.out.println("請(qǐng)輸入正確的性別!");
}
}
System.out.println("請(qǐng)輸入您的年齡:");
int age = sc.nextInt();
if (age < 10) {
System.out.println("您的年齡太小啦!請(qǐng)讓家長(zhǎng)使用賬號(hào)幫您借閱書(shū)籍!");
break;
}
long phone;
while (true) {
System.out.println("請(qǐng)輸入您的電話號(hào)碼:");
phone = sc.nextInt();
if (phone < 0) {
System.out.println("請(qǐng)輸入正確的電話號(hào)碼!");
} else {
break;
}
}
User u = new User(name, sex, age, phone, password);
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
fw.write(u.toString());
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
System.out.println("恭喜您,注冊(cè)成功!");
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file1 = new File("日志.txt");
FileWriter fileWriter = new FileWriter(file1, true);
fileWriter.write(returnTime + " 注冊(cè)了新用戶:" + name);
fileWriter.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fileWriter.close();
}
}
}
} catch (Exception e) {
System.out.println("到底能不能行?能不能看明白了再輸!");
Library library = new Library();
library.Interface();
return;
}
}
}
添加書(shū)籍AddBook:
package Book;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AddBook {
public void addBook() throws IOException {
Scanner sc = new Scanner(System.in);
File file = new File("書(shū)籍.txt");
System.out.println("請(qǐng)輸入書(shū)名:");
String bookName = sc.next();
//查找是否有同名書(shū)籍
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
String[] bookinf = new String[8];
boolean flag = false;
for (String user : list) {
bookinf = user.split(",");
if (bookName.equals(bookinf[0])) { //判斷書(shū)籍是否存在
flag = true;
System.out.println("該書(shū)籍已存在,小圖書(shū)館不整這么多一樣的書(shū)了,浪費(fèi)錢(qián)!");
break;
}
}
if (flag == false) {
System.out.println("請(qǐng)輸入作者:");
String author = sc.next();
double price;
while (true) {
System.out.println("請(qǐng)輸入書(shū)籍價(jià)格:");
price = sc.nextDouble();
if (price < 10) {
System.out.println("這么便宜?要不然直接白送?能不能合理一點(diǎn)?");
} else if (price > 500) {
System.out.println("這河里嗎?這么金貴的書(shū)你舍得借嗎?這么小個(gè)圖書(shū)館放這么貴的書(shū)河里嗎?");
} else {
break;
}
}
System.out.println("請(qǐng)輸入書(shū)籍類(lèi)型:");
String category = sc.next();
System.out.println("請(qǐng)輸入書(shū)籍的出版社:");
String press = sc.next();
String state = "false";
String borrowName = "無(wú)";
String borrowTime = "null";
Book book = new Book(bookName, author, price, category, press, state, borrowName, borrowTime);
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
fw.write(book.toString());
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
System.out.println("恭喜您,添加書(shū)籍成功!");
}
}
}
借閱書(shū)籍BorrowBook:
該類(lèi)中使用了修改文本中特定字符串的內(nèi)容的方法,該方法的使用探討我已經(jīng)寫(xiě)成博文發(fā)布,可以點(diǎn)擊以下鏈接進(jìn)行查看:Java中查找文本中特定內(nèi)容后進(jìn)行修改
package Book;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class BorrowBook {
Scanner scanner = new Scanner(System.in);
public void borrowBook(String operator) throws IOException {
String filePath = "書(shū)籍.txt";
BorrowBook modifyFile = new BorrowBook();
File bookFile = new File("書(shū)籍.txt");
//根據(jù)書(shū)籍名稱查詢到需要修改的書(shū)籍?dāng)?shù)據(jù)
FileInputStream intput = new FileInputStream(bookFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString = null;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
boolean flag = false;
String[] bookinf = new String[8];
System.out.println("=======================當(dāng)前可借閱書(shū)籍有=======================");
int i = 0;
for (String user : list) {
bookinf = user.split(",");
if (bookinf[5].equals("false")) {
System.out.print(" | " + bookinf[0] + " |");
i++;
if (i % 5 == 0) {
System.out.println("");
System.out.println("==========================================================");
}
}
}
if (i % 5 != 0) {
System.out.println("");
System.out.println("==========================================================");
}
System.out.println("請(qǐng)輸入要借閱的書(shū)籍名稱:");
String bookName = scanner.next();
for (String user : list) {
bookinf = user.split(",");
if (bookName.equals(bookinf[0])) { //判斷書(shū)籍是否存在
flag = true;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] +
",類(lèi)型:" + bookinf[3] + ",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5]);
break;
}
}
if (flag == false) {
System.out.println("沒(méi)有查找到該書(shū)籍,請(qǐng)重新確認(rèn)后再進(jìn)行查找!");
return;
}
//修改書(shū)籍?dāng)?shù)據(jù)后,重新寫(xiě)入文件
if (bookName.equals(bookinf[0]) && bookinf[5].equals("false")) {
boolean result = modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, "false", "true"));
// 修改文件中借閱狀態(tài)
if (result == true) {
//獲取借書(shū)人后寫(xiě)入“無(wú)”
modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, "無(wú)", operator));
//獲取時(shí)間后寫(xiě)入最后一位
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String borrowTime = sdf.format(nowDate);
modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, "null", borrowTime));
System.out.println("借閱成功!");
// System.out.println("借閱時(shí)間:" + borrowTime + ",最多可借閱7天,請(qǐng)記得按時(shí)歸還!");
System.out.println("借閱時(shí)間:" + borrowTime + ",最多可借閱20秒,請(qǐng)記得按時(shí)歸還!");//為方便測(cè)試,將借閱時(shí)間設(shè)置為20秒
//寫(xiě)日志
File file = new File("日志.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(borrowTime + " " + operator + "借走了圖書(shū):" + bookinf[0]);
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
return;
}
} else {
System.out.println("該書(shū)已被借出或不存在,請(qǐng)重新查詢后再進(jìn)行借閱!");
}
}
// 讀文件
public String readFileContent(String[] bookinf, String filePath, String oldString, String newString) {
Scanner sc = new Scanner(System.in);
BufferedReader br = null;
String line = null;
StringBuffer bufAll = new StringBuffer();// 保存修改過(guò)后的所有內(nèi)容
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
// 修改內(nèi)容核心代碼
String[] bookinf2 = line.split(",");
if (bookinf2[0].equals(bookinf[0])) {//判斷條件根據(jù)自己的要求修改
buf.append(line);
int indexOf = line.indexOf(oldString);
buf.replace(indexOf, indexOf + oldString.length(), newString);// 修改內(nèi)容
buf.append(System.getProperty("line.separator"));// 添加換行
bufAll.append(buf);
} else {
buf.append(line);
buf.append(System.getProperty("line.separator"));
bufAll.append(buf);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
br = null;
}
}
}
return bufAll.toString();
}
// 寫(xiě)文件
public boolean writeFile(String filePath, String content) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
return true;
}
}
刪除書(shū)籍DeleteBook:
package Book;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DeleteBook {
private String bookDelete;
String[] bookinf = new String[8];
String[] bookinf1 = new String[8];
public void deleteBook() throws IOException {
File file = new File("書(shū)籍.txt");
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;//定義一個(gè)字符串,每一次讀出該行字符串內(nèi)容
List<String> list = new ArrayList<>();//定義一個(gè)list字符串集合用來(lái)儲(chǔ)存每一行的字符串信息
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
System.out.println("==========當(dāng)前書(shū)籍有==========");
//遍歷字符串集合
for (String book : list) {
bookinf = book.split(",");//將‘,‘作為分隔符,將字符串分隔開(kāi)存放進(jìn)入數(shù)組中
System.out.print(bookinf[0] + " ");
}
System.out.println("");
//輸入要?jiǎng)h除的內(nèi)容
System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)籍名稱:");
Scanner scanner = new Scanner(System.in);
bookDelete = scanner.next();
//查詢?cè)撚脩羰欠裼形礆w還的書(shū)籍,如果存在未歸還的書(shū)籍,將不能刪除該用戶的信息
FileInputStream inputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String bookString;
List<String> bookList = new ArrayList<>();
while ((bookString = bufferedReader.readLine()) != null) {
bookList.add(bookString);
}
boolean have = false;//是否存在書(shū)籍
for (String borrowUser : bookList) {
bookinf = borrowUser.split(",");
//判斷書(shū)籍是否存在
if (bookDelete.equals(bookinf[0])) {
have = true;
}
//若該書(shū)籍存在,判斷書(shū)籍對(duì)應(yīng)用戶位是否存在用戶
//若對(duì)應(yīng)位用戶不為“無(wú)”,則表示該書(shū)籍有人借出,則無(wú)法刪除該書(shū)籍
//同時(shí)告知管理員(打印)該書(shū)籍被哪位用戶借出尚未歸還
if (!"無(wú)".equals(bookinf[6]) && have) {
System.out.println("該書(shū)籍尚未被用戶“" + bookinf[6] + "”歸還,請(qǐng)?jiān)谄錃w還書(shū)籍后再進(jìn)行操作!");
return;
}
}
if (have == false) {
System.out.println("無(wú)法找到該書(shū)籍,請(qǐng)重新確認(rèn)后再進(jìn)行操作!");
return;
}
for (String delBook : list) {
bookinf1 = delBook.split(",");
//找到即將刪除的書(shū)籍在集合中的位置,將該部分內(nèi)容從集合中刪除,然后清空整個(gè)文件
if (bookDelete.equals(bookinf1[0])) {
list.remove(delBook);//在集合中刪除該行
FileWriter fd = new FileWriter(file, false);//append傳入false表示寫(xiě)入內(nèi)容時(shí)將會(huì)覆蓋文件中之前存在的內(nèi)容
fd.write("");//執(zhí)行刪除操作,寫(xiě)入空內(nèi)容覆蓋之前的內(nèi)容
fd.close();
break;
}
}
//重新遍歷一遍更改后的集合,將內(nèi)容重新寫(xiě)入文件內(nèi)
for (String user : list) {
bookinf1 = user.split(",");
FileWriter fw = new FileWriter(file, true);//append傳入true表示寫(xiě)入內(nèi)容時(shí)將不會(huì)覆蓋文件中之前存在的內(nèi)容,將新的內(nèi)容寫(xiě)在之前內(nèi)容的后面
fw.write(bookinf1[0] + "," + bookinf1[1] +
"," + bookinf1[2] + "," + bookinf1[3] +
"," + bookinf1[4] + "," + bookinf1[5] +
"," + bookinf1[6] + "," + bookinf1[7]);//執(zhí)行重新寫(xiě)入內(nèi)容的操作,將修改過(guò)的集合通過(guò)數(shù)組讀下標(biāo)后,再重新存寫(xiě)入文件中
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
}
System.out.println("刪除成功!");
}
}
歸還書(shū)籍ReturnBook:
package Book;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class ReturnBook {
Scanner scanner = new Scanner(System.in);
public void returnBook(String operator) throws IOException, ParseException {
String filePath = "書(shū)籍.txt";
ReturnBook modifyFile = new ReturnBook();
File bookFile = new File("書(shū)籍.txt");
//根據(jù)書(shū)籍名稱查詢到需要修改的書(shū)籍?dāng)?shù)據(jù)
FileInputStream intput = new FileInputStream(bookFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString = null;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
int flag = 0;
String[] bookinf = new String[8];
String[] bookinf1 = new String[8];
System.out.println("=======================當(dāng)前已借閱書(shū)籍有=======================");
int i = 0;
for (String user : list) {
bookinf1 = user.split(",");
if (bookinf1[6].equals(operator)) {
System.out.print(" | " + bookinf1[0] + " |");
i++;
if (i % 5 == 0) {
System.out.println("");
System.out.println("==========================================================");
}
}
}
if (i % 5 != 0) {
System.out.println("");
System.out.println("==========================================================");
}
System.out.println("請(qǐng)輸入要?dú)w還的書(shū)籍名稱:");
String bookName = scanner.next();
for (String user : list) {
bookinf = user.split(",");
if (bookName.equals(bookinf[0]) && operator.equals(bookinf[6])) {
//判斷書(shū)籍是否存在,且為本人所借出
//若屬實(shí)則輸出書(shū)籍完整信息
flag = 1;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] + ",類(lèi)型:" + bookinf[3] +
",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5] + ",借書(shū)人:" + bookinf[6] + ",借出日期:" + bookinf[7]);
//判斷是否超時(shí),超時(shí)需付罰金
long charge = 0;//兩個(gè)時(shí)間相差的天數(shù)
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);//獲取歸還時(shí)間
Date d1 = sdf.parse(bookinf[7]);//借出時(shí)間
Date d2 = sdf.parse(returnTime);//歸還時(shí)間
charge = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);//天
long charge1 = (d2.getTime() - d1.getTime()) / (1000);//秒
//逾期書(shū)籍將繳納罰金
if (charge1 <= 20) {
System.out.println("該書(shū)籍被您借出" + charge1 + "秒,感謝您規(guī)范用書(shū)!");
} else if (charge1 > 20) {
System.out.println("該書(shū)籍被您借出" + charge1 + "秒,您已逾期" + (charge1 - 20) + "秒,需交罰金:" + (charge1 - 20) * 5 + "元,謝謝配合!");
}
// if (charge <= 7) {
// System.out.println("該書(shū)籍被您借出" + charge + "天,感謝您規(guī)范用書(shū)!");
// } else if (charge > 7) {
// System.out.println("該書(shū)籍被您借出" + charge + "天,您已逾期" + (charge - 7) + "天,需交罰金:" + (charge - 7) * 5 + "元,謝謝配合!");
// }
break;
} else if (bookName.equals(bookinf[0]) && !operator.equals(bookinf[6]) && bookinf[5].equals("true")) {
//判斷書(shū)籍是否存在,且為本人所借出
//若存在該書(shū)籍且被借出,但不是本人所借出,只輸出該書(shū)籍基本信息
flag = 2;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] +
",類(lèi)型:" + bookinf[3] + ",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5]);
break;
} else if (bookName.equals(bookinf[0]) && bookinf[5].equals("false")) {
//判斷書(shū)籍是否存在,且為本人所借出
//若存在該書(shū)籍但并未被借出,只輸出該書(shū)籍基本信息
flag = 3;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] +
",類(lèi)型:" + bookinf[3] + ",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5]);
break;
}
}
if (flag == 0) {
System.out.println("沒(méi)有查找到該書(shū)籍,請(qǐng)重新確認(rèn)后再進(jìn)行查找!");
} else if (flag == 2) {
System.out.println("該書(shū)不是由您所借出,請(qǐng)查證后再輸入歸還的書(shū)籍!");
} else if (flag == 3) {
System.out.println("該書(shū)并未被借出,請(qǐng)查證后再輸入歸還的書(shū)籍!");
}
//修改書(shū)籍?dāng)?shù)據(jù)后,重新寫(xiě)入文件
else if (flag == 1) {
if (bookName.equals(bookinf[0]) && bookinf[5].equals("true") && operator.equals(bookinf[6])) {
modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, "true", "false"));
// 修改文件中借閱狀態(tài)
modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, operator, "無(wú)"));
modifyFile.writeFile(filePath, modifyFile.readFileContent(bookinf, filePath, bookinf[7], "null"));
System.out.println("歸還成功!");
//寫(xiě)日志
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file = new File("日志.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(returnTime + " " + operator + "歸還了圖書(shū):" + bookinf[0]);
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
return;
}
}
}
// 讀文件
public String readFileContent(String[] bookinf, String filePath, String oldString, String newString) {
Scanner sc = new Scanner(System.in);
BufferedReader br = null;
String line = null;
StringBuffer bufAll = new StringBuffer();// 保存修改過(guò)后的所有內(nèi)容
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
// System.out.println("請(qǐng)確認(rèn)您的用戶名:");
// String username = sc.next();
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
// 修改內(nèi)容核心代碼
String[] bookinf2 = line.split(",");
if (bookinf2[0].equals(bookinf[0])) {//判斷條件根據(jù)自己的要求修改
buf.append(line);
int indexOf = line.indexOf(oldString);
buf.replace(indexOf, indexOf + oldString.length(), newString);// 修改內(nèi)容
buf.append(System.getProperty("line.separator"));// 添加換行
bufAll.append(buf);
} else {
buf.append(line);
buf.append(System.getProperty("line.separator"));
bufAll.append(buf);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
br = null;
}
}
}
return bufAll.toString();
}
// 寫(xiě)文件
public boolean writeFile(String filePath, String content) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
return true;
}
}
查詢書(shū)籍FindBook:
package Book;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class FindBook {
Scanner scanner = new Scanner(System.in);
File bookFile = new File("書(shū)籍.txt");
public void findBook(String operator) throws IOException {
FileInputStream intput = new FileInputStream(bookFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString = null;
// int line = 1;
List<String> list = new ArrayList<>();
// System.out.println("書(shū)籍 | 作者 | 價(jià)格 | 類(lèi)型 | 出版社 | 是否被借出");
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
// System.out.println(tempString);
// line++;
}
boolean flag = false;
String[] bookinf = new String[8];
System.out.println("=========================當(dāng)前書(shū)籍有=========================");
int i = 0;
for (String user : list) {
bookinf = user.split(",");
System.out.print(" | " + bookinf[0] + " 是否被借出:" + bookinf[5] + " |");
i++;
if (i % 2 == 0) {
System.out.println("");
System.out.println("==========================================================");
}
}
if (i % 2 != 0) {
System.out.println("");
System.out.println("==========================================================");
}
System.out.println("請(qǐng)輸入要查看詳細(xì)信息的書(shū)籍名稱:");
String bookName = scanner.next();
for (String user : list) {
bookinf = user.split(",");
if (bookName.equals(bookinf[0])) { //判斷書(shū)籍是否存在
flag = true;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] +
",類(lèi)型:" + bookinf[3] + ",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5]);
//寫(xiě)日志
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String findTime = sdf.format(nowDate);
File file = new File("日志.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(findTime + " " + operator + "查詢了圖書(shū):" + bookinf[0]);
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
break;
}
}
if (flag == false) {
System.out.println("沒(méi)有查找到該書(shū)籍,請(qǐng)重新確認(rèn)后再進(jìn)行查找!");
}
}
public void adminFindBook() throws IOException {
FileInputStream intput = new FileInputStream(bookFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString = null;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
// System.out.println(tempString);
}
String[] bookinf = new String[8];
System.out.println("=========================當(dāng)前書(shū)籍有=========================");
int i = 0;
for (String user : list) {
bookinf = user.split(",");
System.out.print(" | " + bookinf[0] + " |");
i++;
if (i % 5 == 0) {
System.out.println("");
System.out.println("==========================================================");
}
}
System.out.println("");
System.out.println("==========================================================");
System.out.println("請(qǐng)輸入要查看詳細(xì)信息的書(shū)籍名稱:");
String bookName = scanner.next();
boolean flag = false;
for (String user : list) {
bookinf = user.split(",");
if (bookName.equals(bookinf[0])) { //判斷書(shū)籍是否存在
flag = true;
System.out.println("書(shū)名:" + bookinf[0] + ",作者:" + bookinf[1] + ",價(jià)格:" + bookinf[2] + ",類(lèi)型:" + bookinf[3] +
",出版社:" + bookinf[4] + ",是否被借出:" + bookinf[5] + ",借書(shū)人:" + bookinf[6] + ",借出日期:" + bookinf[7]);
break;
}
}
if (flag == false) {
System.out.println("沒(méi)有查找到該書(shū)籍,請(qǐng)重新確認(rèn)后再進(jìn)行查找!");
}
}
}
用戶界面User:
package User;
import Book.BorrowBook;
import Book.FindBook;
import Book.ReturnBook;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class User extends Person {
//用戶屬性
private long Uphone;
private String Upassword;
public User() {
}
public User(String name, String sex, int age, long Uphone, String Upassword) {
super(name, sex, age);
this.Uphone = Uphone;
this.Upassword = Upassword;
}
public long getUphone() {
return Uphone;
}
public void setUphone(int uphone) {
Uphone = uphone;
}
public String getUpassword() {
return Upassword;
}
public void setUpassword(String upassword) {
Upassword = upassword;
}
public String toString() {
return name + "," + Upassword + "," + sex + "," + age + "," + Uphone;
}
public void operation(String[] userinf, String operator) throws IOException, ParseException {
File bookFile = new File("書(shū)籍.txt");
FindBook fb = new FindBook();
while (true) {
System.out.println("===============普通用戶操作界面===============");
System.out.println("1.查詢書(shū)籍 2.借閱書(shū)籍 3.歸還書(shū)籍 4.修改密碼 5.返回");
Scanner sc = new Scanner(System.in);
int j = sc.nextInt();
if (j < 1 || j > 5) {
System.out.println("到底能不能行?能不能看明白了再輸!");
} else if (j == 5) {
return;
} else if (j == 1) {//查詢書(shū)籍
fb.findBook(operator);
} else if (j == 2) {//借閱書(shū)籍
BorrowBook bb = new BorrowBook();
bb.borrowBook(operator);
} else if (j == 3) {//歸還書(shū)籍
ReturnBook gbb = new ReturnBook();
gbb.returnBook(operator);
} else if (j == 4) {//修改密碼
String filePath = "用戶信息.txt";
User modifyFile = new User();
System.out.println("請(qǐng)輸入原密碼:");
String oldString = sc.next();
//如果輸入的原密碼不正確,無(wú)法進(jìn)行修改,如果正確,才能進(jìn)行修改
if (oldString.equals(userinf[1])) {
System.out.println("請(qǐng)輸入新密碼:");
String newString = sc.next();
boolean result = modifyFile.writeFile(filePath, modifyFile.readFileContent(userinf, filePath, oldString, newString));// 修改文件中密碼
// 如果修改結(jié)果為true,進(jìn)行修改成功提示,否則提示修改失敗
if (result == true) {
System.out.println("修改成功,請(qǐng)重新登錄!");
//寫(xiě)日志
Date nowDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String returnTime = sdf.format(nowDate);
File file = new File("日志.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(returnTime + " " + operator + "修改了密碼");
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
return;
} else {
System.out.println("修改錯(cuò)誤,請(qǐng)檢查后重新修改!");
}
} else {
System.out.println("輸入錯(cuò)誤,請(qǐng)檢查后重新進(jìn)行修改!");
}
}
}
}
// 讀文件
public String readFileContent(String[] userinf, String filePath, String oldString, String newString) {
Scanner sc = new Scanner(System.in);
BufferedReader br = null;
String line = null;
StringBuffer bufAll = new StringBuffer();// 保存修改過(guò)后的所有內(nèi)容
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
// 修改內(nèi)容核心代碼
String[] userinf2 = line.split(",");
if (userinf2[0].equals(userinf[0])) {//判斷條件根據(jù)自己的要求修改
buf.append(line);
int indexOf = line.indexOf(oldString);
buf.replace(indexOf, indexOf + oldString.length(), newString);// 修改內(nèi)容
buf.append(System.getProperty("line.separator"));// 添加換行
bufAll.append(buf);
} else {
buf.append(line);
buf.append(System.getProperty("line.separator"));
bufAll.append(buf);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
br = null;
}
}
}
return bufAll.toString();
}
// 寫(xiě)文件
public boolean writeFile(String filePath, String content) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
return true;
}
}
管理員操作界面Admin:
package User;
import Book.AddBook;
import Book.DeleteBook;
import Book.FindBook;
import java.io.*;
import java.util.Scanner;
public class Admin {
public Admin() {
}
public void admin() throws IOException {
File adminBook = new File("書(shū)籍.txt");
File adminUser = new File("用戶信息.txt");
FindBook fb = new FindBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("===============圖書(shū)管理員操作界面===============");
System.out.println("1.查詢書(shū)籍 2.添加書(shū)籍 3.刪除書(shū)籍 4.增/刪用戶 5.查看日志 6.返回");
int choice = scanner.nextInt();
if (choice < 1 || choice > 6) {
System.out.println("到底能不能行?能不能看明白了再輸!");
} else if (choice == 6) {
return;
} else if (choice == 1) {
//查詢書(shū)籍
fb.adminFindBook();
} else if (choice == 2) {
//添加書(shū)籍
AddBook ab = new AddBook();
ab.addBook();
} else if (choice == 3) {
//刪除書(shū)籍
DeleteBook db = new DeleteBook();
db.deleteBook();
} else if (choice == 4) {
//增刪用戶
System.out.println("==========增/刪用戶==========");
System.out.println("1.增加用戶 2.刪除用戶 3.返回");
int decision = scanner.nextInt();
if (decision == 3) {
//返回
break;
} else if (decision == 1) {
//增加用戶
AddUser au = new AddUser();
au.addUser();
} else if (decision == 2) {
//刪除用戶
DeleteUser du = new DeleteUser();
du.deleteUser();
}
} else if (choice == 5) {
//查看日志
File file = new File("日志.txt");
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString = null;
System.out.println("-----------------用戶操作日志-----------------");
while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
System.out.println("--------------------------------------------");
}
while (true) {
System.out.println("查看完畢,是否清空日志?");
System.out.println("1.是 2.否");
int choice1 = scanner.nextInt();
if (choice1 == 1) {
FileWriter fw = new FileWriter(file, false);
fw.write("");
fw.close();
break;
} else if (choice1 == 2) {
break;
} else if (choice1 != 1 && choice1 != 2) {
System.out.println("請(qǐng)認(rèn)真核對(duì)后再輸入!");
}
}
}
}
}
}
增加用戶AddUser:
package User;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AddUser {
public void addUser() throws IOException {
Scanner sc = new Scanner(System.in);
File file = new File("用戶信息.txt");
System.out.println("請(qǐng)輸入添加的用戶名:");
String addName = sc.next();
//查找是否有同名用戶
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
String[] userinf = new String[5];
boolean flag = false;
for (String user : list) {
userinf = user.split(",");
if (addName.equals(userinf[0])) { //判斷用戶名是否存在
flag = true;
System.out.println("該用戶名已存在,請(qǐng)讓其重新擬取名稱");
break;
}
}
if (flag == false) {
System.out.println("請(qǐng)輸入密碼:");
String password = sc.next();
String sex;
while (true) {
System.out.println("請(qǐng)輸入用戶的性別:");
sex = sc.next();
if (sex.equals("男") || sex.equals("女")) {
break;
} else {
System.out.println("請(qǐng)輸入正確的性別!");
}
}
System.out.println("請(qǐng)輸入用戶的年齡:");
int age = sc.nextInt();
if (age < 10) {
System.out.println("該用戶的年齡太小啦!請(qǐng)讓家長(zhǎng)使用賬號(hào)幫他(她)借閱書(shū)籍!");
return;
}
long phone;
while (true) {
System.out.println("請(qǐng)輸入該用戶的電話號(hào)碼:");
phone = sc.nextInt();
if (phone < 0) {
System.out.println("請(qǐng)輸入正確的電話號(hào)碼!");
} else {
break;
}
}
User u = new User(addName, sex, age, phone, password);
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
fw.write(u.toString());
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
System.out.println("成功添加用戶:" + addName);
return;
}
}
}
刪除用戶DeleteUser:
package User;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class DeleteUser {
private String userDelete;
String[] userinf = new String[5];
String[] userinf1 = new String[5];
String[] userinf2 = new String[5];
public void deleteUser() throws IOException {
File file = new File("用戶信息.txt");
FileInputStream intput = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(intput));
Scanner scanner = new Scanner(System.in);
String tempString;
List<String> list = new ArrayList<>();
while ((tempString = reader.readLine()) != null) {
list.add(tempString);
}
System.out.println("==========當(dāng)前用戶有==========");
for (String user : list) {
userinf = user.split(",");
System.out.print(userinf[0] + " ");
}
System.out.println("");
System.out.println("請(qǐng)輸入要?jiǎng)h除信息的用戶名:");
userDelete = scanner.next();
//查詢?cè)撚脩羰欠裼形礆w還的書(shū)籍,如果存在未歸還的書(shū)籍,將不能刪除該用戶的信息
boolean have = false;
String[] bookinf = new String[8];
File bookFile = new File("書(shū)籍.txt");
FileInputStream inputStream = new FileInputStream(bookFile);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String bookString;
List<String> bookList = new ArrayList<>();
while ((bookString = bufferedReader.readLine()) != null) {
bookList.add(bookString);
}
for (String borrowUser : bookList) {
bookinf = borrowUser.split(",");
if (userDelete.equals(bookinf[6])) {
have = true;
System.out.println("該用戶還有書(shū)籍尚未歸還,請(qǐng)?jiān)谄錃w還書(shū)籍后再進(jìn)行刪除!");
return;
}
}
if (have == false) {
boolean flag = false;
for (String user : list) {
userinf1 = user.split(",");
if (userDelete.equals(userinf1[0])) { //判斷用戶名是否存在
flag = true;
list.remove(user);
FileWriter fd = new FileWriter(file, false);
fd.write("");
fd.close();
break;
}
}
if (flag == true) {
for (String user : list) {
userinf2 = user.split(",");
FileWriter fw = new FileWriter(file, true);
fw.write(userinf2[0] + "," + userinf2[1] + "," + userinf2[2] + "," + userinf2[3] + "," + userinf2[4]);
fw.write(System.getProperty("line.separator"));//在段落后添加一個(gè)換行符
fw.close();
// return;
}
System.out.println("刪除成功!");
} else {
System.out.println("無(wú)法找到該用戶,請(qǐng)重新確認(rèn)后再進(jìn)行操作!");
return;
}
}
}
public static void main(String[] args) throws IOException {
DeleteUser du = new DeleteUser();
du.deleteUser();
}
}
最后是運(yùn)行的主函數(shù):
public static void main(String[] args) throws IOException, ParseException {
Library library = new Library();
library.Interface();
}
整個(gè)項(xiàng)目完成之后的包和類(lèi)如下所示:
書(shū)籍和用戶信息中之前添加過(guò)的信息如下:
書(shū)籍信息:
用戶信息:
五、總結(jié)
該項(xiàng)目是我學(xué)Java以來(lái)自己制作的功能最為復(fù)雜,設(shè)計(jì)內(nèi)容最豐富的項(xiàng)目,同時(shí)也是耗費(fèi)我時(shí)間最久的項(xiàng)目,項(xiàng)目中還有很多地方的代碼是可以繼續(xù)優(yōu)化的,而且還有很多可以增加用戶體驗(yàn)的功能是可以再添加的。
整個(gè)項(xiàng)目的制作過(guò)程也是我查缺補(bǔ)漏的一個(gè)過(guò)程,將我之前學(xué)習(xí)的比較薄弱的部分給進(jìn)行了增強(qiáng),同時(shí)也將一些之前學(xué)的扎實(shí)的部分進(jìn)行了加固。
到這里,本篇關(guān)于Java的實(shí)戰(zhàn)練習(xí),基于IO流設(shè)計(jì)的圖書(shū)館管理系統(tǒng)的文章就介紹結(jié)束了,想要了解更多相關(guān)Java有趣好玩的項(xiàng)目實(shí)戰(zhàn)內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,同時(shí)也希望本篇文章能夠幫助大家對(duì)Java語(yǔ)言的學(xué)習(xí)!