使用对象流实现如下效果:
需求:
- 网上购物时某用户填写订单,订单内容为产品列表,保存在"order.bin"文件中
- 运行时
- 如果不存在"order.bin"文件,则进行新订单录入
- 如果存在,则显示并计算客户所需付款
- 分析:
-
编写init()方法提供从控制台输入购买信息
-
编写save()方法保存对象到"order.bin"文件中
- 编写load()方法获得对象,计算客户所需付款
代码实现
// 序列化类的设计:有几个类,分别是什么属性? // 1、判断order.bin文件是否存在 // 2、如果不存在 // 1) 调用init()方法获得一组订单数据 // 2) 调用save()方法将第1步获得的订单数据保存到order.bin文件中 // 3、如果存在 // 1) 调用load()方法从order.bin文件中读取中数据 // 4、获得一组订单数据,遍历,商品的信息显示出来 // 5、计算总价
实现功能代码如下:
//客户类
public class Customer implements java.io.Serializable {
public String name ;
public Order order;
// 省略getter/setter方法
}
import java.util.*;
//订单类
public class Order implements java.io.Serializable {
public String orderNo;
public ArrayList productList;
public Order() {}
public Order(String orderNo) {
this.orderNo = orderNo;
this.productList = new ArrayList<Product>();
}
// 省略getter/setter方法
}
//产品类
public class Product implements java.io.Serializable {
public String productNo ;
public String name ;
public int num ;
public float price ;
public Product(String productNo, String name, int num, float price){
this.productNo = productNo;
this.name = name;
this.num = num;
this.price = price;
}
// 省略getter/setter方法
}
import java.io.*;
import java.util.Scanner;
public class SerializableObj {
Customer cust = new Customer();
public void Init() {
System.out.println("请输入用户名:");
Scanner input = new Scanner(System.in);
String customerName = input.next();
Order order = new Order("A2321");
System.out.println("请输入选择的产品:");
boolean isContinue = true;
while (isContinue) {
System.out.println("产品号:");
String productNo = input.next();
System.out.println("名称:");
String name = input.next();
System.out.println("购买数量:");
int num = input.nextInt();
System.out.println("产品单价:");
float price = input.nextFloat();
Product prod = new Product(productNo, name, num, price);
order.getProductList().add(prod); // 将产品加入订单
System.out.println("是否继续? Y/N");
String yesNo = input.next();
if (yesNo.equals("N") || yesNo.equals("n")) {
isContinue = false;
}
}
cust.setName(customerName);
cust.setOrder(order);
}
public void Save() {
// 序列化
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("order.bin"));
// 对象序列化,写入输出流
oos.writeObject(cust);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void Load() throws ClassNotFoundException {
// 反序列化
ObjectInputStream ois = null;
try {
// 创建ObjectInputStream输入流
ois = new ObjectInputStream(new FileInputStream("order.bin"));
// 反序列化,强转类型
cust = (Customer)ois.readObject();
// 输出生成后对象信息
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null) {ois.close();}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
SerializableObj test = new SerializableObj();
File file=new File("save.bin");
if (file.exists()){
test.Load();
}else {
test.Init();
test.Save();
}
//计算
float total = 0;
System.out.println("产品名\t单价\t数量");
for(Object obj:test.cust.getOrder().getProductList()){
Product prod=(Product)obj;
System.out.println(prod.getName()
+"\t"+prod.getPrice()
+"\t"+prod.getNum());
total += prod.getPrice() * prod.getNum();
}
System.out.println("\n订单总价:" + total);
}