⑴ 用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;
}
}