-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.java
100 lines (99 loc) · 2.32 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
import java.util.*;
public class Inventory {
int a[][];
Inventory(){
a=new int[100][3];
}
void add(int id,int q,int p,int i) {
if(search(id)==-1) {
a[i][0]=id;
a[i][1]=q;
a[i++][2]=p;
}
else
System.out.println("Product already present");
}
void quant(int id, int q) {
int c=search(id);
if(c==-1)
System.out.println("ID not found");
else {
a[c][1]+=q;
}
}
void remove(int id,int q) {
int c=search(id);
if(c==-1)
System.out.println("ID not found");
else {
a[c][1]=a[c][1]<=0?0:a[c][1]-q;
a[c][1]=a[c][1]<0?0:a[c][1];
}
}
void display() {
int i=0;
System.out.println("Product_ID\tQuantity\tPrice");
while(a[i][0]!=0) {
System.out.println(a[i][0]+"\t"+a[i][1]+"\t"+a[i][2]);
i++;
}
}
void price(int id,int p) {
int c=search(id);
if(c==-1)
System.out.println("ID not found");
else {
a[c][2]=p;
}
}
void value() {
int i=0;
int s=0;
while(a[i][0]!=0)
s+=a[i][1]*a[i][2];
System.out.println("The total value of the inventory= "+s);
}
int search(int x){
int i=0;
while(a[i][0]!=0){
if(a[i][0]==x)
return i;
i++;
}
return -1;
}
public static void main(String args[]) {
Inventory ob=new Inventory();
int c;int i=0;
Scanner sc=new Scanner(System.in);
do {
System.out.println("Enter choice:\n(1)Add product to the inventory\n(2)Update quantity of Product\n(3)"
+ "Remove quantity\n(4)Display the inventory"
+ "\n(5)Update price of the items\n(6)Calculate value of the inventory");
int x=sc.nextInt();
switch(x) {
case 1:System.out.println("Enter ID, quantityand price");
ob.add(sc.nextInt(),sc.nextInt(),sc.nextInt(),i++);
break;
case 2:
System.out.println("Enter ID amd quantity");
ob.quant(sc.nextInt(), sc.nextInt());
break;
case 3:
System.out.println("Enter ID and quantity to be removed");
ob.remove(sc.nextInt(), sc.nextInt());
break;
case 4:
ob.display(); break;
case 5:
System.out.println("Enter ID and new price");
ob.price(sc.nextInt(),sc.nextInt());
break;
case 6: ob.value(); break;
default: System.out.println("Wrong choice");
}
System.out.println("Do you want to exit?(1/0)");
c=sc.nextInt();
}while(c!=1);
}
}