-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.java
131 lines (109 loc) · 3.4 KB
/
Inventory.java
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
/**
@author Pranav K. Hari, 101144482
@author Sahil Agrawal, 101132393
*/
import apple.laf.AquaLookAndFeel;
import java.util.ArrayList;
public class Inventory {
private ArrayList<Product> products = new ArrayList<>(); // ArrayList of Products
private ArrayList<Integer> stock = new ArrayList<>(); // Array of Stocks
/**
* Default Constructor initialized with some values
*/
public Inventory () {
Product product1 = new Product("Apple",5.00,0);
Product product2 = new Product("Chocolate",2.00,1) ;
Product product3 = new Product("Cheerios",8.00,2) ;
products.add(0,product1);
products.add(1,product2);
products.add(2,product3);
this.stock.add(0,1);
this.stock.add(1,2);
this.stock.add(2,3);
}
/**
* @param product Product to be added to products arrayList.
*/
public void addProduct (Product product) {
products.add(product);
}
/**
* Retrives the product given the product ID.
*
* @param id Product id to be checked
* @return Product Product with the same ID.
*/
public Product getProduct (int id) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getID() == id) {
return products.get(i);
}
}
return null;
}
/**
* Retrives the products in the inventory
*
* @return ArrayList Products
*/
public ArrayList<Product> getProducts() {
return products;
}
/**
* Retrieves the stock for a product given a product ID
*
* @param id Product id to check for
* @return int Returns stock of specified item id
*/
public int getStock (int id) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getID() == id) {
return stock.get(i);
}
}
return 0;
}
/**
* Increases a products stock given the product and the amount of stock to be added.
*
* @param product Product to add
* @param stock Amount of product to add
*/
public void addStock (Product product, int stock) {
for (int i = 0; i <= products.size(); i++) {
if (products.get(i).equals(product)) {
this.stock.set(i,this.stock.get(i)+1);
}
}
products.add(product);
this.stock.add(stock);
}
/**
* Decreases a products stock given the product and the amount of stock to be added.
* Sets a product's stock to default if stock is below 0.
*
* @param product Product to add
* @param stock Amount of product to remove
*/
public void removeStock (Product product, int stock) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).equals(product)) {
if (this.stock.get(i) - stock < 0){
this.stock.set(i,0);
}
else{
this.stock.set(i,this.stock.get(i)-stock);
}
}
}
}
/**
* Prints the current inventory of the store
*/
public void printInventory(){
System.out.print("Product Name | Unit Price | Stock\n");
for (int i =0; i<products.size(); i++){
System.out.print(products.get(i).getName() + " | " + "$"+products.get(i).getPrice() + " | " + stock.get(i)+"\n");
}
}
}