-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendor.java
More file actions
214 lines (186 loc) · 6.03 KB
/
Vendor.java
File metadata and controls
214 lines (186 loc) · 6.03 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
210
211
212
213
214
package model;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Represents a vendor in the POS system.
* Contains information about the vendor including contact details and
* associated products.
*/
public class Vendor {
// Instance variables
private String vendorId;
private String name;
private String contactInfo;
private List<VendorProduct> productList;
// Constants
private static final String DEFAULT_CONTACT_INFO = "Contact information not available";
/**
* Default constructor
*/
public Vendor() {
this.productList = new ArrayList<>();
}
/**
* Constructor with basic vendor information
*
* @param vendorId unique identifier for the vendor
* @param name vendor name
* @param contactInfo contact information for the vendor
*/
public Vendor(String vendorId, String name, String contactInfo) {
this.vendorId = vendorId;
this.name = name;
this.contactInfo = contactInfo != null ? contactInfo : DEFAULT_CONTACT_INFO;
this.productList = new ArrayList<>();
}
/**
* Constructor with all vendor details
*
* @param vendorId unique identifier for the vendor
* @param name vendor name
* @param contactInfo contact information for the vendor
* @param productList list of products supplied by the vendor
*/
public Vendor(String vendorId, String name, String contactInfo, List<VendorProduct> productList) {
this.vendorId = vendorId;
this.name = name;
this.contactInfo = contactInfo != null ? contactInfo : DEFAULT_CONTACT_INFO;
this.productList = productList != null ? new ArrayList<>(productList) : new ArrayList<>();
}
// Business logic methods
/**
* Add a product to the vendor's product list
*
* @param product product to add
*/
public void addProduct(VendorProduct product) {
if (product != null && !productList.contains(product)) {
productList.add(product);
}
}
/**
* Remove a product from the vendor's product list
*
* @param product product to remove
* @return true if product was removed, false otherwise
*/
public boolean removeProduct(VendorProduct product) {
return productList.remove(product);
}
/**
* Get the number of products supplied by this vendor
*
* @return number of products
*/
public int getProductCount() {
return productList.size();
}
/**
* Check if the vendor has any products
*
* @return true if vendor has products, false otherwise
*/
public boolean hasProducts() {
return !productList.isEmpty();
}
/**
* Get the total value of all products supplied by this vendor
*
* @return total value of all products
*/
public double getTotalProductValue() {
return productList.stream()
.mapToDouble(VendorProduct::getStockValue)
.sum();
}
/**
* Get the display name for the vendor
*
* @return display name or vendor ID if name is not available
*/
public String getDisplayName() {
if (name != null && !name.trim().isEmpty()) {
return name.trim();
}
return vendorId != null ? vendorId : "Unknown Vendor";
}
/**
* Check if the vendor has valid contact information
*
* @return true if contact info is available, false otherwise
*/
public boolean hasContactInfo() {
return contactInfo != null && !contactInfo.trim().isEmpty()
&& !contactInfo.equals(DEFAULT_CONTACT_INFO);
}
/**
* Get products by category
*
* @param category category to filter by
* @return list of products in the specified category
*/
public List<VendorProduct> getProductsByCategory(String category) {
if (category == null || category.trim().isEmpty()) {
return new ArrayList<>();
}
return productList.stream()
.filter(product -> category.equalsIgnoreCase(product.getCategory()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}
/**
* Get low stock products (quantity less than threshold)
*
* @param threshold quantity threshold
* @return list of products with low stock
*/
public List<VendorProduct> getLowStockProducts(int threshold) {
return productList.stream()
.filter(product -> product.getQuantity() < threshold)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}
// Getters and Setters
public String getVendorId() {
return vendorId;
}
public void setVendorId(String vendorId) {
this.vendorId = vendorId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactInfo() {
return contactInfo;
}
public void setContactInfo(String contactInfo) {
this.contactInfo = contactInfo != null ? contactInfo : DEFAULT_CONTACT_INFO;
}
public List<VendorProduct> getProductList() {
return new ArrayList<>(productList); // Return a copy to prevent external modification
}
public void setProductList(List<VendorProduct> productList) {
this.productList = productList != null ? new ArrayList<>(productList) : new ArrayList<>();
}
// Object methods
@Override
public String toString() {
return String.format("Vendor{id='%s', name='%s', products=%d}",
vendorId, getDisplayName(), getProductCount());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Vendor vendor = (Vendor) obj;
return Objects.equals(vendorId, vendor.vendorId);
}
@Override
public int hashCode() {
return Objects.hash(vendorId);
}
}