Created
June 24, 2023 13:41
-
-
Save R3DHULK/ffadc7a0e5e6ecc4cab5c59d7e8add33 to your computer and use it in GitHub Desktop.
Weather App Written In Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class WeatherApp extends JFrame { | |
private JTextField locationField; | |
private JTextField temperatureField; | |
private JLabel conditionLabel; | |
public WeatherApp() { | |
setTitle("Weather App"); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLayout(new FlowLayout()); | |
// Feature 1: Location input | |
JLabel locationLabel = new JLabel("Location:"); | |
locationField = new JTextField(15); | |
// Feature 2: Fetch button | |
JButton fetchButton = new JButton("Fetch"); | |
// Feature 3: Condition display | |
JLabel conditionTextLabel = new JLabel("Condition:"); | |
conditionLabel = new JLabel(); | |
// Feature 4: Temperature display | |
JLabel temperatureLabel = new JLabel("Temperature:"); | |
temperatureField = new JTextField(15); | |
temperatureField.setEditable(false); | |
fetchButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
String location = locationField.getText(); | |
if (!location.isEmpty()) { | |
String apiKey = "aba6ff9d6de967d5eac6fd79114693cc"; | |
String apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + apiKey; | |
try { | |
URL url = new URL(apiUrl); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
int responseCode = connection.getResponseCode(); | |
if (responseCode == HttpURLConnection.HTTP_OK) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
String line; | |
StringBuilder response = new StringBuilder(); | |
while ((line = reader.readLine()) != null) { | |
response.append(line); | |
} | |
reader.close(); | |
// Feature 4: Temperature extraction | |
String temperature = parseTemperature(response.toString()); | |
// Feature 3: Condition extraction | |
String condition = parseCondition(response.toString()); | |
temperatureField.setText(temperature); | |
conditionLabel.setText(condition); | |
} else { | |
temperatureField.setText("Error"); | |
conditionLabel.setText(""); | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
}); | |
add(locationLabel); | |
add(locationField); | |
add(fetchButton); | |
add(temperatureLabel); | |
add(temperatureField); | |
add(conditionTextLabel); | |
add(conditionLabel); | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
// Feature 4: Temperature parsing | |
private String parseTemperature(String json) { | |
int startIndex = json.indexOf("\"temp\":") + 7; | |
int endIndex = json.indexOf(",", startIndex); | |
String temperature = json.substring(startIndex, endIndex); | |
double kelvin = Double.parseDouble(temperature); | |
double celsius = kelvin - 273.15; | |
return String.format("%.2f °C", celsius); | |
} | |
// Feature 3: Condition parsing | |
private String parseCondition(String json) { | |
int startIndex = json.indexOf("\"main\":\"") + 8; | |
int endIndex = json.indexOf("\"", startIndex); | |
String condition = json.substring(startIndex, endIndex); | |
return condition; | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
new WeatherApp(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment