Skip to content

Commit e431741

Browse files
committed
Added Singleton java quiz
1 parent c334ac8 commit e431741

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
30+
31+
*.iml
32+
*.idea

singleton-design-pattern/.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
30+
31+
*.iml
32+
*.idea
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stacktips.designpatterns;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.Map;
6+
import java.util.Queue;
7+
8+
class RateLimiter {
9+
10+
private static RateLimiter instance;
11+
12+
private Map<String, Queue<Integer>> userRequests;
13+
14+
private static final int MAX_REQUESTS_PER_MINUTE = 3;
15+
16+
private RateLimiter() {
17+
userRequests = new HashMap<>();
18+
}
19+
20+
public static RateLimiter getInstance() {
21+
if (instance == null) {
22+
synchronized (RateLimiter.class) {
23+
if (instance == null) {
24+
instance = new RateLimiter();
25+
}
26+
}
27+
}
28+
return instance;
29+
}
30+
31+
public synchronized boolean allowRequest(int timestamp, String userId) {
32+
int currentMinute = timestamp / 60;
33+
34+
Queue<Integer> requests = userRequests.computeIfAbsent(userId, k -> new LinkedList<>());
35+
while (!requests.isEmpty() && requests.peek() / 60 < currentMinute) {
36+
requests.poll();
37+
}
38+
39+
if (requests.size() < MAX_REQUESTS_PER_MINUTE) {
40+
requests.offer(timestamp);
41+
return true;
42+
}
43+
44+
return false;
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.stacktips.designpatterns;
2+
3+
import java.util.Scanner;
4+
5+
public class RateLimiterDemo {
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
RateLimiter rateLimiter = RateLimiter.getInstance();
9+
10+
while (true) {
11+
System.out.print("Enter user ID (or 'exit' to quit): ");
12+
String input = scanner.nextLine();
13+
14+
if (input.equalsIgnoreCase("exit")) {
15+
break;
16+
}
17+
18+
int currentTimestamp = (int) (System.currentTimeMillis() / 1000);
19+
if (rateLimiter.allowRequest(currentTimestamp, input)) {
20+
System.out.println("Hello! Request accepted for user " + input);
21+
} else {
22+
System.out.println("Rate limit exceeded for user " + input + ". Try again later.");
23+
}
24+
}
25+
26+
scanner.close();
27+
System.out.println("Program terminated.");
28+
}
29+
}

0 commit comments

Comments
 (0)