blob: 8986495da934c13b9e97622f381978b771248a80 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package examples.shop.impl.entity;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* This entity represents an order placed for goods.
*
* Note: This entity could easily be extended to include other things, such as:
* <ul>
* <li>Shipping charges
* <li>Shipping address vs. Billing address
* <li>A date which this order is scheduled to be completed/shipped.
* </ul>
*/
@Entity
@Table(name = "ORDERS")
public class Order {
public enum Status {
Submitted, Unverified, Approved, Shipping, Delivered, Returned
};
private String orderId;
private List<LineItem> lineItems;
private Customer customer;
private Timestamp orderDate;
private Status status;
private double taxes;
private double subTotal;
/**
* This order's identification number. It's also our Primary Key.
*
* @return order id
*/
@Id
public String getOrderID() {
return orderId;
}
/**
* Sets the order id of an order
*/
public void setOrderID(String id) {
orderId = id;
}
/**
* This represents a one-to-many relationship to the
* line items in the order. Persisting the order cascades
* to the line items.
*
* @return the set of order line items
*/
@OneToMany(cascade = CascadeType.PERSIST)
public List<LineItem> getLineItems() {
return lineItems;
}
/**
* Sets set of the order line items
*/
public void setLineItems(List<LineItem> lineItems) {
this.lineItems = lineItems;
}
/**
* @returns the Customer who placed this Order.
*/
public Customer getCustomer() {
return customer;
}
/**
* set customer who placed this order.
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* Returns the date this order was placed.
*/
public Timestamp getOrderDate() {
return orderDate;
}
/**
* Sets the date this order
*/
public void setOrderDate(Timestamp orderDate) {
this.orderDate = orderDate;
}
/**
* @return status information about the order.
*/
public Status getStatus() {
return status;
}
/**
* Sets status information about the order.
*/
public void setStatus(Status status) {
this.status = status;
}
/**
* Sets the subtotal of this order
*/
public double getSubTotal() {
return subTotal;
}
/**
* Returns the subtotal of this order
*/
public void setSubTotal(double subTotal) {
this.subTotal = subTotal;
}
/**
* Returns taxes of this order
*/
public double getTaxes() {
return taxes;
}
/**
* Sets the taxes of this order
*/
public void setTaxes(double taxes) {
this.taxes = taxes;
}
/**
* Returns the subtotal rice of the entire order, ie. the sum of
* the base prices of all line items in this order.
*/
public double totalPrice() {
double totalPrice = 0;
for (Iterator<LineItem> iter = getLineItems().iterator(); iter
.hasNext();) {
LineItem item = iter.next();
totalPrice += item.getProduct().basePrice;
}
return totalPrice;
}
/**
* Called to initialize a new order entity
*
* We need to initialize our entity's state with the parameters passed from
* the client.
*/
public void init(String orderID, Customer customer, Status status,
double subTotal, double taxes) {
System.out.println("Order.init(" + orderID + ") called");
setOrderID(orderID);
setOrderDate(new java.sql.Timestamp(System.currentTimeMillis()));
setStatus(status);
setSubTotal(subTotal);
setTaxes(taxes);
}
}
|