import java.io.FileOutputStream
import java.util.ArrayList
import java.util.List
import java.util.Scanner
import org.dom4j.Document
import org.dom4j.DocumentHelper
import org.dom4j.Element
import org.dom4j.io.OutputFormat
import org.dom4j.io.SAXReader
import org.dom4j.io.XMLWriter
public class Book {
private int no
private String name
private double value
public Book() {
}
public Book(int no, String name, double value) {
this.no = no
this.name = name
this.value = value
}
public double getValue() {
return value
}
public void setValue(double value) {
this.value = value
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public int getNo() {
return no
}
public void setNo(int no) {
this.no = no
}
}
class BookList {
private List<Book> bookList
public BookList() {
bookList = readXML()
}
public long getCount() {
return bookList.size()
}
public List<Book> getBookList() {
return bookList
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList
}
public void add(Book book) {
bookList.add(book)
}
public boolean delete(String name) {
Book book = query(name)
return bookList.remove(book)
}
public void update(Book bookBefore, Book bookAfter) {
bookList.remove(bookBefore)
add(bookAfter)
}
public Book query(String name) {
Book temp = null
for (Book book : bookList) {
if (book.getName().equals(name)) {
temp = book
}
}
return temp
}
public synchronized void writeXmlDocument(Book book) {
try {
File file = new File("D:\\book.xml")
Document document = null
Element root = null
if (!file.exists()) {
// 新建student.xml文件并新增内容
document = DocumentHelper.createDocument()
root = document.addElement("Books")//添加根节点
} else {
SAXReader saxReader = new SAXReader()
document = saxReader.read(file)
root = document.getRootElement()//获得根节点
}
Element secondRoot = root.addElement("Book")//二级节点
//为二级节点添加属性,属性值为对应属性的值
secondRoot.addElement("no").setText(book.getNo() + "")
secondRoot.addElement("name").setText(book.getName() + "")
secondRoot.addElement("value").setText(book.getValue() + "")
OutputFormat format = OutputFormat.createPrettyPrint()
format.setEncoding("GBK")
XMLWriter writer = new XMLWriter(new FileOutputStream("D:\\book.xml"), format)
writer.write(document)
writer.close()
document.clearContent()
} catch (Exception e) {
e.printStackTrace()
}
}
public synchronized List<Book> readXML() {
List<Book> list = new ArrayList<Book>()//创建list集合
File file = null
try {
file = new File("D:\\book.xml")//读取文件
if (file.exists()) {
SAXReader saxReader = new SAXReader()
Document document = saxReader.read(file)
List nodeList = document.selectNodes("Books/Book")
for (int i = 0 i < nodeList.size() i++) {
Element el = (Element) nodeList.get(i)
Book book = new Book()
book.setNo(Integer.parseInt(el.elementText("no")))
book.setName(el.elementText("name"))
book.setValue(Double.parseDouble(el.elementText("value")))
list.add(book)
}
}
} catch (Exception e) {
e.printStackTrace()
}
return list
}
}
class Test {
public static void main(String args[]) {
BookList bl = new BookList()
boolean bBreak = true
while (bBreak) {
System.out.println("请输入操作代码:")
System.out.println("1:添加 2:删除 3:修改 4:查询 5:书籍统计 6:退出")
Scanner sc = new Scanner(System.in)
int code = sc.nextInt()
if (code == 1) {
System.out.println("请输入编号")
int no = sc.nextInt()
System.out.println("请输入书名")
String name = sc.next()
System.out.println("请输入售价")
double value = sc.nextDouble()
Book book = new Book(no, name, value)
bl.add(book)
bl.writeXmlDocument(book)
} else if (code == 2) {
System.out.println("请输入要删除的书籍名")
String name = sc.next()
if (bl.delete(name)) {
System.out.println("删除成功")
} else {
System.out.println("书籍不存在")
}
} else if (code == 3) {
System.out.println("请输入要修改的书籍名")
String name = sc.next()
Book bookBefore = bl.query(name)
System.out.println("请输入新的编号")
int newNo = sc.nextInt()
System.out.println("请输入新的书名")
String newName = sc.next()
System.out.println("请输入新的售价")
double value = sc.nextDouble()
Book bookAfter = new Book(newNo, newName, value)
bl.update(bookBefore, bookAfter)
} else if (code == 4) {
System.out.println("请输入要查询的书籍名")
String name = sc.next()
Book book = bl.query(name)
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue())
} else if (code == 5) {
List<Book> list = bl.getBookList()
System.out.println("总书籍数:" + bl.getCount())
for (Book book : list) {
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue())
}
} else if (code == 6) {
bBreak = false
}
}
}
}
jar 包 dom4j.jar jaxen-1.1.4.jar
---------------------------------------------------给你修改了三个地方:
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 = 0i <2i++) {
// self[i].showBookInfo()
// }
for (int i = 0i <3i++) {
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 = 0i <10i++)
all[i].showBookInfo()
// self[score]=all[0]
}
void give(Books[] all, Books[] self) {// 还书
System.out.println("请输入您要还的书的书号")
int n = scan.nextInt()
for (int i = 0i <10i++) {
if (n == all[i].num) {
for (int j = 0j <3j++) {
if (self[j] == all[i]) {
self[j] = null
System.out.println("还书成功")
}
}
}
}
}
void have(Books[] self) {
// for (int i = 0i <2i++) {
// self[i].showBookInfo()
// }
for (int i = 0i <3i++) {
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 = 0i <10i++) {
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("**************************")
}
}
----------------------------------------------------
你的程序中错误很多,下面是我修改后的程序,其中的错误在程序的注释中说明了,希望可以帮到你#include//usingnamespacestd由于VC的Debug出现了fatalerrorC1001:INTERNALCOMPILERERROR错误,//将这个注释掉,上面写成#includeclass Vect{//向量表示 float *p//指向向量的指针 int n//向量的元素个数public: Vect(floata[]){ n=10 //这里初始化n,为了简单,暂时给n赋10 p=newfloat[10] //你需要给你的指针分配空间才可以给指针赋值 for(inti=0in!=v.n) return0//this是指针,不能用this.a,error没有定义,不能随便用 for(inti=0ip[i]+v.p[i] //这里不能用this.p[i],因为this是指针,要用this->p[i] returna}Vectoperator-(Vect&v1,Vect&v2) //你程序中VectVECT::operator-(Vect&v),‘-’重载的运算符是友元而不是类的成员函数{ //友元函数重载的双目运算符,形参需要两个,因为友元函数既然不属于类,那么自然没有隐含this指针 floatb[10]={0} Vecta(b) if(v1.n!=v2.n)return0 //同理,error没有定义,不能随便用 for(inti=0i