-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLShortenerApp.java
115 lines (90 loc) · 3.83 KB
/
URLShortenerApp.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
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
class URLShortener {
private static final String CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final int SHORT_URL_LENGTH = 6;
private HashMap<String, String> urlMap; // To store shortened and original URL mappings
private HashMap<String, String> shortUrlMap; // To store reverse mappings for uniqueness
public URLShortener() {
urlMap = new HashMap<>();
shortUrlMap = new HashMap<>();
}
// Method to shorten a long URL
public String shortenURL(String longURL) {
if (urlMap.containsKey(longURL)) {
System.out.println("This URL is already shortened.");
return urlMap.get(longURL);
}
String shortURL = generateShortURL();
// Check for collision and generate a new short URL if needed
while (shortUrlMap.containsKey(shortURL)) {
shortURL = generateShortURL();
}
urlMap.put(longURL, shortURL);
shortUrlMap.put(shortURL, longURL);
System.out.println("URL successfully shortened: " + shortURL);
return shortURL;
}
// Method to expand a short URL to the original long URL
public String expandURL(String shortURL) {
if (shortUrlMap.containsKey(shortURL)) {
System.out.println("Original URL found.");
return shortUrlMap.get(shortURL);
} else {
System.out.println("Error: Short URL not found.");
return null;
}
}
// Generate a short URL using random characters from CHAR_SET
private String generateShortURL() {
StringBuilder shortURL = new StringBuilder(SHORT_URL_LENGTH);
Random random = new Random();
for (int i = 0; i < SHORT_URL_LENGTH; i++) {
int index = random.nextInt(CHAR_SET.length());
shortURL.append(CHAR_SET.charAt(index));
}
return shortURL.toString();
}
}
public class URLShortenerApp {
public static void main(String[] args) {
URLShortener urlShortener = new URLShortener();
Scanner scanner = new Scanner(System.in);
boolean running = true;
System.out.println("Welcome to the URL Shortener!");
while (running) {
System.out.println("\nSelect an option:");
System.out.println("1. Shorten a URL");
System.out.println("2. Expand a short URL");
System.out.println("3. Exit");
System.out.print("Your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter a URL to shorten: ");
String longURL = scanner.nextLine();
String shortURL = urlShortener.shortenURL(longURL);
System.out.println("Shortened URL: " + shortURL);
break;
case 2:
System.out.print("Enter a short URL to expand: ");
String shortURLToExpand = scanner.nextLine();
String originalURL = urlShortener.expandURL(shortURLToExpand);
if (originalURL != null) {
System.out.println("Original URL: " + originalURL);
}
break;
case 3:
System.out.println("Exiting the program.");
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
scanner.close();
}
}