-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonLocker.java
More file actions
160 lines (122 loc) · 5.37 KB
/
AmazonLocker.java
File metadata and controls
160 lines (122 loc) · 5.37 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
import java.util.*;
import java.io.*;
public class AmazonLocker{
static Map<String, Integer> lockerMap = new HashMap<>();
static Map<String,String> itemLockerAsssociation = new HashMap<>();
static enum LockerCategory{
small,
medium,
large
}
static enum ItemCategory{
small,
medium,
large
}
static class Item{
String id;
String category;
public Item(String id, String category){
this.id = id;
this.category = category;
}
public String getId(){
return this.id;
}
public String getCategory(){
return this.category;
}
public String setId(String id){
this.id = id;
return id;
}
public String setCategory(String category){
this.category = category;
return category;
}
}
public static void initialiseLocker(){
lockerMap.put("small",1);
lockerMap.put("medium",0);
lockerMap.put("large",5);
}
public boolean checkLockerAvailablityForItem(String itemEnum){
if(lockerMap.get(itemEnum) > 0){
return true;
} else if(itemEnum.equals("small") && (lockerMap.get("medium") >0 || lockerMap.get("large") >0)){
return true;
} else if(itemEnum.equals("medium") && (lockerMap.get("large") >0)){
return true;
}
return false;
}
public void addItemInLocker(Item item){
String category = item.getCategory();
if(checkLockerAvailablityForItem(category)){
if(category.equals("small")){
// find samll, medium or large locker available
if(lockerMap.get(category.toString()) > 0){
System.out.println("small locker available - adding to it");
lockerMap.put(category.toString(), lockerMap.get(category.toString())-1);
itemLockerAsssociation.put(item.getId(),item.getCategory().toString());
}else if (lockerMap.get("medium") > 0){
System.out.println("small not available but medium locker available - adding to it");
lockerMap.put("medium", lockerMap.get("medium")-1);
itemLockerAsssociation.put(item.getId(),"medium");
}else{
System.out.println("small not available but large locker available - adding to it");
lockerMap.put("large", lockerMap.get("large")-1);
itemLockerAsssociation.put(item.getId(),"large");
}
} else if(category.equals("medium")){
// find medium or large locker available
if(lockerMap.get(category.toString()) > 0){
System.out.println("medium locker available - adding to it");
lockerMap.put(category.toString(), lockerMap.get(category.toString())-1);
itemLockerAsssociation.put(item.getId(),item.getCategory().toString());
}else if (lockerMap.get("large") > 0){
System.out.println("medium not available but large locker available - adding to it");
lockerMap.put("large", lockerMap.get("large")-1);
itemLockerAsssociation.put(item.getId(),"large");
}
} else{
if(lockerMap.get(category.toString()) > 0){
System.out.println("large locker available - adding to it");
lockerMap.put(category.toString(), lockerMap.get(category.toString())-1);
itemLockerAsssociation.put(item.getId(),item.getCategory().toString());
}
}
} else{
System.out.println("No Locker Available to add item");
}
}
public void removeItemFromLocker(Item item){
System.out.println(" Removing "+ item.getId() + " "+ "item from locker");
lockerMap.put(itemLockerAsssociation.get(item.getId()),lockerMap.get(itemLockerAsssociation.get(item.getId()))+1);
itemLockerAsssociation.remove(item.getId());
}
public void getCurrentLockerStatus(){
System.out.println("Current Lockers Available");
System.out.println(lockerMap);
System.out.println("-------------------------------------");
System.out.println("Current Item_ID-Locker Association");
System.out.println(itemLockerAsssociation);
}
public static void main(String[] args){
AmazonLocker lockerObj = new AmazonLocker();
Item item1 = new Item("200","small");
Item item2 = new Item("230","medium");
Item item3 = new Item("500","small");
Item item4 = new Item("600","large");
//initialise locker
initialiseLocker();
// add item in locker
lockerObj.addItemInLocker(item1);
lockerObj.addItemInLocker(item2);
lockerObj.addItemInLocker(item3);
lockerObj.addItemInLocker(item4);
lockerObj.getCurrentLockerStatus();
lockerObj.removeItemFromLocker(item1);
lockerObj.getCurrentLockerStatus();
}
}