图书类的设计主要是图书的一些基本属性:
1.1 图书类
import java.util.Date;
/**
* 图书
* @author 李海明
*
*/
public class Book {
private int id; //图书编号
private String isbn; //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; // 图书类型
/**
* 下面属性的get和set方法
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
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;
}
}
1.2 读者类
import java.util.Date;
/**
* 读者
* @author 李海明
*
*/
public class Reader {
private int id; //编号
private String name;//名字
private boolean gender;//性别
private String tel;//电话
private Date registerDate;//注册时间
private boolean available;//是否注销
private boolean isVip=false;//是否vip
private int maxLendeddate;//最大借书天数
private int lendednum;//当前借书未还数量
/**
* 读者的get和set方法
*/
public int getLendednum() {
return lendednum;
}
public void setLendednum(int lendednum) {
this.lendednum = lendednum;
}
public boolean isVip() {
return isVip;
}
public void setVip(boolean isVip) {
this.isVip = isVip;
}
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;
}
}
2.1 导入jdbc-jar包
与数据库有关的操作放在这个类中,首先需要导入jdbc的jar包,步骤:1.下载jdbc-jar包 2.在Eclipse上右击项目,build path->add external archive->选择jar包就ok了
2.2 JdbcUtil工具类
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC操作工具类
* @author 李海明
*
*/
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("drv"));
} 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("uid"), props.getProperty("pwd"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭数据库连接
* @param connection 连接对象
*/
public static void closeConnection(Connection connection) {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 开启事务
* @param connection 连接对象
*/
public static void beginTx(Connection connection) {
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 提交事务
* @param connection 连接对象
*/
public static void commitTx(Connection connection) {
try {
connection.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 回滚事务
*/
public static void rollbackTx(Connection connection) {
try {
connection.rollback();
} catch (SQLException 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) throws SQLException {
try (PreparedStatement stmt = con.prepareStatement(sql)) {
for (int i = 0; i < params.length; ++i) {
stmt.setObject(i + 1, params[i]);
}
return stmt.executeUpdate();
}
}
public static ResultSet executeSelect(Connection con, String sql, Object... params) throws SQLException {
try (PreparedStatement stmt = con.prepareStatement(sql)) {
for (int i = 0; i < params.length; ++i) {
stmt.setObject(i + 1, params[i]);
}
return stmt.executeQuery();
}
}
}
将一些操作书本的方法封装在**bookManager**类中,有利于管理和修改。
3.1 bookmanager类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;
import javax.swing.text.StyledEditorKit.ForegroundAction;
import com.mysql.jdbc.Statement;
/**
* 图书管理器
*
* @author 李海明
*
*/
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) values (?,?,?,?,?,?,?)",
book.getId(), book.getIsbn(), book.getName(), book.getPrice(), book.getAuthor(),
book.getPublisher(), book.getPubDate()) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 根据编号移除图书(不可借阅)
* @param 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 图书编号
* @param readerId 读者编号
* @return 借出成功返回true 否则返回false
*/
@SuppressWarnings("resource")
public boolean lendOut(int bookId, int readerId) {
Connection connection = JdbcUtil.getConnection();
try {
PreparedStatement stmt = connection.prepareStatement(
"select available from tb_reader where lendednum maxlendeddate ?
Math.round((days -maxlendeddate) * 0.2 * 100) / 100.0 : 0;
JdbcUtil.executeUpdate(connection,
"update tb_record set backdate=?, pulishment=? where recordid=?",
backDate, pulishment, recordId);
JdbcUtil.executeUpdate(connection, "update tb_reader set lendednum=lendednum-1");//还书成功后,当前借书书减1
}
}
JdbcUtil.commitTx(connection);
return pulishment;
} catch (SQLException e) {
e.printStackTrace();
JdbcUtil.rollbackTx(connection);
} finally {
JdbcUtil.closeConnection(connection);
}
return -1;
}
/**
* 根据图书编号查找图书
* @param id 图书编号
* @return 图书对象或null
*/
@SuppressWarnings("resource")
public Book searchBookById(int id) {
try (Connection connection = JdbcUtil.getConnection()) {
PreparedStatement stmt = connection.prepareStatement(
"select bookid, isbn, bname, price, author, publisher, pubdate, lended, counter from tb_book where bookid=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return handleResultSet(rs);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据ISBN号查找图书
* @param isbn
* @return 保存查询结果的列表容器
*/
public List searchBookByIsbn(String isbn) {
List bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
@SuppressWarnings("resource")
PreparedStatement stmt = connection.prepareStatement(
"select bookid, isbn, bname, price, author, publisher, pubdate, lended, counter from tb_book where isbn=?");
stmt.setString(1, isbn);
@SuppressWarnings("resource")
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();
}
/**
* 根据分类查找图书
*/
public List ground(String type,Object...params){
List sametype=new ArrayList();
try(Connection connection =JdbcUtil.getConnection()){
/*PreparedStatement statement=connection.prepareStatement(
"select bookid, isbn,"
+ " bname, price, author, publisher, pubdate, lended,"
+ " counter from tb_book where type=?)");
statement.setString(1,type);
ResultSet rSet=statement.executeQuery();*/
ResultSet rSet=JdbcUtil.executeSelect(connection,"select bookid, isbn,"
+ " bname, price, author, publisher, pubdate, lended,"
+ " counter from tb_book where type=? ",type);
while(rSet.next()){
Book book=handleResultSet(rSet);
sametype.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sametype.size() > 0 ? sametype : Collections.emptyList();
}
/**
*
* 根据书名查找图书
* @param name 书名(支持模糊查找)
* @return 保存查询结果的列表容器
*/
public List searchBookByName(String name) {
List bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
@SuppressWarnings("resource")
PreparedStatement stmt = connection.prepareStatement(
"select bookid, isbn, bname, price, author, publisher, pubdate, lended, counter from tb_book where bname like ?");
stmt.setString(1, "%" + name + "%");
@SuppressWarnings("resource")
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();
}
/**
* 根据作者查找图书
* @param author 作者
* @return 保存查询结果的列表容器
*/
public List searchBookByAuthor(String author) {
List bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
@SuppressWarnings("resource")
PreparedStatement stmt = connection.prepareStatement(
"select bookid, isbn, bname, price, author, publisher, pubdate, lended, counter from tb_book where author like ?");
stmt.setString(1, "%" + author + "%");
@SuppressWarnings("resource")
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名
* @return 保存查询结果的列表容器
*/
public List searchTop10Books() {
List bookList = new ArrayList<>();
try (Connection connection = JdbcUtil.getConnection()) {
@SuppressWarnings("resource")
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");
@SuppressWarnings("resource")
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();
}
// 处理结果集
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"));
return book;
}
}
3.2 readerManager类
package com.qfedu;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
/**
* @author 李海明
* 为了找个漂亮
又会调bug
的女朋友
请努力学习!
*/
public class ReaderManager {
/**
* 新增一个读者
* @param reader 读者
* @return 新增成功返回true 否则返回false
*/
public boolean createNewReader(Reader reader) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection,
"insert into tb_reader (readerid, rname, gender, tel, regdate) values (?,?,?,?,?)",
reader.getId(), reader.getName(), reader.isGender(), reader.getTel(),
new Date()) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 注销一个读者
* @param id 读者编号
* @return 注销成功返回true 否则返回false
*/
public boolean cancelReaderById(int id) {
try (Connection connection = JdbcUtil.getConnection()) {
return JdbcUtil.executeUpdate(connection,
"update tb_reader set available=0 where readerid=? and lendednum=0", id) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 编辑读者电话
* @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.getTel(), reader.getId()) == 1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 注册会员
*/
public void upToVip(int readerid){
try(Connection connection =JdbcUtil.getConnection()){
if(JdbcUtil .executeUpdate(connection, "update "
+ "tb_reader set isVip=1,maxlendeddate=60,maxlendednum=10 where readerid=?",readerid)==1){
System.out.println("注册成功!!!");
}else{
System.out.println("注册失败!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("注册失败!");
}
}
}
BookMIS
package com.qfedu;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
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) {
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. 返回主菜单");
System.out.println("5. 注册会员");
do {
System.out.print("请选择: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 5);
switch (choice) {
case 1:
addReader();
break;
case 2:
cancelReader();
break;
case 3:
editReader();
break;
case 4:
flag = false;
break;
case 5:
addvip();
break;
}
}
}
private static void addvip(){
System.out.println("请输入要注册会员的读者编号");
int id=scanner.nextInt();
readerManager.upToVip(id);
}
private static void addReader() {
System.out.print("编号: ");
int id = scanner.nextInt();
System.out.print("姓名: ");
String name = scanner.next();
System.out.print("性别(1. 男, 0. 女): ");
int gender = scanner.nextInt();
System.out.print("电话: ");
String tel = scanner.next();
Reader reader = new Reader();
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.print("编号: ");
int id = scanner.nextInt();
if (readerManager.cancelReaderById(id)) {
System.out.println("注销成功!");
} else {
System.out.println("注销失败!");
}
}
private static void editReader() {
System.out.print("编号: ");
int id = scanner.nextInt();
System.out.print("电话: ");
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() {
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. 按编号查找图书");
System.out.println("6. 按书名查找图书");
System.out.println("7. Top10排行榜");
System.out.println("8. 返回主菜单");
System.out.println("9. 按类型查找图书");
do {
System.out.print("请选择: ");
choice = scanner.nextInt();
} while (choice < 1 || choice > 9);
switch (choice) {
case 1:
addBook();
break;
case 2:
removeBook();
break;
case 3:
lendBook();
break;
case 4:
returnBook();
break;
case 5:
getBookById();
break;
case 6:
listBooksByIsbn();
break;
case 7:
listTop10Books();
break;
case 8:
flag = false;
break;
case 9:
listSameType();
break;
}
}
}
private static void addBook() {
System.out.print("编号: ");
int id = scanner.nextInt();
System.out.print("ISBN: ");
String isbn = scanner.next();
System.out.print("书名: ");
String name = scanner.next();
System.out.print("价格: ");
double price = scanner.nextDouble();
System.out.print("作者: ");
String author = scanner.next();
System.out.print("出版社: ");
String publisher = scanner.next();
System.out.print("出版日期: ");
Date pubDate = CommonUtil.stringToDate(scanner.next());
Book book = new Book();
book.setId(id);
book.setIsbn(isbn);
book.setName(name);
book.setPrice(price);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPubDate(pubDate);
if (bookManager.addNewBook(book)) {
System.out.println("新增图书成功!");
} else {
System.out.println("新增图书失败!");
}
}
private static void removeBook() {
System.out.print("编号: ");
int id = scanner.nextInt();
if (bookManager.removeBookById(id)) {
System.out.println("下架成功!");
} else {
System.out.println("下架失败!");
}
}
private static void lendBook() {
System.out.print("图书编号: ");
int bookId = scanner.nextInt();
System.out.print("读者编号: ");
int readerId = scanner.nextInt();
if (bookManager.lendOut(bookId, readerId)) {
System.out.println("借书成功!!!");
} else {
System.out.println("借书失败!!!");
}
}
private static void returnBook() {
System.out.print("图书编号: ");
int bookId = scanner.nextInt();
System.out.print("读者编号: ");
int readerId = scanner.nextInt();
double pulishment = bookManager.returnBack(bookId, readerId);
if (pulishment >= 0) {
System.out.println("还书成功!!!");
if (pulishment > 0) {
System.out.printf("请缴纳罚款: %.1f元\n", pulishment);
}
} else {
System.out.println("还书失败!!!");
}
}
private static void getBookById() {
System.out.print("编号: ");
int id = scanner.nextInt();
Book book = bookManager.searchBookById(id);
displayTitle();
displayBook(book);
}
private static void listBooksByIsbn() {
System.out.print("书名: ");
String name = scanner.next();
List list = bookManager.searchBookByName(name);
displayBookList(list);
}
private static void listSameType(){
System.out.println("请输入你要查找的图书类型");
String type =scanner.next();
List list=bookManager.ground(type);
displayBookList(list);
}
private static void listTop10Books() {
List list = bookManager.searchTop10Books();
displayBookList(list);
}
private static void displayBookList(List list) {
displayTitle();
for (Book book : list) {
displayBook(book);
}
}
private static void displayTitle() {
System.out.printf("%-50s%-20s%-10s\n", "书名", "作者", "借阅量");
}
private static void displayBook(Book book) {
System.out.printf("%-50s%-20s%-10d\n", book.getName(),
book.getAuthor(), book.getCounter());
}
}