switch(key){
10年積累的網(wǎng)站設計、網(wǎng)站建設經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先建設網(wǎng)站后付款的網(wǎng)站建設流程,更有永昌免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
case 1:
//借書
break;
case 2:
//還書
break;
}
---------------------------------------------------
給你修改了三個地方:
1.borrowBooks方法中,將System.out.println("你要借嗎?"); 改為:
System.out.println("你要借嗎?輸入1表示借,其他數(shù)字表示不借。");
保證輸入的時候輸入的數(shù)字,否則會報出異常。
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表示借,其他數(shù)字表示不借。");
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連接數(shù)據(jù)庫,這個網(wǎng)上好多你去搜下就有
2.借書還書就是對某張表的更新操作,你借書還書的時候可以得到該書的一個id,這時,你通過這個id從數(shù)據(jù)庫中找到所對應的數(shù)據(jù),更改書得庫存量即可。
自己動手做一遍,其實不難的,加油!
/
用戶登陸數(shù)據(jù)庫代碼:
import java.sql.*;
class Database {
Connection con;
ResultSet rs;
Statement stmt;
public Database() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
加載
JDBC-ODBC
橋驅(qū)動程序
String url = "jdbcdbc:HDB";
con = DriverManager.getConnection(url);
//
連接數(shù)據(jù)庫
HDB //stmt
提供一個創(chuàng)建
SQL
查詢、執(zhí)行查詢、得到返回結果
的空間
stmt =
// con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
// ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
根據(jù)用戶名,獲取密碼
*/
public String getPswd(String name) {
try {
// rs
為執(zhí)行
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
列的數(shù)據(jù)
return pswdDB;
}
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/*
增加一行用戶名、密碼數(shù)據(jù)
*/
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;
}
}
}
//
進庫數(shù)據(jù)庫代碼:
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
橋驅(qū)動程序
String url = "jdbcdbc:Book";
con = DriverManager.getConnection(url);
//
連接數(shù)據(jù)庫
HDB //stmt
提供一個創(chuàng)建
SQL
查詢、執(zhí)行查詢、得到返回結果
的空間
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
System.out.println(ex);
}
}
/*
增加一行用戶名、密碼數(shù)據(jù)
*/
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; } }
*/
}
//
出庫數(shù)據(jù)庫代碼:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//只實現(xiàn)了圖書的添加
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("系統(tǒng)出現(xiàn)問題,請聯(lián)系管理員!");
}
}
{
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("++++++++++++借書管理系統(tǒng)++++++++++++");
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("是否繼續(xù)添加?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未
}