-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathDesign Movie Rental System.java
102 lines (89 loc) · 2.93 KB
/
Design Movie Rental System.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
class MovieRentingSystem {
Comparator<MovieEntry> comparator =
(o1, o2) -> {
if (o1.price != o2.price) {
return o1.price - o2.price;
}
if (o1.shopId != o2.shopId) {
return o1.shopId - o2.shopId;
}
return o1.movieId - o2.movieId;
};
Map<Integer, Set<MovieEntry>> movieToUnrentedShopMapping;
Map<ShopMoviePair, Integer> shopMoviePairToPriceMapping;
TreeSet<MovieEntry> rentedMovies;
public MovieRentingSystem(int n, int[][] entries) {
movieToUnrentedShopMapping = new HashMap<>();
shopMoviePairToPriceMapping = new HashMap<>();
rentedMovies = new TreeSet<>(comparator);
for (int[] entry : entries) {
movieToUnrentedShopMapping
.computeIfAbsent(entry[1], k -> new TreeSet<>(comparator))
.add(new MovieEntry(entry[1], entry[2], entry[0]));
shopMoviePairToPriceMapping.put(new ShopMoviePair(entry[0], entry[1]), entry[2]);
}
}
public List<Integer> search(int movie) {
return movieToUnrentedShopMapping.getOrDefault(movie, Collections.emptySet()).stream()
.limit(5)
.map(e -> e.shopId)
.collect(Collectors.toList());
}
public void rent(int shop, int movie) {
int price = shopMoviePairToPriceMapping.get(new ShopMoviePair(shop, movie));
movieToUnrentedShopMapping.get(movie).remove(new MovieEntry(movie, price, shop));
rentedMovies.add(new MovieEntry(movie, price, shop));
}
public void drop(int shop, int movie) {
int price = shopMoviePairToPriceMapping.get(new ShopMoviePair(shop, movie));
movieToUnrentedShopMapping.get(movie).add(new MovieEntry(movie, price, shop));
rentedMovies.remove(new MovieEntry(movie, price, shop));
}
public List<List<Integer>> report() {
return rentedMovies.stream()
.limit(5)
.map(e -> List.of(e.shopId, e.movieId))
.collect(Collectors.toList());
}
class ShopMoviePair {
int shopId;
int movieId;
public ShopMoviePair(int shopId, int movieId) {
this.shopId = shopId;
this.movieId = movieId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ShopMoviePair that = (ShopMoviePair) o;
return shopId == that.shopId && movieId == that.movieId;
}
@Override
public int hashCode() {
return Objects.hash(shopId, movieId);
}
}
class MovieEntry {
int movieId;
int price;
int shopId;
public MovieEntry(int movieId, int price, int shopId) {
this.movieId = movieId;
this.price = price;
this.shopId = shopId;
}
}
}
/**
* Your MovieRentingSystem object will be instantiated and called as such:
* MovieRentingSystem obj = new MovieRentingSystem(n, entries);
* List<Integer> param_1 = obj.search(movie);
* obj.rent(shop,movie);
* obj.drop(shop,movie);
* List<List<Integer>> param_4 = obj.report();
*/