-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
140 lines (139 loc) · 6.75 KB
/
Copy pathMain.java
File metadata and controls
140 lines (139 loc) · 6.75 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setSize(400, 550);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setTitle("Weather App");
jFrame.setLayout(null);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
JLabel label = new JLabel("Enter Location, City, or Zip Code:");
JLabel creater = new JLabel("Weather App Created by Juovani Kirlos");
JTextField textField = new JTextField(20);
JButton button = new JButton("Get Weather");
JButton infoPM = new JButton("PM Accelerator LinkedIn");
inputPanel.add(creater);
inputPanel.add(label);
inputPanel.add(textField);
inputPanel.add(button);
inputPanel.add(infoPM);
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0, 110, 400, 500);
inputPanel.setBounds(0, 0, 400, 110);
jFrame.add(inputPanel, BorderLayout.NORTH);
jFrame.add(scrollPane);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
infoPM.addActionListener(e -> {
try {
String linkedInUrl = "https://www.linkedin.com/school/pmaccelerator/about/";
Desktop.getDesktop().browse(new URL(linkedInUrl).toURI());
} catch (Exception ex){
JOptionPane.showMessageDialog(jFrame, "Failed to open LinkedIn page: " + ex.getMessage());
}
});
button.addActionListener(e -> {
String locationInput = textField.getText().trim();
if (locationInput.isEmpty()) {
JOptionPane.showMessageDialog(jFrame, "Please enter a location.");
return;
}
try {
String apiKey = readConfigFile();
String locationKey = getLocation(locationInput, apiKey);
StringBuilder result = new StringBuilder();
result.append("Current Conditions: ")
.append(getConditions(locationKey, apiKey))
.append("\n");
result.append("Current Temperature: ")
.append(getTemperature(locationKey, apiKey))
.append("°F\n\n");
result.append(get5DayForecast(locationKey, apiKey));
textArea.setText(result.toString());
} catch (Exception ex) {
textArea.setText("Error: " + ex.getMessage());
}
});
}
private static String getLocation(String locationInput, String apiKey) throws Exception {
String locationUrl = "https://dataservice.accuweather.com/locations/v1/cities/search?apikey="
+ apiKey + "&q=" + locationInput;
String response = sendHttpRequest(locationUrl);
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response, JsonArray.class);
return jsonArray.get(0).getAsJsonObject().get("Key").getAsString();
}
private static String getConditions(String locationKey, String apiKey) throws Exception {
String conditionsUrl = "https://dataservice.accuweather.com/currentconditions/v1/"
+ locationKey + "?apikey=" + apiKey;
String response = sendHttpRequest(conditionsUrl);
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response, JsonArray.class);
return jsonArray.get(0).getAsJsonObject().get("WeatherText").getAsString();
}
private static String getTemperature(String locationKey, String apiKey) throws Exception {
String temperatureUrl = "https://dataservice.accuweather.com/currentconditions/v1/"
+ locationKey + "?apikey=" + apiKey;
String response = sendHttpRequest(temperatureUrl);
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(response, JsonArray.class);
JsonObject temperatureObject = jsonArray.get(0).getAsJsonObject()
.getAsJsonObject("Temperature")
.getAsJsonObject("Imperial");
return String.valueOf(temperatureObject.get("Value").getAsDouble());
}
private static String get5DayForecast(String locationKey, String apiKey) throws Exception {
String forecastUrl = "https://dataservice.accuweather.com/forecasts/v1/daily/5day/"
+ locationKey + "?apikey=" + apiKey;
String response = sendHttpRequest(forecastUrl);
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
JsonArray dailyForecasts = jsonObject.getAsJsonArray("DailyForecasts");
StringBuilder forecast = new StringBuilder();
for (int i = 0; i < dailyForecasts.size(); i++) {
JsonObject dayForecast = dailyForecasts.get(i).getAsJsonObject();
String date = dayForecast.get("Date").getAsString();
double maxTemp = dayForecast.getAsJsonObject("Temperature")
.getAsJsonObject("Maximum")
.get("Value").getAsDouble();
String conditions = dayForecast.getAsJsonObject("Day").get("IconPhrase").getAsString();
forecast.append(i + 1 + " day: ").append(date).append(": Max Temp: ").append(maxTemp)
.append("°F, Conditions: ").append(conditions).append("\n");
}
return forecast.toString();
}
private static String readConfigFile() {
// Replace with your actual API key
return "API KEY GOES HERE";
}
private static String sendHttpRequest(String urlString) throws Exception {
URL url = new URL(urlString);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty("Content-Type", "application/json");
int responseCode = connect.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("HTTP request failed with response code: " + responseCode);
}
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
}