-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.java
More file actions
77 lines (70 loc) · 2.82 KB
/
Copy pathProduct.java
File metadata and controls
77 lines (70 loc) · 2.82 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
import java.util.*;
public class Product {
static List<Product> productArrayList = new ArrayList<>();
static Map<Integer, Product> allProducts = new HashMap<>();
static Scanner input = new Scanner(System.in);
static int id = 1;
int productId;
String productName;
int productCount;
int productPrice;
public Product(String productName, int productPrice, int stockCount) {
this.productId = id++;
this.productName = productName;
this.productPrice = productPrice;
this.productCount =stockCount;
}
public static void ProductsControlPanel() {
System.out.println("1 -> Add new Product\n2 -> Update Product Stock\n3 -> Delete Product\n4 -> Back\nChoice");
int choice = input.nextInt();
switch (choice){
case 1:
createNewProduct();
break;
case 2:
System.out.println("Enter Product Id:");
int productId = input.nextInt();
if(!isExistingProduct(productId)) break;
updateProductStock(productId);
break;
case 3:
System.out.println("Enter Product Id:");
productId = input.nextInt();
if(!isExistingProduct(productId)) break;
allProducts.remove(productId);
System.out.println("=====================> Product Deleted!");
break;
case 4:
AdminControlPanel.AdminControls("admin", "password");
break;
}
ProductsControlPanel();
}
private static void updateProductStock(int productId) {
Product currentProduct = allProducts.get(productId);
System.out.println("Enter Stock:");
int stockCount = input.nextInt();
currentProduct.productCount =stockCount;
System.out.println("=====================> Stock Updated!");
}
private static boolean isExistingProduct(int productId) {
boolean isExisting = allProducts.containsKey(productId);
if(!isExisting) System.out.println("Product Not Available!");
return isExisting;
}
private static void createNewProduct() {
System.out.println("Enter Product name, Price, Stock Count");
String productName = input.next();
int productPrice = input.nextInt();
int stockCount = input.nextInt();
Product newProduct = new Product(productName,productPrice,stockCount);
allProducts.put(newProduct.productId, newProduct);
System.out.println("Product "+newProduct.productName+" added with Stock Count : "+newProduct.productCount);
}
static void updateProductList() {
productArrayList.clear();
for (Product currentProduct : allProducts.values()) {
productArrayList.add(currentProduct);
}
}
}