展开全部
//产品32313133353236313431303231363533e59b9ee7ad9431333366303162类
package cn.pojo;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
//产品编号
private int id;
//产品名
private String name;
//库存
private int stock;
//单价
private double price;
public Product() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
//订单类
package cn.pojo;
import java.io.Serializable;
import java.util.List;
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
//订单号
private String orderId;
//产品列表
private List productList;
public Order() {
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public List getProductList() {
return productList;
}
public void setProductList(List productList) {
this.productList = productList;
}
}
//顾客类
package cn.pojo;
import java.io.Serializable;
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
//顾客姓名
private String name;
//订单对象
private Order order;
public Customer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
//顾客服务(测试)
package cn.pojo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;
public class CustomerBiz {
//顾客对象
private static Customer customer = new Customer();
//编写save()方法保存对象到"save.bin"二进制文件。
private static void save() {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("data\\save.bin"));
oos.writeObject(customer);
oos.flush();
System.out.println("保存成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
oos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//编写load()方法获得对象,计算顾客所需的付款
private static void load() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("data/save.bin"));
customer = (Customer)ois.readObject();
System.out.println("加载成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//显示产品信息
private static void showProduct() {
double total = 0.0;
List proList= customer.getOrder().getProductList();
System.out.println("产品名\t单价\t数量");
for (Product product : proList) {
if(product != null) {
total += (product.getPrice() * product.getStock());
System.out.println(product.getName() + "\t" + product.getPrice() + "\t" + product.getStock());
}
}
System.out.println("订单总价:" + total);
}
//编写InitData()初始化数据的方法,提供从控制台输入购买产品的信息
private static void initData() {
BufferedReader input = null;
try {
input = new BufferedReader(new InputStreamReader(System.in));
String reply = null;
Product product = null;
List proList = new ArrayList<>();
//声明订单对象
Order order = new Order();
//使用UUID随机生成订单流水号
//UUID 通用唯一标识符(Universally Unique Identifier)
order.setOrderId(UUID.randomUUID().toString().replaceAll("-", ""));
System.out.print("请输入顾客姓名:");
String name = input.readLine();
System.out.println("请输入选择的产品");
do {
System.out.print("请输入产品号:");
int proId = Integer.parseInt(input.readLine());
System.out.print("请输入产品名:");
String proName = input.readLine();
System.out.print("请输入购买数量:");
int proNum = Integer.valueOf(input.readLine());
System.out.print("请输入产品单价:");
double proPrice = Double.parseDouble(input.readLine());
System.out.print("是否继续购买(Y/N)?");
reply = input.readLine();
//创建产品对象,封装产品数据
product = new Product();
product.setId(proId);
product.setName(proName);
product.setStock(proNum);
product.setPrice(proPrice);
//将每一次购买的产品添加容器里面
proList.add(product);
} while("y".equalsIgnoreCase(reply));
order.setProductList(proList); //将产品列表添加到订单对象中
customer.setName(name);
customer.setOrder(order); //订单对象添加顾客对象
//序列化
save();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != input) {
try {
input.close();
input = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 程序在运行的时候,如果不存在"save.bin",则进行新订单录入,如果存在,
则显示并计算客户所需付款
* */
private static void orderInput() {
File file = new File("data\\save.bin");
if(!file.exists()) {
initData();
showProduct();
}
else {
load();
showProduct();
}
}
public static void main(String[] args) {
CustomerBiz cb = new CustomerBiz();
cb.orderInput();
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起