-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWareHouse.java
More file actions
63 lines (53 loc) · 1.6 KB
/
Copy pathWareHouse.java
File metadata and controls
63 lines (53 loc) · 1.6 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Scanner;
public class WareHouseProblem {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int numOfTestCases = sc.nextInt();
Hashtable<String, Item> toysWithNameAndQuantity = new Hashtable<String, Item>();
for(int i = 0; i<numOfTestCases; i++) {
fillHashTable(toysWithNameAndQuantity);
}
}
public static void fillHashTable(Hashtable<String, Item> table){
int noOfItems = sc.nextInt();
sc.nextLine();
for(int i =0; i<noOfItems; i++) {
String keyVal = sc.nextLine();
String[] keyValPair = keyVal.split(" ");
Item itemToAdd = new Item(keyValPair[0], Integer.parseInt(keyValPair[1]));
if(table.containsKey(keyValPair[0])) {
Item duplicatedObject = table.get(keyValPair[0]);
table.put(keyValPair[0], new Item(keyValPair[0], Integer.parseInt(keyValPair[1]) + duplicatedObject.Quantity));
}
else {
table.put(keyValPair[0], itemToAdd );
}
}
List<Item> itemList = new ArrayList<>(table.values());
Collections.sort(itemList, Comparator.comparing(Item::GetQuantity).reversed().thenComparing(Item::GetName));
System.out.println(table.size());
for (Item i : itemList) {
System.out.println(i.Name + " " + i.Quantity);
}
table.clear();
}
}
class Item {
public String Name;
public int Quantity;
public Item(String Name, int Quantity) {
this.Name = Name;
this.Quantity = Quantity;
}
public String GetName() {
return this.Name;
}
public int GetQuantity() {
return this.Quantity;
}
}