簡單圖書管理系統(含mysql)
阿新 • • 發佈:2019-01-29
建立一個圖書管理系統可以滿足的需求:
增加讀者資訊,修改讀者資訊,登出讀者;
滿足圖書基本的增、刪、改、查;
使用資料庫,達到持久化操作。
import java.util.Date; /** * @author 向超 * @date 2018年1月3日 下午8:50:08 * @Description:讀者類 */ public class Reader { private boolean vip; private int id; private String name; private boolean gender; private String tel; private Date registerDate; private boolean available; private int borrows; public boolean isVip() { return vip; } public void setVip(boolean vip) { this.vip = vip; } public int getBorrows() { return borrows; } public void setBorrows(int borrows) { this.borrows = borrows; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public Date getRegisterDate() { return registerDate; } public void setRegisterDate(Date registerDate) { this.registerDate = registerDate; } public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; } }
import java.util.Date; /** * @author 向超 * @date 2018年1月3日 下午8:42:48 * @Description:圖書類 */ public class Book { private int id; private String isbn; private String name; private double price; private String author; private String publisher; private Date pubDate; private boolean lended; private int counter; private String type; public int getId() { return id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public void setId(int id) { this.id = id; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public Date getPubDate() { return pubDate; } public void setPubDate(Date pubDate) { this.pubDate = pubDate; } public boolean isLended() { return lended; } public void setLended(boolean lended) { this.lended = lended; } public int getCounter() { return counter; } public void setCounter(int counter) { this.counter = counter; } }
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:// localhost:3306/booksys_new?useSSL=false&useUnicode=true&characterEncoding=utf8
username=root
password=123456
package com.qfedu; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; import javax.management.RuntimeErrorException; /** * @author 向超 * @date 2018年1月3日 下午7:26:31 * @Description:自定義JDBC操作工具類 */ public class JdbcUtil { /** * 利用靜態方法 */ private static Properties props = new Properties(); static { try (InputStream inputStream = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties")) { props.load(inputStream); Class.forName(props.getProperty("driver")); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } /** * 私有化建構函式 */ private JdbcUtil() { throw new AssertionError(); } /** * 獲得資料庫連線 * * @return Connection物件 */ public static Connection getConnection() { try { return DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password")); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 關閉資料庫連線 * * @param connection * 連線物件 */ public static void closeConnection(Connection connection) { try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * 開啟事務 * * @param connection * 連線物件 */ public static void beginTx(Connection connection) { try { connection.setAutoCommit(false); } catch (Exception e) { throw new RuntimeException(e); } } /** * 提交事務 * * @param connection * 連線物件 */ public static void commitTx(Connection connection) { try { connection.commit(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 回滾事務 * * @param connection * 連線物件 */ public static void rollbackTx(Connection connection) { try { connection.rollback(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 執行增刪改操作 * @param con 連線物件 * @param sql SQL語句 * @param params 替換佔位符的引數 * @return 受影響的行數 * @throws SQLException */ public static int executeUpdate(Connection con, String sql, Object... params) { try (PreparedStatement stmt = con.prepareStatement(sql)) {//傳入從con和sql,建立預編譯SQL,減少sql執行 for (int i = 0; i < params.length; i++) {//通過for迴圈給每個問號賦值 stmt.setObject(i + 1, params[i]); } return stmt.executeUpdate();//返回修改的行數,int值 } catch (SQLException e) { throw new RuntimeException(e); } } }
import java.sql.Connection;
import java.util.Date;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author 向超
* @date 2018年1月3日 下午8:44:07
* @Description:讀者管理
*/
public class ReaderManager {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 新增一個讀者
*
* @param reader
* 讀者
* @return 新增成功返回true 否則返回false
*/
public boolean createNewReader(Reader reader) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection,
"insert into tb_reader (vip,readerid, rname, gender, tel, regdate) values (?,?,?,?,?,?)",
reader.isVip(), reader.getId(), reader.getName(), reader.isGender(), reader.getTel(),
new Date(System.currentTimeMillis())) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 登出一個讀者
*
* @param id
* 讀者編號
* @return 登出成功返回true 否則返回false
*/
public int cancelReaderById(int id) {
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection
.prepareStatement("select available,borrows from tb_reader where readerid=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
boolean flag = rs.next();
if (flag && rs.getInt("borrows") == 0 && rs.getBoolean("available")) {
return JdbcUtil.executeUpdate(connection, "update tb_reader set available=0 where readerid=?", id);
} else if (flag && rs.getInt("borrows") != 0 && rs.getBoolean("available")) {
return 0;
} else {
return -1;
}
} catch (SQLException e) {
e.printStackTrace();
}
return -2;
}
/**
* 編輯讀者資訊
*
* @param reader
* 讀者
* @return 編輯成功返回true 否則返回false
*/
public boolean editReaderInfo(Reader reader) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection, "update tb_reader set tel=? where readerid=?", reader.getId(),
reader.getTel()) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.imageio.event.IIOReadWarningListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
/**
* @author 向超
* @date 2018年1月3日 下午8:43:26
* @Description:圖書管理
*/
public class BookManager {
/**
* 新增圖書
*
* @param book
* 圖書物件
* @return 新增成功返回true否則返回false
*/
public boolean addNewBook(Book book) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection,
"insert into tb_book(bookid,isbn,bname,price,author,publisher,pubdate,type) values(?,?,?,?,?,?,?,?)",
book.getId(), book.getIsbn(), book.getName(), book.getPrice(), book.getAuthor(),
book.getPublisher(), book.getPubDate(), book.getType()) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 根據圖書id凍結圖書(不可借閱)
*
* @param id
* 圖書id
* @return 凍結圖書返回true,否則返回false
*/
public boolean removeBookById(int id) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection, "update tb_book set lended=1 where bookid=?and lended=0",
id) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 借閱圖書
*
* @param bookId
* 圖書id
* @param readerId
* 讀者id
* @return 借閱成功返回 1; 因為不是會員且達借閱最大值而借閱失敗返回 2; 因為是會員且達借閱最大值而借閱失敗返回 3; 其他原因返回
* 0;
*/
private int VIP = 30;// vip最大借閱量
private int VIP0 = 15;// 非vip最大借閱量
public int lendOut(int bookId, int readerId) {
Connection connection = JdbcUtil.getConnection();
try {
PreparedStatement stmt = connection
.prepareStatement("select available,vip,borrows from tb_reader where readerid=?");
stmt.setInt(1, readerId);
ResultSet rs = stmt.executeQuery();
boolean flag = rs.next();
boolean vip01 = false;
boolean vip02 = false;
boolean vip11 = false;
boolean vip12 = false;
if (flag) {
vip01 = !rs.getBoolean("vip") && rs.getInt("borrows") < 15;// 不是會員,借閱未達上限
vip02 = !rs.getBoolean("vip") && rs.getInt("borrows") >= 15;// 不是會員,借閱已達上限
vip11 = rs.getBoolean("vip") && rs.getInt("borrows") < 30;// 是會員,借閱未達上限
vip12 = rs.getBoolean("vip") && rs.getInt("borrows") >= 30;// 是會員,借閱已達上限
}
JdbcUtil.beginTx(connection);
if ((flag && rs.getBoolean("available")) && ((vip11) || (vip01))) {
JdbcUtil.beginTx(connection);
if (kejie(bookId, readerId, connection)) {
int affectedRows = JdbcUtil.executeUpdate(connection,
"insert into tb_record (bid,rid,lenddate)values(?,?,?)", bookId, readerId,
new Date(System.currentTimeMillis()));
JdbcUtil.commitTx(connection);// 手動提交
return 1;
}
} else if ((flag && rs.getBoolean("available")) && ((vip11) || (vip02))) {
JdbcUtil.beginTx(connection);
if (kejie(bookId, readerId, connection)) {
return 2;
}
} else if ((flag && rs.getBoolean("available")) && ((vip01) || (vip12))) {
JdbcUtil.beginTx(connection);
if (kejie(bookId, readerId, connection)) {
return 3;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
JdbcUtil.rollbackTx(connection);
} finally {
JdbcUtil.closeConnection(connection);
}
return 0;
}
/**
* 判斷借閱修改是否成功
*
* @param bookId
* 圖書id
* @param readerId
* 讀者id
* @param connection
* 已建好的連線
* @return 成功修改返回true,否則返回false
*/
private boolean kejie(int bookId, int readerId, Connection connection) {
return (JdbcUtil.executeUpdate(connection,
"update tb_book set lended=1,counter=counter+1 where lended=0 and bookId=?", bookId) == 1)
&& (JdbcUtil.executeUpdate(connection, "update tb_reader set borrows=borrows+1 where readerId=?",
readerId) == 1);
}
/**
* 歸還圖書
*
* @param bookId
* 圖書編號
* @param readerId
* 讀者編號
* @return 如果超期則返回罰款金額 否則返回0 操作失敗返回-1
*/
public double returnBack(int bookId, int readerId) {
double pulishment = 0;
Connection connection = JdbcUtil.getConnection();
try {
JdbcUtil.beginTx(connection);
if (JdbcUtil.executeUpdate(connection, "update tb_book set lended=0 where lended=1 and bookId=?",
bookId) == 1) {
PreparedStatement stmt = connection
.prepareStatement("select a.recordid,a.lenddate,b.vip from tb_record as a,tb_reader "
+ "as b where a.lenddate =(select max(lenddate) from tb_record where bid=? and rid=?) "
+ "and a.bid=? and a.rid=? and b.readerid=?");
stmt.setInt(1, bookId);
stmt.setInt(2, readerId);
stmt.setInt(3, bookId);
stmt.setInt(4, readerId);
stmt.setInt(5, readerId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int recordId = rs.getInt("recordId");
Date lenddate = rs.getDate("lenddate");
boolean vip = rs.getBoolean("vip");
Date backdate = new Date();
Double days = Math.ceil((backdate.getTime() - lenddate.getTime()) / 86400000.0);
if (vip) {
pulishment = days > 60 ? Math.round((days - 60) * 0.2 * 100) / 100.0 : 0;
} else {
pulishment = days > 30 ? Math.round((days - 30) * 0.2 * 100) / 100.0 : 0;
}
JdbcUtil.executeUpdate(connection, "update tb_record set backdate=?,pulishment=? where recordId=?",
new Date(), pulishment, rs.getInt("recordid"));
}
}
JdbcUtil.commitTx(connection);
return pulishment;
} catch (SQLException e) {
e.printStackTrace();
JdbcUtil.rollbackTx(connection);
} finally {
JdbcUtil.closeConnection(connection);
}
return -1;
}
/**
* 根據圖書id和圖書型別進行查詢
*
* @param id
* 圖書id
* @param type
* 圖書型別
* @return 返回查詢結果的Book物件,否則返回null
*/
public Book searchBookById(int id, String type) {
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection.prepareStatement(
"select type,bookid,isbn,bname,price,author,publisher,pubdate,lended,counter from tb_book where type=? and bookid=?");
stmt.setString(1, type);
stmt.setInt(2, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Book book = handleResultSet(rs);
return book;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 根據isbn和圖書型別進行查詢
*
* @param isbn
* 圖書的國際標準書號
* @param type
* 圖書型別
* @return 返回儲存查詢結果的列表容器
*/
public List<Book> searchBookByIsbn(String isbn, String type) {
List<Book> bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection.prepareStatement(
"select bookid,isbn,bname,price,author,publisher,pubdate,lended,counter,type from tb_book where isbn=? and type=?");
stmt.setString(1, isbn);
stmt.setString(2, type);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Book book = handleResultSet(rs);
bookList.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookList.size() > 0 ? bookList : Collections.emptyList();
}
/**
* 根據圖書名稱和圖書的型別進行查詢
*
* @param name
* 圖書名稱
* @param page
* 需要檢視的頁數
* @param type
* 圖書的型別
* @return 返回儲存查詢結果的列表容器
*/
private int size = 2;
public List<Book> searchBookByName(String name, int page, String type) {
List<Book> bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection.prepareStatement(
"select type,bookid,isbn,bname,price,author,publisher,pubdate,lended,counter from "
+ "(select type,bookid,isbn,bname,price,author,publisher,pubdate,lended,counter "
+ "from tb_book where type=? and bname like ? limit ?,?)as tb order by counter desc");
stmt.setString(1, type);
stmt.setString(2, "%" + name + "%");
stmt.setInt(3, (page - 1) * size);
stmt.setInt(4, size);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Book book = handleResultSet(rs);
bookList.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookList.size() > 0 ? bookList : Collections.emptyList();
}
/**
* 根據圖書的作者和圖書型別查詢
*
* @param author
* 圖書作者
* @param type
* 圖書型別
* @return 返回儲存查詢結果的列表容器
*/
public List<Book> searchBookByAuthor(String author, String type) {
List<Book> bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection
.prepareStatement("select type,bookid,isbn,bname,price,author,publisher,pubdate,lended,counter "
+ "from tb_book where author like ? and type=?");
stmt.setString(1, type);
stmt.setString(1, "%" + author + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Book book = handleResultSet(rs);
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bookList.size() > 0 ? bookList : Collections.emptyList();
}
/**
* 查詢圖書借閱量的前10名(Top10)
*
* @return 返回儲存查詢結果的列表容器
*/
public List<Book> searchTop10Books() {
List<Book> bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection
.prepareStatement("select bookid,isbn,bname,price,author,publisher,pubdate,lended, "
+ "sum(counter) as counter from tb_book group by isbn order by counter desc limit 10");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Book book = handleResultSet(rs);
bookList.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookList.size() > 0 ? bookList : Collections.emptyList();
}
/**
* Book物件賦值
*
* @param rs
* @return 返回一個Book物件
* @throws SQLException
*/
private Book handleResultSet(ResultSet rs) throws SQLException {
Book book = new Book();
book.setId(rs.getInt("bookid"));
book.setIsbn(rs.getString("isbn"));
book.setName(rs.getString("bname"));
book.setPrice(rs.getDouble("price"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPubDate(rs.getDate("pubdate"));
book.setLended(rs.getBoolean("lended"));
book.setCounter(rs.getInt("counter"));
book.setType(rs.getString("type"));
return book;
}
}
import java.lang.ProcessBuilder.Redirect;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
/**
* @author 向超
* @date 2018年1月3日 下午8:43:05
* @Description:圖書管理系統
*/
public class BookMIS {
private static BookManager bookManager = new BookManager();
private static ReaderManager readerManager = new ReaderManager();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
boolean isRunning = true;
System.out.println("===歡迎使用ABC圖書管理系統===");
while (isRunning) {
int choice;
System.out.println("1. 管理讀者");
System.out.println("2. 管理圖書");
System.out.println("3. 退出系統");
do {
System.out.print("請選擇: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 3);
switch (choice) {
case 1:
showReaderSubMenu();
break;
case 2:
showBookSubMenu();
break;
case 3:
System.out.println("感謝使用本系統, 歡迎下次再來");
isRunning = false;
break;
}
}
scanner.close();
}
private static void showReaderSubMenu() {
System.out.println("======讀者管理子系統======");
boolean flag = true;
int choice;
while (flag) {
System.out.println("1. 新增讀者");
System.out.println("2. 登出讀者");
System.out.println("3. 修改資訊");
System.out.println("4. 返回主選單");
do {
System.out.print("請選擇: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 4);
switch (choice) {
case 1:
addReader();
break;
case 2:
cancelReader();
break;
case 3:
editReader();
break;
case 4:
flag = false;
break;
}
}
}
private static void addReader() {
System.out.println("vip:");
int vip = scanner.nextInt();
System.out.println("編號:");
int id = scanner.nextInt();
System.out.println("姓名:");
String name = scanner.next();
System.out.println("性別(1.男,0.女):");
int gender = scanner.nextInt();
System.out.println("電話:");
String tel = scanner.next();
Reader reader = new Reader();
reader.setVip(vip == 0);
reader.setId(id);
reader.setName(name);
reader.setGender(gender == 1);
reader.setTel(tel);
if (readerManager.createNewReader(reader)) {
System.out.println("新增讀者成功");
} else {
System.out.println("新增讀者失敗");
}
}
private static void cancelReader() {
System.out.println("編號:");
int id = scanner.nextInt();
int num = readerManager.cancelReaderById(id);
if (num > 0) {
System.out.println("登出成功");
} else if (num == 0) {
System.out.println("登出失敗");
System.out.println("請您歸還您所借的所有書籍,方便他人借閱");
} else if (num == -1) {
System.out.println("登出失敗");
System.out.println("您想要登出的使用者已是登出使用者,不需要重複登出");
} else {
System.out.println("登出失敗");
}
}
private static void editReader() {
System.out.println("編號:");
int id = scanner.nextInt();
System.out.println("電話:");
String tel = scanner.next();
Reader reader = new Reader();
reader.setId(id);
reader.setTel(tel);
if (readerManager.editReaderInfo(reader)) {
System.out.println("修改電話成功");
} else {
System.out.println("修改電話失敗");
}
}
private static void showBookSubMenu() throws ParseException {
System.out.println("======圖書管理子系統======");
boolean flag = true;
int choice;
while (flag) {
System.out.println("1. 新增圖書");
System.out.println("2. 下架圖書");
System.out.println("3. 借出圖書");
System.out.println("4. 歸還圖書");
System.out.println("5. 根據id查詢圖書");
System.out.println("6. 根據ibsn查詢圖書");
System.out.println("7. 根據名字查詢圖書");
System.out.println("8. 根據作者查詢圖書");
System.out.println("9. Top10排行榜");
System.out.println("10. 返回主選單");
do {
System.out.print("請選擇: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 10);
switch (choice) {
case 1:
addBook();
break;
case 2:
removeBook();
break;
case 3:
lendBook();
break;
case 4:
returnBook();
break;
case 5:
ListBooksById();
break;
case 6:
ListBooksByIsbn();
break;
case 7:
ListBooksByName();
break;
case 8:
ListBooksByAuthor();
break;
case 9:
listTop10Books();
break;
case 10:
flag = false;
break;
}
}
}
private static void addBook() throws ParseException {
System.out.println("圖書本地編號:");
int id = scanner.nextInt();
System.out.println("圖書國際編號:");
String isbn = scanner.next();
System.out.println("圖書名稱:");
String name = scanner.next();
System.out.println("圖書價格:");
double price = scanner.nextDouble();
System.out.println("圖書作者:");
String author = scanner.next();
System.out.println("圖書出版社:");
String publisher = scanner.next();
System.out.println("圖書出版時間");
String pubdate = scanner.next();
System.out.println("書的型別");
String type = scanner.next();
SimpleDateFormat slp = new SimpleDateFormat("yyyy-MM-dd");
Date date = slp.parse(pubdate);
Book book = new Book();
book.setId(id);
book.setIsbn(isbn);
book.setName(name);
book.setPrice(price);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPubDate(date);
book.setType(type);
if (bookManager.addNewBook(book)) {
System.out.println("新增圖書成功");
} else {
System.out.println("新增圖書失敗");
}
}
private static void removeBook() {
System.out.println("圖書本地編號:");
int id = scanner.nextInt();
if (bookManager.removeBookById(id)) {
System.out.println("刪除圖書成功");
} else {
System.out.println("刪除圖書失敗");
}
}
private static void lendBook() {
System.out.println("圖書本地編號:");
int bookId = scanner.nextInt();
System.out.println("讀者編號:");
int readerId = scanner.nextInt();
int num = bookManager.lendOut(bookId, readerId);
if (num == 1) {
System.out.println("借書成功");
} else if (num == 2) {
System.out.println("借書失敗,你的借閱量為15,已達您的許可權範圍!");
System.out.println("成為VIP讓您的上限增加至30!");
} else if (num == 0) {
System.out.println("借書失敗");
} else {
System.out.println("借書失敗,你的借閱量為30,已達您的許可權範圍!");
System.out.println("請歸還您暫時不需要的書籍,方便您借閱其他書籍!");
}
}
private static void returnBook() {
System.out.println("圖書編號:");
int bookId = scanner.nextInt();
System.out.println("讀者編號:");
int readerId = scanner.nextInt();
Double pulishment = bookManager.returnBack(bookId, readerId);
if (pulishment >= 0) {
System.out.println("還書成功");
if (pulishment > 0) {
System.out.printf("您應付的違約金為:%.2f元\n", pulishment);
}
} else {
System.out.println("還書失敗");
}
}
private static void ListBooksByIsbn() {
System.out.println("Isbn:");
String isbn = scanner.next();
System.out.println("Type:");
String type = scanner.next();
List<Book> list = bookManager.searchBookByIsbn(isbn, type);
/*
* System.out.printf("%30s%20s%10d\n","","","");//格式化輸出,右對齊,%
* 30s表示留出30個位置,s表示要用字元填充
* System.out.printf("%-30s%-20s%-10d\n","","","");//格式化輸出,左對齊,後面的引數對應s,
* s,d System.out.printf("%-30s%-20s%-10d\n","","","");//格式化輸出,左對齊,10d\
* n表示留出10位置,d表示要用數字填充
*/
if (list.size() != 0) {
System.out.println("書名\t\t\t\t作者");
for (Book book : list) {
System.out.print(book.getName() + "\t\t\t");
System.out.println(book.getAuthor());
System.out.println("---------------------------");
}
} else {
System.out.println("對不起,本圖書館暫時沒有這本書,請檢視其它書籍!");
}
}
private static void ListBooksById() {
System.out.println("bookId");
int bookId = scanner.nextInt();
System.out.println("Type:");
String type = scanner.next();
Book book = bookManager.searchBookById(bookId, type);
if (book != null) {
System.out.println("書名\t\t\t作者");
System.out.println(book.getName() + "\t\t\t" + book.getAuthor());
} else {
System.out.println("對不起,本圖書館暫時沒有這本書,請檢視其它書籍!");
}
}
private static void ListBooksByName() {
System.out.println("name:");
String name = scanner.next();
System.out.println("每頁顯示2個,您想要查第幾頁:");
int page = scanner.nextInt();
System.out.println("Type");
String type = scanner.next();
List<Book> list = bookManager.searchBookByName(name, page, type);
if (list.size() != 0) {
System.out.println("書名\t\t\t作者\t\t\t借閱量");
for (Book book : list) {
System.out.println(book.getName() + "\t\t\t" + book.getAuthor() + "\t\t\t" + book.getCounter());
}
} else {
System.out.println("對不起,本圖書館暫時沒有這本書,請檢視其它書籍!");
}
}
private static void ListBooksByAuthor() {
System.out.println("author");
String author = scanner.next();
System.out.println("Type");
String type = scanner.next();
List<Book> list = bookManager.searchBookByAuthor(author, type);
if (list.size() != 0) {
System.out.printf("%-30s%-30s%-30s\n", "書名", "作者", "借閱量");
for (Book book : list) {
System.out.printf("%-30s%-30s%-30s\n", book.getName(), book.getAuthor(), book.getCounter());
}
} else {
System.out.println("對不起,本圖書館暫時沒有這本書,請檢視其它書籍!");
}
}
private static void listTop10Books() {
List<Book> list = bookManager.searchTop10Books();
System.out.printf("%-30s%-30s%-30s\n", "書名", "作者", "借閱量");
for (Book book : list) {
System.out.printf("%-30s%-30s%-30s\n", book.getName(), book.getAuthor(), book.getCounter());
}
}
}