-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.java
More file actions
138 lines (122 loc) · 3.43 KB
/
Copy pathOrder.java
File metadata and controls
138 lines (122 loc) · 3.43 KB
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
import java.util.Date;
import java.text.DateFormat;
import java.text.DecimalFormat;
public class Order{
/**
* The new line separator
*/
private String NL = System.getProperty("line.separator");
/**
* This atribute stores the order reference code
*/
private String ref;
/**
* This atribute stores the order date
*/
private Date date;
/**
* This atribute stores the number of product units to make
*/
private int quantity;
/**
* This atribute stores the type of product to make
*/
private Product product;
/**
* Constructor method
*
* @param ref
* the Order's reference code
* @param date
* the Order's date
* @param quantity
* the number of units of product to make
* @param product
* the type of product to make
*/
public Order(String ref, Date date, int quantity, Product product) {
this.ref = ref;
this.date = date;
this.quantity = quantity;
this.product = product;
}
/**
* Getter method of atribute ref
* @return atribute ref
*/
public String getRef() {
return this.ref;
}
/**
* Getter method of atribute date
* @return atribute date
*/
public Date getDate() {
return this.date;
}
/**
* Getter method of atribute quantity
* @return atribute quantity
*/
public int getQuantity() {
return this.quantity;
}
/**
* Getter method of atribute product
* @return atribute product
*/
public Product getProduct() {
return this.product;
}
/**
* This method calculates and returns the total amount
* of the order.
*
* The total amount is calculated as:
* (quantity) x (price of the product)
*
* @return total amount of the order
*/
public double totalAmount() {
return (this.quantity * this.product.price());
}
/**
* This method generates and returns an String with
* the information of the Order object.
*
* Note that this method has to access to the Product
* atribute of the class to get some information.
*
*/
public String toString() {
DecimalFormat myFormat = new DecimalFormat("#,##0.00");
StringBuilder sb = new StringBuilder();
sb.append("Order ref: " + this.ref + NL);
sb.append("Order date: " + DateFormat.getDateInstance().format(this.date) + NL);
sb.append("Order information:" + NL);
sb.append("Product:" + NL);
sb.append(this.product.toString());
sb.append("Quantity: " + this.quantity + " units" + NL);
sb.append("Total amount: " + myFormat.format(this.totalAmount()) + " euros" + NL);
return sb.toString();
}
/**
* Redefinition of the equals method, to define whether two
* Order objects are equals.
*
* The method has to verify that the paramater object is an
* instance of class Order.
*
* So, two Order objects will be considered equals if, and
* only if, their reference codes are equals (regardless
* the values of the rest of atributes).
*/
public boolean equals (Object obj) {
if (obj instanceof Order) {
Order tmpOrder = (Order) obj;
return this.ref.equals(tmpOrder.getRef());
}
else
return false;
}
}