简单图书管理系统(含mysql)

建立一个图书管理系统可以满足的需求:

增加读者信息,修改读者信息,注销读者;

满足图书基本的增、删、改、查;

使用数据库,达到持久化操作。

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());
		}
	}
}


评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值