-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductList.java
More file actions
102 lines (89 loc) · 3.01 KB
/
ProductList.java
File metadata and controls
102 lines (89 loc) · 3.01 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
package onlinestore;
import java.util.Arrays;
public class ProductList implements Cloneable{
private Product[] products;
private int amountProducts;
//constructor
public ProductList() {
this.products = new Product[2];
this.amountProducts = 0;
}
//copy constructor
public ProductList(ProductList other) {
this.amountProducts = other.amountProducts;
this.products = new Product[other.products.length];
for (int i = 0; i < this.amountProducts; i++) {
products[i] = new Product(other.products[i]);
}
}
public Product[] getProducts() {
return this.products;
}
public int getAmountProducts() {
return this.amountProducts;
}
private void largeArrayProduct() {
if (amountProducts == products.length) {
Product[] newProducts = new Product[products.length * 2];
for (int i = 0; i < amountProducts; i++) {
newProducts[i] = products[i];
}
this.products = newProducts;
}
}
public void addProduct(Product product) {
largeArrayProduct();
products[amountProducts++] = product;
}
//פונקציה שבנינו סתם לא צריך לבדוק אותה
//Switch between old product to a new one
public boolean updateProduct(Product oldProduct, Product newProduct) {
for (int i = 0; i < amountProducts; i++) {
if (products[i].equals(oldProduct)) {
products[i] = newProduct;
return true;
}
}
return false;
}
//פונקציה שבנינו סתם לא צריך לבדוק אותה
//remove or change product from seller
// this function checks if the product exist and if exist its remove the product.
public boolean removeProduct(Product product) {
for (int i = 0; i < amountProducts; i++) {
if (products[i].equals(product)) {
for (int j = i + 1; j < amountProducts; j++) {
products[j - 1] = products[j];
}
products[amountProducts - 1] = null;
amountProducts--;
return true;
}
}
return false;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ProductList)) {
return false;
}
ProductList products = (ProductList) other;
return Arrays.equals(this.products, products.products);
}
@Override
public ProductList clone() throws CloneNotSupportedException {
ProductList temp = (ProductList) super.clone();
for (int i = 0; i < temp.amountProducts; i++) {
temp.products[i] = this.products[i].clone();
}
return temp;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < amountProducts; i++) {
sb.append("- " + products[i].toString() + "\n");
}
return sb.toString();
}
}