-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava-code.txt
More file actions
71 lines (54 loc) · 2.27 KB
/
Copy pathJava-code.txt
File metadata and controls
71 lines (54 loc) · 2.27 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
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class LinkShortener {
private Map<String, String> shortToLongMap;
private Map<String, String> longToShortMap;
private Random random;
private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final int SHORT_URL_LENGTH = 7;
public LinkShortener() {
this.shortToLongMap = new HashMap<>();
this.longToShortMap = new HashMap<>();
this.random = new Random();
}
public String shortenURL(String longUrl) {
if (longToShortMap.containsKey(longUrl)) {
return longToShortMap.get(longUrl);
}
String shortUrl = generateShortURL();
shortToLongMap.put(shortUrl, longUrl);
longToShortMap.put(longUrl, shortUrl);
return shortUrl;
}
public String expandURL(String shortUrl) {
if (shortToLongMap.containsKey(shortUrl)) {
return shortToLongMap.get(shortUrl);
} else {
return "Short URL does not exist in the system.";
}
}
private String generateShortURL() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SHORT_URL_LENGTH; i++) {
int index = random.nextInt(CHARACTERS.length());
sb.append(CHARACTERS.charAt(index));
}
return sb.toString();
}
public static void main(String[] args) {
LinkShortener linkShortener = new LinkShortener();
String longUrl1 = "https://www.example.com/articles/how-to-use-java";
String longUrl2 = "https://www.example.com/products/java-programming-book";
String shortUrl1 = linkShortener.shortenURL(longUrl1);
String shortUrl2 = linkShortener.shortenURL(longUrl2);
System.out.println("Shortened URLs:");
System.out.println(shortUrl1);
System.out.println(shortUrl2);
String expandedUrl1 = linkShortener.expandURL(shortUrl1);
String expandedUrl2 = linkShortener.expandURL(shortUrl2);
System.out.println("\nExpanded URLs:");
System.out.println(expandedUrl1);
System.out.println(expandedUrl2);
}
}