-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSale.java
More file actions
209 lines (181 loc) · 5.6 KB
/
Sale.java
File metadata and controls
209 lines (181 loc) · 5.6 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package model;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
* Represents a sale transaction in the POS system.
* Contains information about the sale including products, amounts, and
* metadata.
*/
public class Sale {
// Instance variables
private String saleId;
private List<Product> products;
private double totalAmount;
private Date date;
private String branchCode;
private int quantity;
// Constants
private static final double MIN_AMOUNT = 0.0;
private static final int MIN_QUANTITY = 0;
/**
* Constructor with all sale details
*
* @param saleId unique identifier for the sale
* @param products list of products in the sale
* @param quantity total quantity of items sold
* @param totalAmount total amount of the sale
* @param date date of the sale
* @param branchCode branch code where sale occurred
*/
public Sale(String saleId, List<Product> products, int quantity,
double totalAmount, Date date, String branchCode) {
this.saleId = saleId;
this.products = products;
this.quantity = validateQuantity(quantity);
this.totalAmount = validateAmount(totalAmount);
this.date = date != null ? new Date(date.getTime()) : new Date();
this.branchCode = branchCode;
}
/**
* Constructor without quantity (quantity will be calculated from products)
*
* @param saleId unique identifier for the sale
* @param products list of products in the sale
* @param totalAmount total amount of the sale
* @param date date of the sale
* @param branchCode branch code where sale occurred
*/
public Sale(String saleId, List<Product> products, double totalAmount,
Date date, String branchCode) {
this.saleId = saleId;
this.products = products;
this.totalAmount = validateAmount(totalAmount);
this.date = date != null ? new Date(date.getTime()) : new Date();
this.branchCode = branchCode;
this.quantity = calculateTotalQuantity();
}
// Validation methods
private double validateAmount(double amount) {
if (amount < MIN_AMOUNT) {
throw new IllegalArgumentException("Total amount cannot be negative");
}
return amount;
}
private int validateQuantity(int quantity) {
if (quantity < MIN_QUANTITY) {
throw new IllegalArgumentException("Quantity cannot be negative");
}
return quantity;
}
// Business logic methods
/**
* Calculate total quantity from products
*
* @return total quantity of all products
*/
private int calculateTotalQuantity() {
if (products == null) {
return 0;
}
return products.stream()
.mapToInt(Product::getSoldQuantity)
.sum();
}
/**
* Get the number of different products in this sale
*
* @return number of unique products
*/
public int getProductCount() {
return products != null ? products.size() : 0;
}
/**
* Check if this sale has any products
*
* @return true if sale has products, false otherwise
*/
public boolean hasProducts() {
return products != null && !products.isEmpty();
}
/**
* Calculate the average price per item
*
* @return average price, or 0 if no quantity
*/
public double getAveragePricePerItem() {
if (quantity <= 0) {
return 0.0;
}
return totalAmount / quantity;
}
/**
* Get a formatted string representation of the sale date
*
* @return formatted date string
*/
public String getFormattedDate() {
if (date == null) {
return "N/A";
}
return date.toString();
}
// Getters and Setters
public String getSaleId() {
return saleId;
}
public void setSaleId(String saleId) {
this.saleId = saleId;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
// Recalculate quantity when products change
this.quantity = calculateTotalQuantity();
}
public double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(double totalAmount) {
this.totalAmount = validateAmount(totalAmount);
}
public Date getDate() {
return date != null ? new Date(date.getTime()) : null;
}
public void setDate(Date date) {
this.date = date != null ? new Date(date.getTime()) : new Date();
}
public String getBranchCode() {
return branchCode;
}
public void setBranchCode(String branchCode) {
this.branchCode = branchCode;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = validateQuantity(quantity);
}
// Object methods
@Override
public String toString() {
return String.format("Sale{id='%s', amount=%.2f, date=%s, branch='%s', items=%d}",
saleId, totalAmount, getFormattedDate(), branchCode, quantity);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Sale sale = (Sale) obj;
return Objects.equals(saleId, sale.saleId);
}
@Override
public int hashCode() {
return Objects.hash(saleId);
}
}