-- 圖書借閱數據庫設計
為北京等地區用戶提供了全套網頁設計制作服務,及北京網站建設行業解決方案。主營業務為成都網站制作、成都網站設計、北京網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
-- 記錄每一本圖書借出和還回的操作
-- 書的信息
DROP DATABASE IF EXISTS db_lib;
CREATE DATABASE db_lib;
use db_lib
SET @READER_MAX = 10;
SET @EVERY_DAY_FEE=0.1;
SELECT 10 INTO @temp;
-- 新建相關的表
CREATE TABLE books (
id INT PRIMARY KEY AUTO_INCREMENT,
name CHAR(255) NOT NULL,
price DECIMAL(8,2) DEFAULT 0
);
CREATE TABLE readers (
id INT PRIMARY KEY AUTO_INCREMENT,
name CHAR(255) NOT NULL,
borrowed INT DEFAULT 0
);
CREATE TABLE lib_books (
id INT PRIMARY KEY AUTO_INCREMENT,
info INT,
FOREIGN KEY(info) REFERENCES books(id),
stat CHAR(10) NOT NULL DEFAULT "在館"
);
-- 記錄借書和還書
CREATE TABLE lib_loan (
id INT AUTO_INCREMENT,
who INT,
which INT,
check_out DATETIME NOT NULL DEFAULT NOW(),
check_in DATETIME DEFAULT NULL,
renew BOOLEAN NOT NULL DEFAULT 0,
latest DATE NOT NULL,
fee DECIMAL(8,2) DEFAULT NULL,
PRIMARY KEY(id),
FOREIGN KEY(who) REFERENCES readers(id),
FOREIGN KEY(which) REFERENCES lib_books(id)
);
-- 給館藏圖書添加10本書
-- 存儲過程
DELIMITER $$$
CREATE PROCEDURE add_books( IN n INT)
BEGIN
-- 需要有多行數據放入變量,需要用 游標 cursor
-- i 局部變量
DECLARE i INT DEFAULT 1;
-- @temp全局變量
SELECT COUNT(*) INTO @temp
FROM books;
WHILE i=n DO
INSERT INTO lib_books (info) VALUES ( FLOOR(RAND()*@temp)+1 );
SET i = i+1;
END WHILE;
END $$$
DELIMITER ;
-- 觸發器 trigger ,管理圖書被借出后,藏書和讀者信息的維護
-- 表中數據相關的操作: 增 刪 改 (查)
-- 一個表中某條記錄被修改:OLD 代表原有的那條記錄 NEW代表新的那條記錄
-- [BEFORE | AFTER] [INSERT | UPDATE | (DELETE) ]
DELIMITER $$$
CREATE TRIGGER before_insert_loan
BEFORE INSERT ON lib_loan
FOR EACH ROW
BEGIN
-- OLD NEW 已知
DECLARE v1 CHAR(100);
DECLARE v2 INT;
SET NEW.latest =DATE_ADD(NEW.check_out,INTERVAL 1 month);
-- 只能修改NEW
SELECT stat INTO v1
FROM lib_books
WHERE id=NEW.which;
SELECT borrowed INTO v2
FROM readers
WHERE id=NEW.who;
IF ( v1"在館" ) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "圖書已借出!";
END IF ;
IF (v2=@READER_MAX) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "讀者已經到達最大借閱數!";
END IF ;
END $$$
DELIMITER ;
-- 書借出的數據維護
DELIMITER $$$
CREATE TRIGGER after_insert_loan
AFTER INSERT ON lib_loan
FOR EACH ROW
BEGIN
UPDATE lib_books
SET stat = "已借出"
WHERE id = NEW.which ;
UPDATE readers
SET borrowed = borrowed + 1
WHERE id = NEW.who;
END $$$
DELIMITER ;
-- 書歸還的數據維護
DELIMITER $$$
CREATE TRIGGER after_update_loan
AFTER UPDATE ON lib_loan
FOR EACH ROW
BEGIN
IF(OLD.check_in is NULL AND NEW.check_in is NOT NULL) THEN
UPDATE lib_books
SET stat="在館"
WHERE id= NEW.which;
UPDATE readers
SET borrowed=borrowed -1
WHERE id=OLD.who;
END IF;
END $$$
DELIMITER ;
DELIMITER $$$
CREATE TRIGGER before_update_loan
BEFORE UPDATE ON lib_loan
FOR EACH ROW
BEGIN
DECLARE x INT;
IF(OLD.renew=0 AND NEW.renew=1 ) THEN
SET NEW.latest=DATE_ADD(OLD.latest,INTERVAL 1 month);
END IF;
-- 計算超期費用
IF(OLD.check_in is NULL AND NEW.check_in is NOT NULL) THEN
SET x=DATEDIFF(NEW.check_in,NEW.latest);
IF(x=0 )THEN
SET NEW.fee =0 ;
ELSE
SET NEW.fee=@EVERY_DAY_FEE*x;
END IF;
END IF;
END $$$
DELIMITER ;
-- 添加若干的測試數據
INSERT INTO books (name, price)
VALUES
("javascript程序設計", 19.8),
("數據庫系統開發", 20.8),
("mysql數據庫必知必會", 28.8);
INSERT INTO readers ( name )
VALUES
("Alice"),
("Bob"),
("Carl");
-- 測試語句
SHOW TABLES;
-- 根據書的信息添加10本館藏圖書
CALL add_books( 10) ;
SELECT * from lib_books;
-- 借書
-- 2號讀者借了2號書
INSERT INTO lib_loan (who , which ) VALUES (2,2);
INSERT INTO lib_loan (who , which ) VALUES (2,3);
INSERT INTO lib_loan (who , which ) VALUES (2,4);
INSERT INTO lib_loan (who , which ) VALUES (1,2);
INSERT INTO lib_loan (who , which ) VALUES (1,6);
INSERT INTO lib_loan (who , which ) VALUES (2,1);
INSERT INTO lib_loan (who , which ) VALUES (1,1);
-- 還書
UPDATE lib_loan
SET check_in='2022-12-15 12:00:00'
WHERE id = 1;
-- 續借
UPDATE lib_loan
SET renew = 1
WHERE id = 4;
SELECT * FROM lib_books;
SELECT * FROM readers;
SELECT * FROM lib_loan;
switch(key){
case 1:
//借書
break;
case 2:
//還書
break;
}
package?Management;
import?java.util.List;
import?java.util.ArrayList;
public?class?Management?{
public?static?ListBook?bookList=null;
public?Management()?{
//?TODO?Auto-generated?constructor?stub
bookList=new?ArrayListBook(100);
}
public?void?addbook(Book?book)
{
bookList.add(book);
}
public?Book[]?findBook(String?bookName)
{
Book?[]?book=new?Book[100];
int?j=0;
for(int?i=0;ibookList.size();i++)
{
if(bookName.equals(bookList.get(i).getName()))
{
book[j]=bookList.get(i);
j++;
}
}
return?book;
}
public?Book[]?ShowAllBook()
{
Book[]book=new?Book[100];
for(int?i=0;ibookList.size();i++)
{
book[i]=bookList.get(i);
bookList.get(i).printInfo();
}
return?book;
}
}
class?Book
{
private?String?name;
private?String?author;
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getAuthor()?{
return?author;
}
public?void?setAuthor(String?author)?{
this.author?=?author;
}
void?printInfo()
{
System.out.println("書名為"+this.name+"作者為"+this.author);
}
}
class?Reader
{
private?String??name;
public?void?borrowbook(Book?book)?{
Management?liabry=new?Management();
for(int?i=0;iliabry.bookList.size();i++)
{ if(book.getAuthor().equals(liabry.bookList.get(i).getAuthor())book.getName().equals(liabry.bookList.get(i).getName()))
{
liabry.bookList.remove(i);
System.out.println("借書成功!");
break;
}
}
}
public?void?backbook(Book?book)?{
Management?liabry=new?Management();
liabry.addbook(book);
System.out.println("還書成功");
}
}
分享標題:java圖書借還代碼 java圖書館借閱系統代碼
新聞來源:http://m.newbst.com/article26/dophejg.html
成都網站建設公司_創新互聯,為您提供做網站、網站維護、網站營銷、響應式網站、軟件開發、定制開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯