l现在有一个订单处理系统,里面有一个保存订单的业务功能,需求:每当订单的预定产品数量超过1000的时候,就需要把订单拆成两份订单来保存。如果拆成两份后还是超过1000,则继续拆分,直到每份产品预订数量不超过1000.
根据业务,目前的订单系统分成两种,一种是个人订单、一种是公司订单。
客户名称、产品对象(ID,Name),订购产品数量。
公司名称、产品对象(ID,Name),订购产品数量。
订单接口
package B;
public interface Order {
int getNum();
void setNum(int num);
void display();
Object Clone() throws CloneNotSupportedException;
}
产品类
package B;
public class Product implements Cloneable {
private int Id;
private String name;
public Product() {
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object Clone() throws CloneNotSupportedException {
return super.clone();
}
}
客户订单
package B;
public class CustomerOrder implements Cloneable, Order {
private String Uname;
private int num;
private Product product;
public CustomerOrder(String Uname) {
this.Uname = Uname;
product = new Product();
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
private CustomerOrder(Product product) throws CloneNotSupportedException {
this.product = (Product) product.Clone();
}
public void setProduct(int Pid, String Pname) {
product.setId(Pid);
product.setName(Pname);
}
public void display() {
System.out.print("客户名:" + Uname + " ");
System.out.println("订购数目:" + num);
System.out
.println("产品号" + product.getId() + " 产品名" + product.getName());
}
public Object Clone() throws CloneNotSupportedException {
CustomerOrder obj = new CustomerOrder(this.product);
obj.Uname = this.Uname;
obj.num = this.num;
return obj;
}
}
企业订单
package B;
public class CompanyOrder implements Cloneable, Order {
private String Uname;
private int num;
private Product product;
public CompanyOrder(String Uname) {
this.Uname = Uname;
product = new Product();
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
private CompanyOrder(Product product) throws CloneNotSupportedException {
this.product = (Product) product.Clone();
}
public void setProduct(int Pid, String Pname) {
product.setId(Pid);
product.setName(Pname);
}
public void display() {
System.out.print("客户名:" + Uname + " ");
System.out.println("订购数目:" + num);
System.out
.println("产品号" + product.getId() + " 产品名" + product.getName());
}
public Object Clone() throws CloneNotSupportedException {
CompanyOrder obj = new CompanyOrder(this.product);
obj.Uname = this.Uname;
obj.num = this.num;
return obj;
}
}
拆分订单操作类
package B;
public class Chaifen {
private Order co;
public Chaifen() {
}
public Chaifen(Order co) {
this.co = co;
}
public void Manage() throws CloneNotSupportedException {
while (co.getNum() > 1000) {
Order c2 = (Order) co.Clone();
c2.setNum(1000);
c2.display();
co.setNum(co.getNum() - 1000);
}
co.display();
}
}
客户端
package B;
public class Test {
public static void main(String args[]) throws CloneNotSupportedException {
CustomerOrder co = new CustomerOrder("王浩");
co.setNum(3500);
co.setProduct(1, "蒙牛");
Chaifen cf = new Chaifen(co);
cf.Manage();
CompanyOrder cp = new CompanyOrder("山东农大");
cp.setNum(2200);
cp.setProduct(2, "光明");
Chaifen cf2 = new Chaifen(cp);
cf2.Manage();
}
}