借书还书java代码 java编写借书卡程序代码

java简单的借书系统

HashMap的常用方法

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网络空间、营销软件、网站建设、淄川网站维护、网站推广。

map的存储和修改方式 map.put(key, value);

map是否包含某个元素map.containsKey(key);

map某个key的value值map.get(key);

参考代码

import java.util.HashMap;

public class BookTest {

static HashMapString, Integer map=new HashMapString, Integer();

static{//初始化图书信息

map.put("三国志", 8);

map.put("红楼梦", 0);

map.put("西游记", 15);

}

public static void main(String[] args) {//main方法

jieShu("水浒");

jieShu("三国志");

huanShu("三国志");

jieShu("红楼梦");

huanShu("红楼梦");

jieShu("红楼梦");

}

public static void jieShu(String name){//借书

if(!map.containsKey(name)){//查询图书馆是否有这本书

System.out.println("书籍:"+name+"\t查无此书!");

return;

}

if(map.get(name)=0){//查询图书数量是否为0

System.out.println("书籍:"+name+"\t已全部借出,请明日在来");

return;

}

map.put(name, map.get(name)-1);

System.out.println("书籍:"+name+"\t借书成功。"+"\t库存:"+map.get(name));

}

public static void huanShu(String name){//还书

if(!map.containsKey(name)){

System.out.println("查无此书");

}else{

map.put(name, map.get(name)+1);

System.out.println("书籍:"+name+"\t还书成功。"+"\t库存:"+map.get(name));

}

}

}

测试输出

书籍:水浒 查无此书!

书籍:三国志 借书成功。 库存:7

书籍:三国志 还书成功。 库存:8

书籍:红楼梦 已全部借出,请明日在来

书籍:红楼梦 还书成功。 库存:1

书籍:红楼梦 借书成功。 库存:0

借书还书数据库代码?

-- 图书借阅数据库设计

-- 记录每一本图书借出和还回的操作

-- 书的信息

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;

用java编写一个 图书馆图书借阅管理系统

---------------------------------------------------

给你修改了三个地方:

1.borrowBooks方法中,将System.out.println("你要借吗?"); 改为:

System.out.println("你要借吗?输入1表示借,其他数字表示不借。");

保证输入的时候输入的数字,否则会报出异常。

2.borrowBooks方法中,将self[score] = all[9]; 改为:self[score] = all[i];

如果是all[9],那么就始终是最后一本书籍信息了。

3.have方法中,你是想将所借的书籍信息都打印出来。修改的比较多,下面注释代码是原来的。

void have(Books[] self) {

// for (int i = 0; i 2; i++) {

// self[i].showBookInfo();

// }

for (int i = 0; i 3; i++) {

if(self[i]!=null)

self[i].showBookInfo();

}

}

****************** 附上所有代码:*************************

import java.util.Scanner;

public class TestBook {

public static void main(String[] args) {

Books all[] = new Books[10];

Books self[] = new Books[3];

all[0] = new Books("java", 1, "12345", "tom", 34.0f, "人民出版社");

all[1] = new Books("c", 2, "12346", "tnn", 31.0f, "人民出版社");

all[2] = new Books("c++", 3, "12445", "mm", 35.0f, "人民出版社");

all[3] = new Books("c#", 4, "12365", "tt", 38.0f, "人民出版社");

all[4] = new Books("j2se", 5, "13345", "tosm", 31.1f, "人民出版社");

all[5] = new Books("j2ee", 6, "18345", "ttm", 32.0f, "人民出版社");

all[6] = new Books("jsp", 7, "12335", "cc", 33.0f, "人民出版社");

all[7] = new Books("net", 8, "12341", "bb", 36.0f, "人民出版社");

all[8] = new Books("ip", 9, "12343", "aa", 37.0f, "人民出版社");

all[9] = new Books("tcp", 10, "22345", "jj", 39.0f, "人民出版社");

Readers r = new Readers("xiaoming", 101, "1", 3);

r.searchAllBooks(all);

r.borrowBooks(all, self);

r.have(self);

r.give(all, self);

}

}

class Readers {

Scanner scan = new Scanner(System.in);

String names;

int nums;

String classes;

int grade;

int score = 0;

// Books self[]=new Books[3];

Readers(String n, int u, String c, int g) {

names = n;

nums = u;

classes = c;

grade = g;

}

void searchAllBooks(Books[] all) {// 查书

for (int i = 0; i 10; i++)

all[i].showBookInfo();

// self[score]=all[0];

}

void give(Books[] all, Books[] self) {// 还书

System.out.println("请输入您要还的书的书号");

int n = scan.nextInt();

for (int i = 0; i 10; i++) {

if (n == all[i].num) {

for (int j = 0; j 3; j++) {

if (self[j] == all[i]) {

self[j] = null;

System.out.println("还书成功");

}

}

}

}

}

void have(Books[] self) {

// for (int i = 0; i 2; i++) {

// self[i].showBookInfo();

// }

for (int i = 0; i 3; i++) {

if(self[i]!=null)

self[i].showBookInfo();

}

}

void giveMoney() {

}

void borrowBooks(Books[] all, Books[] self) {

System.out.println("请输入您要查找的书名:");

String n = scan.next();

int i;

for (i = 0; i 10; i++) {

if (n.equals(all[i].name)) {

all[i].showBookInfo();

break;

}

}

//System.out.println("你要借吗?");

System.out.println("你要借吗?输入1表示借,其他数字表示不借。");

int j;

j = scan.nextInt();

if (j == 1) {

System.out.println("借阅成功");

//self[score] = all[9];

self[score] = all[i];

score += 1;

}

if (score 4) {

System.out.println("您还可以借阅" + (3 - score) + "本");

} else {

System.out.println("对不起,一个人只能借3本");

}

}

}

class Books {

String name;

int num;

String ISBN;

String writer;

float price;

String publisher;

Books(String n, int u, String i, String w, float p, String l) {

name = n;

num = u;

ISBN = i;

writer = w;

price = p;

publisher = l;

}

void showBookInfo() {

System.out.println("**************************");

System.out.println("书名:" + name);

System.out.println("索书号:" + num);

System.out.println("ISBN号:" + ISBN);

System.out.println("价格:" + price);

System.out.println("出版社:" + publisher);

System.out.println("**************************");

}

}

----------------------------------------------------

java 简单的借书还书代码怎么写

switch(key){

case 1:

//借书

break;

case 2:

//还书

break;

}


网页题目:借书还书java代码 java编写借书卡程序代码
URL标题:http://ybzwz.com/article/ddojsci.html