-- 圖書借閱數(shù)據(jù)庫設(shè)計(jì)
為北京等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及北京網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、北京網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
-- 記錄每一本圖書借出和還回的操作
-- 書的信息
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;
-- 新建相關(guān)的表
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本書
-- 存儲(chǔ)過程
DELIMITER $$$
CREATE PROCEDURE add_books( IN n INT)
BEGIN
-- 需要有多行數(shù)據(jù)放入變量,需要用 游標(biāo) 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 ;
-- 觸發(fā)器 trigger ,管理圖書被借出后,藏書和讀者信息的維護(hù)
-- 表中數(shù)據(jù)相關(guān)的操作: 增 刪 改 (查)
-- 一個(gè)表中某條記錄被修改: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 = "讀者已經(jīng)到達(dá)最大借閱數(shù)!";
END IF ;
END $$$
DELIMITER ;
-- 書借出的數(shù)據(jù)維護(hù)
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 ;
-- 書歸還的數(shù)據(jù)維護(hù)
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;
-- 計(jì)算超期費(fèi)用
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 ;
-- 添加若干的測(cè)試數(shù)據(jù)
INSERT INTO books (name, price)
VALUES
("javascript程序設(shè)計(jì)", 19.8),
("數(shù)據(jù)庫系統(tǒng)開發(fā)", 20.8),
("mysql數(shù)據(jù)庫必知必會(huì)", 28.8);
INSERT INTO readers ( name )
VALUES
("Alice"),
("Bob"),
("Carl");
-- 測(cè)試語句
SHOW TABLES;
-- 根據(jù)書的信息添加10本館藏圖書
CALL add_books( 10) ;
SELECT * from lib_books;
-- 借書
-- 2號(hào)讀者借了2號(hào)書
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;
-- 續(xù)借
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("還書成功");
}
}
分享標(biāo)題:java圖書借還代碼 java圖書館借閱系統(tǒng)代碼
新聞來源:http://jinyejixie.com/article26/dophejg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站營(yíng)銷、響應(yīng)式網(wǎng)站、軟件開發(fā)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)