欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)迷你圖書(shū)管理系統(tǒng)案例全程

 更新時(shí)間:2022年12月13日 09:19:05   作者:Java?Fans  
這篇文章主要為大家詳細(xì)介紹了如何利用java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

b872f203ebb24b888cc7e36c41fa60d6.jpeg

?

項(xiàng)目需求

為某圖書(shū)館開(kāi)發(fā)一個(gè)迷你圖書(shū)管理系統(tǒng),實(shí)現(xiàn)圖書(shū)的管理,包括如下功能:

(1)新增圖書(shū)

(2)查看圖書(shū)

(3)刪除圖書(shū)

(4)借出圖書(shū)

(5)歸還圖書(shū)

(6)退出圖書(shū)系統(tǒng)

覆蓋知識(shí)

程序基本概念、數(shù)據(jù)類(lèi)型、流程控制、順序、選擇 、循環(huán)、跳轉(zhuǎn)語(yǔ)句、變量、類(lèi)、方法、數(shù)據(jù)庫(kù)、JDBC等相關(guān)知識(shí)

掌握數(shù)據(jù)庫(kù)、JDBC、三層架構(gòu)等相關(guān)知識(shí)。

掌握Druid連接池、Apache的DBUtils使用 。

開(kāi)發(fā)思路

(1)明確需求

(2)編碼順序

1)、添加需要的jar包到項(xiàng)目中,將lib文件夾中的jar文件通過(guò)鼠標(biāo)右單擊選擇Build Path的方式添加到你設(shè)置的eatJar文件目錄里。

?

2)、創(chuàng)建database.properties文件,用來(lái)配置注冊(cè)驅(qū)動(dòng)和數(shù)據(jù)庫(kù)連接對(duì)象的相關(guān)數(shù)據(jù)。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java221804
username=root
password=huanghuang
initialSize=10
maxActive=30
maxIdle=5
maxWait=3000

3)、添加需要的工具類(lèi)DBUtils類(lèi)

package cn.book.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DBUtils {
	private static DruidDataSource druidDataSource;
	static {
		Properties properties = new Properties();
		try {
			InputStream is = DBUtils.class
					.getResourceAsStream("/database.properties");
			properties.load(is);
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(properties);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static DataSource getDataSource(){
		return druidDataSource;
	}
}

4)、創(chuàng)建數(shù)據(jù)表:book表

CREATE DATABASE IF NOT EXISTS `java221804`;
CREATE TABLE IF NOT EXISTS book(
	`name` VARCHAR(20),
	`state` INT,
	`date` INT,
	`count` INT
);

?

5)、編寫(xiě)book類(lèi),包含get/set方法、有參/無(wú)參構(gòu)造、toString方法等

package cn.book.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Book implements Serializable {
	//定義屬性
	private String name;
	private int state;
	private int date;
	private int count;
	//添加無(wú)參構(gòu)造
	public Book() {
		super();
	}
	//添加有參構(gòu)造
	public Book(String name, int state, int date, int count) {
		super();
		this.name = name;
		this.state = state;
		this.date = date;
		this.count = count;
	}
	//添加get/set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public int getDate() {
		return date;
	}
	public void setDate(int date) {
		this.date = date;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	@Override
	public String toString() {
		return "Book [name=" + name + ", state=" + state + ", date=" + date
				+ ", count=" + count + "]";
	}
}

6)、數(shù)據(jù)訪問(wèn)層DAO層的接口和實(shí)現(xiàn)類(lèi)的增刪改查方法的編寫(xiě)

7)、服務(wù)層Service層的接口和實(shí)現(xiàn)類(lèi)的增刪改查方法的編寫(xiě)

8)、最后完成視圖層View層測(cè)試類(lèi)的編寫(xiě)

開(kāi)發(fā)步驟

1、數(shù)據(jù)初始化?

2、BookDaoImpl類(lèi)中的部分重要方法

創(chuàng)建 QueryRunner 對(duì)象

private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

增加圖書(shū)的方法

	public int insert(Book book) {
		String sql = "insert into book(name,state,date,count) values(?,?,?,?);";
		Object[] args = { book.getName(), book.getState(), book.getDate(),
				book.getCount() };
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

更改圖書(shū)信息的方法

	public int update(Book book) {
		String sql="update book set state=?,date=?,count=? where name=?; ";
		Object[] args={book.getState(),book.getDate(),book.getCount(),book.getName()};
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

查詢(xún)?nèi)繄D書(shū)的方法

	public List<Book> selectAll() {
		String sql = "SELECT * FROM book;";
		try {
			return queryRunner.query(sql, new BeanListHandler<Book>(Book.class) );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

歡迎使用迷你圖書(shū)管理器方法

新增圖書(shū)方法

查看圖書(shū)方法

刪除圖書(shū)方法

借出圖書(shū)方法

歸還圖書(shū)方法

退出圖書(shū)方法

歡迎首界面:

	public static void useBookSystem() {
		// 輸出歡迎菜單
		System.out.println("歡迎使用迷你圖書(shū)管理器");
		System.out.println("-------------------------");
		System.out.println("1.新增圖書(shū)");
		System.out.println("2.查看圖書(shū)");
		System.out.println("3.刪除圖書(shū)");
		System.out.println("4.借出圖書(shū)");
		System.out.println("5.歸還圖書(shū)");
		System.out.println("6.退出圖書(shū)");
		System.out.println("-------------------------");
	}

全部代碼展示

1、Book類(lèi)

package cn.book.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Book implements Serializable {
	//定義屬性
	private String name;
	private int state;
	private int date;
	private int count;
	//添加無(wú)參構(gòu)造
	public Book() {
		super();
	}
	//添加有參構(gòu)造
	public Book(String name, int state, int date, int count) {
		super();
		this.name = name;
		this.state = state;
		this.date = date;
		this.count = count;
	}
	//添加get/set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public int getDate() {
		return date;
	}
	public void setDate(int date) {
		this.date = date;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	@Override
	public String toString() {
		return "Book [name=" + name + ", state=" + state + ", date=" + date
				+ ", count=" + count + "]";
	}
}

2、DBUtils類(lèi)

(使用連接池DruidDataSource、Apache的知識(shí))

package cn.book.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DBUtils {
	private static DruidDataSource druidDataSource;
	static {
		Properties properties = new Properties();
		try {
			InputStream is = DBUtils.class
					.getResourceAsStream("/database.properties");
			properties.load(is);
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(properties);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static DataSource getDataSource(){
		return druidDataSource;
	}
}

3、數(shù)據(jù)訪問(wèn)層的BookDao接口

package cn.book.dao;
import java.util.List;
import cn.book.entity.Book;
public interface BookDao {
	// 增
	public int insert(Book book);
	// 刪
	public int delete(String name);
	// 改
	public int update(Book book);
	// 查一個(gè)
	public Book select(String name);
	// 查全部
	public List<Book> selectAll();
}

4、數(shù)據(jù)訪問(wèn)層的BookDaoImpl類(lèi)

package cn.book.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.book.dao.BookDao;
import cn.book.entity.Book;
import cn.book.utils.DBUtils;
public class BookDaoImpl implements BookDao {
	private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
	@Override
	public int insert(Book book) {
		String sql = "insert into book(name,state,date,count) values(?,?,?,?);";
		Object[] args = { book.getName(), book.getState(), book.getDate(),
				book.getCount() };
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public int delete(String name) {
		String sql="delete from book where name=?;";
		try {
			return queryRunner.update(sql, name);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public int update(Book book) {
		String sql="update book set state=?,date=?,count=? where name=?; ";
		Object[] args={book.getState(),book.getDate(),book.getCount(),book.getName()};
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public Book select(String name) {
		String sql = "select * from book where name=?;";
		try {
			return queryRunner.query(sql,new BeanHandler<Book>(Book.class), name);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Book> selectAll() {
		String sql = "SELECT * FROM book;";
		try {
			return queryRunner.query(sql, new BeanListHandler<Book>(Book.class) );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}

5、服務(wù)層的BookService接口

package cn.book.service;
import java.util.List;
import cn.book.entity.Book;
public interface BookService {
	// 增
	public int insertBook(Book book);
	// 刪
	public int deleteBook(String name);
	// 改
	public int updateBook(Book book);
	// 查一個(gè)
	public Book selectBook(String name);
	// 查全部
	public List<Book> selectAllBooks();
}

6、服務(wù)層的BookServiceImpl類(lèi)

package cn.book.service.impl;
import java.util.List;
import cn.book.dao.impl.BookDaoImpl;
import cn.book.entity.Book;
import cn.book.service.BookService;
public class BookServiceImpl implements BookService {
	BookDaoImpl bookDaoImpl = new BookDaoImpl();
	@Override
	public int insertBook(Book book) {
		if (bookDaoImpl.select(book.getName()) == null) {
			return bookDaoImpl.insert(book);
		}
		return 0;
	}
	@Override
	public int deleteBook(String name) {
		if (bookDaoImpl.select(name) != null) {
			return bookDaoImpl.delete(name);
		}
		return 0;
	}
	@Override
	public int updateBook(Book book) {
		if (bookDaoImpl.select(book.getName()) != null) {
			return bookDaoImpl.update(book);
		}
		return 0;
	}
	@Override
	public Book selectBook(String name) {
		bookDaoImpl.select(name);
		return null;
	}
	@Override
	public List<Book> selectAllBooks() {
		List<Book> listBooks = bookDaoImpl.selectAll();
		return listBooks;
	}
}

7、視圖層BookMgr測(cè)試類(lèi)

package cn.book.view;
import java.util.List;
import java.util.Scanner;
import cn.book.entity.Book;
import cn.book.service.impl.BookServiceImpl;
public class BookMgr {
	// 定義一個(gè)靜態(tài)的Scanner
	static Scanner inputsc = new Scanner(System.in);
	// 是否退出系統(tǒng),false代表退出系統(tǒng)
	static boolean flag = true;
	static int num = -1;
	static BookServiceImpl bookServiceImpl = new BookServiceImpl();
	// 歡迎使用迷你圖書(shū)管理器方法
	public static void useBookSystem() {
		// 輸出歡迎菜單
		System.out.println("歡迎使用迷你圖書(shū)管理器");
		System.out.println("-------------------------");
		System.out.println("1.新增圖書(shū)");
		System.out.println("2.查看圖書(shū)");
		System.out.println("3.刪除圖書(shū)");
		System.out.println("4.借出圖書(shū)");
		System.out.println("5.歸還圖書(shū)");
		System.out.println("6.退出圖書(shū)");
		System.out.println("-------------------------");
	}
	// 新增圖書(shū)方法
	public static void addBook() {
		System.out.println("-->新增圖書(shū)\n");
		System.out.println("請(qǐng)輸入圖書(shū)名稱(chēng):");
		String bookName = inputsc.next();
		Book book = new Book(bookName, 0, 0, 0);
		// 向數(shù)據(jù)庫(kù)添加圖書(shū)
		int num = bookServiceImpl.insertBook(book);
		System.out.println();
		if (num != 0) {
			System.out.println("新增《" + bookName + "》成功!");
		} else {
			System.out.println("新增《" + bookName + "》失敗!");
		}
		System.out.println("*****************************************");
	}
	// 查看圖書(shū)方法
	public static void lookBook() {
		System.out.println("-->查看圖書(shū)\n");
		System.out.println("序號(hào)\t狀態(tài)\t名稱(chēng)\t借出日期\t借出次數(shù)");
		// 獲取數(shù)據(jù)庫(kù)全部圖書(shū)
		List<Book> books = bookServiceImpl.selectAllBooks();
		for (int i = 0; i < books.size(); i++) {
			String BookState = (books.get(i).getState() == 0) ? "可借閱" : "已借出";
			String dateStr = (books.get(i).getDate() == 0) ? "" : (books.get(i)
					.getDate() + "日");
			System.out.println((i + 1) + "\t" + BookState + "\t"
					+ books.get(i).getName() + "\t" + dateStr + "\t"
					+ books.get(i).getCount() + "次");
		}
		System.out.println("*****************************************");
	}
	// 刪除圖書(shū)方法
	public static void delBook() {
		System.out.println("-->刪除圖書(shū)\n");
		System.out.println("請(qǐng)輸入要?jiǎng)h除圖書(shū)的名稱(chēng):");
		String deleteBook = inputsc.next();
		// 從數(shù)據(jù)庫(kù)中查找此圖書(shū)
		Book delBook = bookServiceImpl.selectBook(deleteBook);
		boolean flag3 = false;
		// 刪除的圖書(shū)存在、狀態(tài)處于可借閱狀態(tài)
		if (delBook.getName() != null && deleteBook.equals(delBook.getName())
				&& delBook.getState() == 0) {
			flag3 = true;
			int num = bookServiceImpl.deleteBook(delBook.getName());
			if (num != 0) {
				System.out.println("圖書(shū)刪除成功!");
			} else {
				System.out.println("圖書(shū)刪除失??!");
			}
		} else if (delBook.getName() != null
				&& deleteBook.equals(delBook.getName())
				&& delBook.getState() == 1) {
			flag3 = true;
			System.out.println("該圖書(shū)已被借出,目前無(wú)法刪除!");
		}
		if (!flag3) {
			System.out.println("沒(méi)有找到匹配信息!");
		}
		System.out.println("*****************************************");
	}
	// 借出圖書(shū)方法
	public static void lendBook() {
		System.out.println("-->借出圖書(shū)\n");
		System.out.print("請(qǐng)輸入圖書(shū)名稱(chēng):");
		String want = inputsc.next(); // 要借出的圖書(shū)名稱(chēng)
		// 從數(shù)據(jù)庫(kù)中查找此圖書(shū)
		Book wantBook = bookServiceImpl.selectBook(want);
		if (wantBook == null) {
			System.out.println("沒(méi)有找到匹配信息!");
		} else {
			if (want.equals(wantBook.getName()) && wantBook.getState() == 0) { // 找到匹配可借
				wantBook.setState(1); // 將此圖書(shū)置于借出狀態(tài)
				System.out.print("請(qǐng)輸入借出日期:");
				int inputscData = inputsc.nextInt();
				wantBook.setDate(inputscData);
				while (wantBook.getDate() < 1 || wantBook.getDate() > 31) { // 當(dāng)輸入借出的日期不滿(mǎn)足1-31時(shí)
					System.out.println("必須輸入大于等于1且小于等于31的數(shù)字,請(qǐng)重新輸入:");
					inputscData = inputsc.nextInt();
					wantBook.setDate(inputscData);
				}
				wantBook.setCount(wantBook.getCount() + 1);
				// 更新書(shū)本最新信息
				int num = bookServiceImpl.updateBook(wantBook);
				if (num != 0) {
					System.out.println("借出《" + want + "》成功!");
				} else {
					System.out.println("借出《" + want + "》失??!");
				}
			} else if (want.equals(wantBook.getName())
					&& wantBook.getState() == 1) { // 找到匹配已被借出
				System.out.println("《" + want + "》已被借出!");
			}
		}
		System.out.println("*****************************************");
	}
	// 歸還圖書(shū)方法
	public static void returnBook() {
		System.out.println("-->歸還圖書(shū)\n");
		int charge = 0; // 租金
		System.out.print("請(qǐng)輸入歸還圖書(shū)名稱(chēng):");
		String back = inputsc.next();
		// 從數(shù)據(jù)庫(kù)中查找此圖書(shū)
		Book backBook = bookServiceImpl.selectBook(back);
		if (backBook == null) {
			System.out.println("沒(méi)有找到匹配信息!");
		} else {
			if (back.equals(backBook.getName()) && backBook.getDate() == 1) {// 找到匹配
				backBook.setDate(0); // 將借閱狀態(tài)修改為可借閱
				System.out.print("請(qǐng)輸入歸還日期:");
				int redate = inputsc.nextInt();
				while (redate < backBook.getDate() || redate > 31) { //
					// 歸還日期不能小于借出日期,也不能大于31
					if (redate < backBook.getDate()) {
						System.out.println("歸還日期不能小于借出日期,請(qǐng)重新輸入:");
					} else {
						System.out.println("一個(gè)月只有31天,請(qǐng)重新輸入:");
					}
					redate = inputsc.nextInt();
				}
				charge = redate - backBook.getDate();
				// 更新書(shū)本最新信息
				int num = bookServiceImpl.updateBook(backBook);
				if (num != 0) {
					System.out.println("\n歸還《" + back + "》成功!");
					System.out.println("借出日期為:" + (backBook.getDate() + 1)
							+ "日");
					System.out.println("歸還日期為:" + redate + "日");
					System.out.println("應(yīng)付租金(元):" + charge);
					backBook.setDate(0);
				} else {
					System.out.println("借出《" + back + "》失敗!");
				}
			} else if (back.equals(backBook.getName())
					&& backBook.getState() == 0) {// 找到匹配但沒(méi)有借出
				System.out.println("該圖書(shū)沒(méi)有被借出!無(wú)法進(jìn)行歸還操作。");
			}
		}
		System.out.println("*****************************************");
	}
	// 操作
	public static void runTest() {
		// 循環(huán)操作
		do {
			// 定義一個(gè)BookMethod操作類(lèi),將操作方法定義在BookMethod類(lèi)中,然后調(diào)用
			// 歡迎菜單方法
			BookMgr.useBookSystem();
			System.out.println("請(qǐng)選擇:");
			int choose = inputsc.nextInt();
			switch (choose) {
			case 1:
				// 新增圖書(shū)
				BookMgr.addBook();
				break;
			case 2:
				// 查看圖書(shū)
				BookMgr.lookBook();
				break;
			case 3:
				// 刪除圖書(shū)
				BookMgr.delBook();
				break;
			case 4:
				// 借出圖書(shū)
				BookMgr.lendBook();
				break;
			case 5:
				// 歸還圖書(shū)
				BookMgr.returnBook();
				break;
			case 6:
				// 退出圖書(shū)
				flag = false;
				break;
			default:
				flag = false;
				break;
			}
			if (flag) {
				System.out.println("輸入0返回:");
				num = inputsc.nextInt();
			} else {
				break;
			}
		} while (num == 0);
		System.out.println("謝謝使用!");
	}
	public static void main(String[] args) {
		BookMgr.runTest();
	}
}

到此這篇關(guān)于Java實(shí)現(xiàn)迷你圖書(shū)管理系統(tǒng)案例全程的文章就介紹到這了,更多相關(guān)Java圖書(shū)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論