重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
switch(key){
10年积累的网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有永昌免费网站建设让你可以放心的选择与我们合作。
case 1:
//借书
break;
case 2:
//还书
break;
}
---------------------------------------------------
给你修改了三个地方:
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("**************************");
}
}
----------------------------------------------------
1.先用jdbc连接数据库,这个网上好多你去搜下就有
2.借书还书就是对某张表的更新操作,你借书还书的时候可以得到该书的一个id,这时,你通过这个id从数据库中找到所对应的数据,更改书得库存量即可。
自己动手做一遍,其实不难的,加油!
/
用户登陆数据库代码:
import java.sql.*;
class Database {
Connection con;
ResultSet rs;
Statement stmt;
public Database() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
加载
JDBC-ODBC
桥驱动程序
String url = "jdbcdbc:HDB";
con = DriverManager.getConnection(url);
//
连接数据库
HDB //stmt
提供一个创建
SQL
查询、执行查询、得到返回结果
的空间
stmt =
// con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
// ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
根据用户名,获取密码
*/
public String getPswd(String name) {
try {
// rs
为执行
SQL
语句所查询的结果赋给结果集对象
rs
rs = stmt.executeQuery("SELECT pswd FROM User WHERE Name = '"
+ name + "'");
rs.last();
//
把
rs
的指针移到最后一行
if (rs.getRow() == 0) {
return null;
} else {
String pswdDB = rs.getString("Pswd");
//
获取
rs
结果集中的
pswd
列的数据
return pswdDB;
}
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
增加一行用户名、密码数据
*/
public boolean insertData(String name, String pswd) {
try {
String s = getPswd(name);
if (s == null) {
int rtn = stmt.executeUpdate("INSERT INTO User VALUES('" + name
+ "','" + pswd + "')");
if (rtn != 0)
return true;
} else {
return false;
}
return false;
} catch (Exception et) {
System.out.println(et);
return false;
}
}
}
//
进库数据库代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
class BookDatabase {
Connection con;
ResultSet rs;
Statement stmt;
public BookDatabase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
加载
JDBC-ODBC
桥驱动程序
String url = "jdbcdbc:Book";
con = DriverManager.getConnection(url);
//
连接数据库
HDB //stmt
提供一个创建
SQL
查询、执行查询、得到返回结果
的空间
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
增加一行用户名、密码数据
*/
public boolean insertData(String id, String name, String where, String
price, String date) {
try {
int rtn = stmt.executeUpdate("INSERT INTO Book VALUES('" + id
+ "','"
+ name + "','" + where + "','" + price + "','" + date
+ "')");
if (rtn != 0) {
return true;
} else {
return false;
}
} catch (Exception et) {
System.out.println(et);
return false;
}
}
/*
* public boolean deleteData(String id){ try{ int rtn =
* stmt.executeUpdate("DELETE FROM Book WHERE id=001" ); if( rtn != 0 ){
* return true; } else{ return false; } } catch(Exception e){
* System.out.println( e ) return false; } } public boolean Select(String
* id){ try{ int rtn = stmt.executeUpdate("SELECT * FROM BookIn WHERE
* id='"+id+"'");
*
* if( rtn != 0 ){ return true; } else{ return false; } } catch(Exception
* e){ System.out.println( e ) return false; } }
*/
}
//
出库数据库代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//只实现了图书的添加
package com.suncheng.main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
//路径
private final static String PATH = "D:\\";
//图书录入.txt
private final static String TSLR = PATH+"图书录入.txt";
//人员信息.txt
private final static String RYXX = PATH+"人员信息.txt";
//借还书.txt
private final static String JS_HS = PATH+"借还书.txt";
static{
try {
String[][] arr = {{"图书ID,图书名称",TSLR},{"人员ID,姓名",RYXX},{"借还ID,借书人,开始借书时间,最终还书截至时间,还书时间,是否已还",JS_HS}};
for(int i = 0; i arr.length; i++){
getTitle(arr[i][0], arr[i][1]);
}
} catch (IOException e) {
throw new RuntimeException("系统出现问题,请联系管理员!");
}
}
{
try {
init();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new Main();
}
//初始化菜单
private void init() throws IOException{
System.out.println("++++++++++++借书管理系统++++++++++++");
System.out.println("\t1). 图书的录入");
System.out.println("\t2). 人员信息的录入");
System.out.println("\t3). 图书的查询");
System.out.println("\t4). 借书的录入");
System.out.println("\t5). 还书的录入");
System.out.println("\t6). 人员借阅信息的显示");
System.out.println("请输入相应菜单:");
try {
int num = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
switch(num){
case 1 :
getTSLR();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//通用添加标题方法
private static void getTitle(String split,String path) throws IOException{
String[] arr = split.split(",");
BufferedWriter out = new BufferedWriter(new FileWriter(path));
for(int i = 0; i arr.length; i++){
out.write(arr[i]+"\t");
}
out.newLine();
out.close();
}
//添加图书方法
private boolean getTSLR() throws IOException{
boolean flag = false;
FileWriter fw = new FileWriter(this.TSLR,true);
BufferedReader br = getSystem_In();
while(true){
System.out.println("请输入图书ID");
String id = br.readLine();
System.out.println("请输入图书姓名");
String name = br.readLine();
fw.write(id+"\t");
fw.write(name+"\t");
fw.write("\r\n");
System.out.println("是否继续添加 Y、N");
if(!"y".equalsIgnoreCase(br.readLine())){
break;
}
}
fw.close();
br.close();
return flag;
}
private BufferedReader getSystem_In(){
return new BufferedReader(new InputStreamReader(System.in));
}
}
//图书类
class Book{
private int id; //图书ID
private String name; //图书名称
}
//人员类
class Person{
private int id; //人员ID
private String name; //姓名
}
//借还信息类
class Person_Book{
private int id; //借还ID
private String person; //借书人
private String startDate; //开始借书时间
private String stopDate; //最终还书截至时间
private String hsDate; //还书时间
private boolean flag; //是否已还 true还,false未
}