-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLShortenerGUI.java
More file actions
108 lines (91 loc) · 3.33 KB
/
Copy pathURLShortenerGUI.java
File metadata and controls
108 lines (91 loc) · 3.33 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
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package com.mycompany.link_shortener;
/**
*
* @author Sambit
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class URLShortenerGUI extends JFrame {
private JTextField originalUrlTextField;
private JTextField shortUrlTextField;
private JButton shortenButton;
private Map<String, String> urlMap;
public URLShortenerGUI() {
// Initialize the URL map
urlMap = new HashMap<>();
// Set up the JFrame
setTitle("URL Shortener");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 150);
setLocationRelativeTo(null);
// Create components
originalUrlTextField = new JTextField();
shortUrlTextField = new JTextField();
shortUrlTextField.setEditable(false);
shortenButton = new JButton("Shorten");
// Set layout
setLayout(new GridLayout(3, 2));
// Add components to the JFrame
add(new JLabel("Original URL:"));
add(originalUrlTextField);
add(new JLabel("Shortened URL:"));
add(shortUrlTextField);
add(new JLabel());
add(shortenButton);
// Add action listener to the shorten button
shortenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shortenUrl();
}
});
}
private void shortenUrl() {
String originalUrl = originalUrlTextField.getText().trim();
if (originalUrl.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a valid URL.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Check if the URL is already shortened
if (urlMap.containsValue(originalUrl)) {
for (Map.Entry<String, String> entry : urlMap.entrySet()) {
if (entry.getValue().equals(originalUrl)) {
shortUrlTextField.setText(entry.getKey());
return;
}
}
}
// Generate a short URL
String shortUrl = generateShortUrl();
urlMap.put(shortUrl, originalUrl);
shortUrlTextField.setText(shortUrl);
}
private String generateShortUrl() {
// Generate a random short URL (in practice, a more sophisticated algorithm
// would be used)
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int length = 6;
Random random = new Random();
StringBuilder shortUrl = new StringBuilder();
for (int i = 0; i < length; i++) {
shortUrl.append(characters.charAt(random.nextInt(characters.length())));
}
return shortUrl.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new URLShortenerGUI().setVisible(true);
}
});
}
}