8-1 图书馆持久化 (40 分)

8-1 图书馆持久化 (40 分)

构造图书类,包含名称(字符串)、作者(字符串)、出版社(字符串)、版本号(整数)、价格(浮点数),构造图书馆类,其中包含若干图书,用容器存储图书对象,然后定义方法void addBook(Book b)添加图书对象,定义方法void persist(),将所有图书存至本地文件books.dat里,定义方法Book[] restore() 从文件books,dat中读取所有图书,并返回图书列表数组。 main函数中构造图书馆类对象,向其中添加3个图书对象,测试其persist和restore方法。(注意,请使用对象序列化方法实现)

代码

代码仅供参考!!!

import java.util.*;
import java.io.*;

class Book implements Serializable {
	public String name;
	public String author;
	public String publisher;
	public int version;
	public double price;

	public Book(String name, String author, String publisher, int version, double price) {
		super();
		this.name = name;
		this.author = author;
		this.publisher = publisher;
		this.version = version;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Book [name=" + name + ", author=" + author + ", publisher=" + publisher + ", version=" + version
				+ ", price=" + price + "]";
	}

}

class Library implements Serializable {

	LinkedList<Book> list = new LinkedList<Book>();

	public Library(Book book) {
		list.add(book);
	}

	public Library() {
		// TODO Auto-generated constructor stub
	}

	public void addBook(Book book) {
		list.add(book);
	}

	public void persist() {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("book.dat"));
			oos.writeObject(list);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	public Book[] restore() {
		LinkedList<Book> listbook = new LinkedList<Book>();
		ObjectInputStream ois = null;
		try {
			Book book;
			ois = new ObjectInputStream(new FileInputStream("book.dat"));
			listbook = (LinkedList<Book>) ois.readObject();

		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return (Book[]) listbook.toArray(new Book[listbook.size()]);

	}

}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		Library lib = new Library();
		System.out.print("请输入图书的本数:");
		int n = scan.nextInt();
		for (int i = 0; i < n; i++) {
			System.out.println("请依次输入第" + (i + 1) + "本书的名称(字符串)、作者(字符串)、出版社(字符串)、版本号(整数)、价格(浮点数)");
			lib.addBook(new Book(scan.next(), scan.next(), scan.next(), scan.nextInt(), scan.nextDouble()));
		}
		lib.persist();
		Book[] books = lib.restore();

		for (Book book : books) {
			System.out.println(book);
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值