⑴ 用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編寫圖書管理系統,使用XML存儲
importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
importorg.dom4j.Document;
importorg.dom4j.DocumentHelper;
importorg.dom4j.Element;
importorg.dom4j.io.OutputFormat;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;
publicclassBook{
privateintno;
privateStringname;
privatedoublevalue;
publicBook(){
}
publicBook(intno,Stringname,doublevalue){
this.no=no;
this.name=name;
this.value=value;
}
publicdoublegetValue(){
returnvalue;
}
publicvoidsetValue(doublevalue){
this.value=value;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetNo(){
returnno;
}
publicvoidsetNo(intno){
this.no=no;
}
}
classBookList{
privateList<Book>bookList;
publicBookList(){
bookList=readXML();
}
publiclonggetCount(){
returnbookList.size();
}
publicList<Book>getBookList(){
returnbookList;
}
publicvoidsetBookList(List<Book>bookList){
this.bookList=bookList;
}
publicvoidadd(Bookbook){
bookList.add(book);
}
publicbooleandelete(Stringname){
Bookbook=query(name);
returnbookList.remove(book);
}
publicvoipdate(BookbookBefore,BookbookAfter){
bookList.remove(bookBefore);
add(bookAfter);
}
publicBookquery(Stringname){
Booktemp=null;
for(Bookbook:bookList){
if(book.getName().equals(name)){
temp=book;
}
}
returntemp;
}
(Bookbook){
try{
Filefile=newFile("D:\book.xml");
Documentdocument=null;
Elementroot=null;
if(!file.exists()){
//新建student.xml文件並新增內容
document=DocumentHelper.createDocument();
root=document.addElement("Books");//添加根節點
}else{
SAXReadersaxReader=newSAXReader();
document=saxReader.read(file);
root=document.getRootElement();//獲得根節點
}
ElementsecondRoot=root.addElement("Book");//二級節點
//為二級節點添加屬性,屬性值為對應屬性的值
secondRoot.addElement("no").setText(book.getNo()+"");
secondRoot.addElement("name").setText(book.getName()+"");
secondRoot.addElement("value").setText(book.getValue()+"");
OutputFormatformat=OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriterwriter=newXMLWriter(newFileOutputStream("D:\book.xml"),format);
writer.write(document);
writer.close();
document.clearContent();
}catch(Exceptione){
e.printStackTrace();
}
}
publicsynchronizedList<Book>readXML(){
List<Book>list=newArrayList<Book>();//創建list集合
Filefile=null;
try{
file=newFile("D:\book.xml");//讀取文件
if(file.exists()){
SAXReadersaxReader=newSAXReader();
Documentdocument=saxReader.read(file);
ListnodeList=document.selectNodes("Books/Book");
for(inti=0;i<nodeList.size();i++){
Elementel=(Element)nodeList.get(i);
Bookbook=newBook();
book.setNo(Integer.parseInt(el.elementText("no")));
book.setName(el.elementText("name"));
book.setValue(Double.parseDouble(el.elementText("value")));
list.add(book);
}
}
}catch(Exceptione){
e.printStackTrace();
}
returnlist;
}
}
classTest{
publicstaticvoidmain(Stringargs[]){
BookListbl=newBookList();
booleanbBreak=true;
while(bBreak){
System.out.println("請輸入操作代碼:");
System.out.println("1:添加2:刪除3:修改4:查詢5:書籍統計6:退出");
Scannersc=newScanner(System.in);
intcode=sc.nextInt();
if(code==1){
System.out.println("請輸入編號");
intno=sc.nextInt();
System.out.println("請輸入書名");
Stringname=sc.next();
System.out.println("請輸入售價");
doublevalue=sc.nextDouble();
Bookbook=newBook(no,name,value);
bl.add(book);
bl.writeXmlDocument(book);
}elseif(code==2){
System.out.println("請輸入要刪除的書籍名");
Stringname=sc.next();
if(bl.delete(name)){
System.out.println("刪除成功");
}else{
System.out.println("書籍不存在");
}
}elseif(code==3){
System.out.println("請輸入要修改的書籍名");
Stringname=sc.next();
BookbookBefore=bl.query(name);
System.out.println("請輸入新的編號");
intnewNo=sc.nextInt();
System.out.println("請輸入新的書名");
StringnewName=sc.next();
System.out.println("請輸入新的售價");
doublevalue=sc.nextDouble();
BookbookAfter=newBook(newNo,newName,value);
bl.update(bookBefore,bookAfter);
}elseif(code==4){
System.out.println("請輸入要查詢的書籍名");
Stringname=sc.next();
Bookbook=bl.query(name);
System.out.println("編號:"+book.getNo()+"書名:"+book.getName()+"售價:"+book.getValue());
}elseif(code==5){
List<Book>list=bl.getBookList();
System.out.println("總書籍數:"+bl.getCount());
for(Bookbook:list){
System.out.println("編號:"+book.getNo()+"書名:"+book.getName()+"售價:"+book.getValue());
}
}elseif(code==6){
bBreak=false;
}
}
}
}
jar 包 dom4j.jar jaxen-1.1.4.jar
⑶ JAVA圖書租賃系統源代碼
程序如下:
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LibrarySystem
{
static List<String> list = new ArrayList<String>();
static double money = 0;
static boolean borrow = true;
public static void doBorrow()
{
System.out.print("請輸入圖書名稱<計算機書,新書>:");
Scanner scanner = new Scanner(System.in);
String bookName = scanner.next();
System.out.print("請輸入圖書類型<1:計算機書,2:新書>:");
int type = 0;
int day = 0;
try
{
type = scanner.nextInt();
if((bookName.equals("計算機書") && type != 1) ||
(bookName.equals("新書") && type != 2))
{
System.out.println("類型輸入有誤,系統退出。");
System.exit(0);
}
} catch (RuntimeException e)
{
System.out.println("類型輸入有誤,系統退出。");
System.exit(0);
}
System.out.print("請輸入借閱天數(必須是整數):");
try
{
day = scanner.nextInt();
} catch (RuntimeException e)
{
System.out.println("天數輸入有誤,系統退出。");
System.exit(0);
}
double tempMoney = 0;
//計算租金
if(type == 1)
{
tempMoney = day*1.5;
money += tempMoney;
}
else if(type == 2)
{
tempMoney = day > 3?((day-3)*2+4.5):day*1.5;
money += tempMoney;
}
list.add(MessageFormat.format("書名:{0},借閱天數:{1},租金:{2}", bookName,day,tempMoney));
System.out.print("是否繼續借書(1-是,2-不是):");
try
{
type = scanner.nextInt();
while(type != 1 && type != 2)
{
System.out.print("輸入有誤,請重新輸入:");
type = scanner.nextInt();
}
borrow = type == 1? true:false;
} catch (RuntimeException e)
{
System.out.println("天數輸入有誤,系統退出。");
}
}
public static void main(String[] args)
{
System.out.println("============歡迎使用圖書租賃管理系統===========");
while(borrow)
{
doBorrow();
}
System.out.println("你的借書記錄:");
System.out.println("-----------------------------------");
for(String s : list)
{
System.out.println(s);
}
System.out.println("-----------------------------------");
System.out.println("總金額:" + money);
}
}
有問題歡迎提問,滿意請採納,謝謝!
⑷ 用JAVA做一個圖書館管理系統,不需要用到資料庫
packagecom.efounder.formbuilder.dat;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Map.Entry;
importcom.efounder.formbuilder.fmt.FmtCol;
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2005</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
{
protectedjava.util.MapdataCellList=null;
/**
*
*@returnMap
*/
publicMapgetDataCellList(){
returndataCellList;
}
/**
*
*@parammapMap
*/
publicvoidsetDataCellList(Mapmap){
dataCellList=map;
}
/**
*數據行上存在的是多個維度,每個維度對應的是一個數據字典
*/
publicDataRow(){
}
/**
*
*@paramkeyObject
*@paramdataCellDataCell
*/
publicvoidputDataCell(Objectkey,DataCelldataCell){
if(dataCellList==null)dataCellList=newjava.util.HashMap();
dataCellList.put(key,dataCell);
}
/**
*
*@paramkeyObject
*@returnDataCell
*/
publicDataCellgetDataCell(Objectkey){
DataCelldatacell=null;
if(dataCellList!=null){
datacell=(DataCell)dataCellList.get(key);
if(datacell==null)
datacell=getDataCellIterator(key);
}
returndatacell;
}
/**
*FmtColFLEX對象以FmtCol對象名稱為key需要遍歷取出對應的DataCelladdbywujfat20120220
*@paramkeyObject
*@returnDataCell
*/
(Objectkey){
if(dataCellList!=null){
IteratorentrySetIterator=dataCellList.entrySet().iterator();
while(entrySetIterator.hasNext()){
Entryentry=(Entry)entrySetIterator.next();
if(entry.getKey()instanceofFmtCol)break;
// if(((String)entry.getKey()).startsWith("com.efounder.formbuilder.fmt.FmtCol")){
DataCelldatacell=(DataCell)entry.getValue();
if(datacell.getDataCol()==key){
returndatacell;
}
}
}
returnnull;
}
}
⑸ 使用java編寫一份圖書管理系統(急!!!!)
首先建立一個book類在model包下的book.Java文件中,接著是主文件,在UI包下的MainClass.java文件,然後編寫代碼,不會的找專業書籍學習下,不難的 純手打!望採納!
⑹ 圖書管理系統用java語言 1:輸入5本書 書名,書號,價格 2:計算書的總價格,平均價格 3
packagetest;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
importjava.util.Scanner;publicclassBookShow{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannersc=newScanner(System.in);
System.out.println("請輸入第一本書名、書號、價格(輸入時用*隔開,例:紅樓夢*1001*100):");
Stringstr1=sc.next();
Bookbook1=BookShow.ReturnBook(str1);
System.out.println("請輸入第二本書名、書號、價格(輸入時用*隔開,例:紅樓夢*1001*100):");
Stringstr2=sc.next();
Bookbook2=BookShow.ReturnBook(str2);
System.out.println("請輸入第三本書名、書號、價格(輸入時用*隔開,例:紅樓夢*1001*100):");
Stringstr3=sc.next();
Bookbook3=BookShow.ReturnBook(str3);
System.out.println("請輸入第四本書名、書號、價格(輸入時用*隔開,例:紅樓夢*1001*100):");
Stringstr4=sc.next();
Bookbook4=BookShow.ReturnBook(str4);
System.out.println("請輸入第五本書名、書號、價格(輸入時用*隔開,例:紅樓夢*1001*100):");
Stringstr5=sc.next();
Bookbook5=BookShow.ReturnBook(str5);
List<Book>books=newArrayList<Book>();
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(book5);
System.out.println("----------------------------------------------");
doublesumprice=book1.getPrice()+book2.getPrice()+book3.getPrice()+book4.getPrice()+book5.getPrice();
System.out.println("五本書的總價格為:"+sumprice);
doubleavgprice=sumprice/5;
System.out.println("五本書的平均價格為:"+avgprice);
List<Double>prices=newArrayList<Double>();
prices.add(book1.getPrice());
prices.add(book2.getPrice());
prices.add(book3.getPrice());
prices.add(book4.getPrice());
prices.add(book5.getPrice());
Collections.sort(prices);
System.out.println("價格最高的為:"+prices.get(4)+"價格最低的為:"+prices.get(0));
List<Book>books2=newArrayList<Book>();
List<Book>books3=newArrayList<Book>();
for(Bookb:books){
if(b.getPrice()>avgprice){
books2.add(b);
}elseif(b.getPrice()<avgprice){
books3.add(b);
}
}
System.out.println("高於平均價格的書有:"+BookShow.toShow(books2));
System.out.println("低於平均價格的書有:"+BookShow.toShow(books3));
System.out.println("----------------------------------------------");
while(true){
System.out.println("請輸入書號查詢書籍(輸入0退出):");
intbookId=sc.nextInt();
if(bookId==0){
break;
}else{
Bookbook=newBook();
for(Bookb:books){
if(bookId==b.getBookId()){
book=b;
}
}
System.out.println("當前書籍為:書名"+book.getName()+",書號"+book.getBookId()+",價格"+book.getPrice());
}
}
}
publicstaticBookReturnBook(Stringstr){
Bookbook=newBook(str.substring(0,str.indexOf("*")),
str.substring(str.indexOf("*")+1,str.lastIndexOf("*")),
str.substring(str.lastIndexOf("*")+1,str.length()));
returnbook;
}
publicstaticStringtoShow(List<Book>books){
Stringstr="";
for(Bookb:books){
str+=b.getName()+"";
}
returnstr;
}
}
classBook{
privateStringname;
privatedoubleprice;
privateintbookId;
publicBook(){}
publicBook(Stringname,StringbookId,Stringprice){
this.name=name;
this.price=Double.parseDouble(price);
this.bookId=Integer.parseInt(bookId);
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
publicintgetBookId(){
returnbookId;
}
publicvoidsetBookId(intbookId){
this.bookId=bookId;
}
}