Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Silva committed Oct 30, 2018
1 parent 90f4cba commit 0f393f1
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 128 deletions.
4 changes: 2 additions & 2 deletions www-weather-information-system/src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WeatherReceiver.WundergroundWeatherReceiver;
import database.Reading;
/*import WeatherReceiver.WundergroundWeatherReceiver;
import database.Reading;*/

public class Main {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,36 @@

abstract public class WeatherReceiver {
String stationName;
public WeatherReceiver(String stationName){

public WeatherReceiver(String stationName) {
this.stationName = stationName;
}

abstract public Reading getCurrentReading();

public static String readPage(String url, String delimeter)
{
try
{
public static String readPage(String url, String delimeter) {
try {
URL URL = new URL(url);
URLConnection connection = URL.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, lines = "";
while ((line = reader.readLine()) != null)
{
if(lines != "")
{
while ((line = reader.readLine()) != null) {
if (lines != "") {
lines += delimeter;
}
lines += line;
}
return lines;
}
catch (Exception e)
{
} catch (Exception e) {
return null;
}
}

protected float farenheitToCelcius(float temperature){
return (temperature - 32) * 5/9;
protected float farenheitToCelcius(float temperature) {
return (temperature - 32) * 5 / 9;
}

protected float mercuryInchesToPascals(float mercuryInches){
protected float mercuryInchesToPascals(float mercuryInches) {
return mercuryInches / (float) 0.00029530;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.regex.Pattern;

public class WundergroundWeatherReceiver extends WeatherReceiver {
public WundergroundWeatherReceiver(String stationName){
public WundergroundWeatherReceiver(String stationName) {
super(stationName);
}

Expand All @@ -31,7 +31,7 @@ public Reading getCurrentReading() {
while (m.find()) {
String s = m.group(1);
System.out.printf("Found Units: %s\n", s);
if(s.equals("F")){
if (s.equals("F")) {
imperialUnits = true;
}
// s now contains "BAR"
Expand Down Expand Up @@ -64,7 +64,7 @@ public Reading getCurrentReading() {
while (m.find()) {
String s = m.group(1);
System.out.printf("Found UV: %s\n", s);
if(uvCount == 3){
if (uvCount == 3) {
uvIndex = Integer.parseInt(s);
}
uvCount++;
Expand Down Expand Up @@ -112,15 +112,15 @@ public Reading getCurrentReading() {
}

//Unit conversions
int temperatureC = (int)temperatureF;
int pressurePascals = (int)pressureInches;
if(imperialUnits){
temperatureC = (int)farenheitToCelcius(temperatureF);
pressurePascals = (int)mercuryInchesToPascals(pressureInches) / 100;
int temperatureC = (int) temperatureF;
int pressurePascals = (int) pressureInches;
if (imperialUnits) {
temperatureC = (int) farenheitToCelcius(temperatureF);
pressurePascals = (int) mercuryInchesToPascals(pressureInches) / 100;
}


Reading reading = new Reading(uvIndex,humidity,temperatureC,(int)windSpeed,windDirection,pressurePascals,(int)rainfall);
Reading reading = new Reading(uvIndex, humidity, temperatureC, (int) windSpeed, windDirection, pressurePascals, (int) rainfall);


String testOutput = String.format("UV: %s Humidity: %s Temperature: %s Wind speed: %s Wind direction: %s Pressure: %s Rainfall: %s",
Expand All @@ -132,9 +132,9 @@ public Reading getCurrentReading() {
return reading;
}

private String getPageData(String stationName){
String url = String.format("https://www.wunderground.com/weather/au/%s",stationName);
private String getPageData(String stationName) {
String url = String.format("https://www.wunderground.com/weather/au/%s", stationName);

return readPage(url,"\n");
return readPage(url, "\n");
}
}
2 changes: 1 addition & 1 deletion www-weather-information-system/src/database/Reading.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package database;

import java.sql.Timestamp;
//import java.sql.Timestamp;

public class Reading {
int readingUVindex;
Expand Down
5 changes: 0 additions & 5 deletions www-weather/src/GUI/Panels/LoginScreen.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package GUI.Panels;

import GUI.*;
import GUI.Window;
import database.AdminUser;
import database.WeatherStation;
import javafx.scene.control.PasswordField;

import javax.swing.*;
import javax.swing.tree.ExpandVetoException;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.security.PublicKey;

/**
* Created by AleCSilva on 23/10/18.
Expand Down
7 changes: 3 additions & 4 deletions www-weather/src/GUI/Panels/MaintenanceScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Date;

Expand Down Expand Up @@ -74,17 +73,17 @@ public MaintenanceScheduler(WeatherStation station) {
});
submitButton.addActionListener((e) -> {
try {
Date date = new Date(model.getYear()-1900,model.getMonth(),model.getDay());
Date date = new Date(model.getYear() - 1900, model.getMonth(), model.getDay());
String notes = notesInput.getText();

database.databaseInterface.submitMaintenanceSchedule(station,notes,date);
database.databaseInterface.submitMaintenanceSchedule(station, notes, date);

System.out.println("Job submitted to database.");
JOptionPane.showMessageDialog(null, ("Job submitted."));

Window.showStationMaintenance();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Submission failure");
JOptionPane.showMessageDialog(null, "Submission failure");
ex.printStackTrace();
}
});
Expand Down
36 changes: 16 additions & 20 deletions www-weather/src/GUI/Panels/PastWeather.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package GUI.Panels;

import GUI.*;
import GUI.Window;
import database.Reading;
import database.WeatherStation;

import javax.swing.*;
import javax.swing.tree.ExpandVetoException;
import java.awt.*;
import java.security.PublicKey;

/**
* Created by jc300556 on 23/10/18.
*/
public class PastWeather extends JPanel {
public PastWeather(WeatherStation station){
public PastWeather(WeatherStation station) {
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
Expand All @@ -29,55 +26,54 @@ public PastWeather(WeatherStation station){
JLabel pastWeatherLabel = new JLabel("Past weather: ");
c.gridx = 0;
c.gridy = 0;
add(pastWeatherLabel,c);
add(pastWeatherLabel, c);

//Back button
JButton backButton = new JButton("< Back");
c.gridx = 1;
add(backButton,c);
add(backButton, c);

//Table of weather data
String[] columnNames = { "Reading time", "UV index", "Humidity", "Temperature","Pressure","Rainfall", "Wind speed", "Wind direction"};
String[] columnNames = {"Reading time", "UV index", "Humidity", "Temperature", "Pressure", "Rainfall", "Wind speed", "Wind direction"};
String[][] rowData = new String[0][0];
try{
try {
rowData = getWeatherData(station);
}catch (Exception ex){
} catch (Exception ex) {
ex.printStackTrace();
}
JTable weatherDataTable = new JTable(rowData,columnNames);

JTable weatherDataTable = new JTable(rowData, columnNames);


c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
//Nest inside a JScrollPane so columnNames are displayed
add(new JScrollPane(weatherDataTable),c);
add(new JScrollPane(weatherDataTable), c);

//Event listeners
backButton.addActionListener((e -> Window.showStationViewer(station)));
}

private String[][] getWeatherData(WeatherStation station) throws java.sql.SQLException{
private String[][] getWeatherData(WeatherStation station) throws java.sql.SQLException {
Reading[] readings = database.databaseInterface.getWeatherStationReadings(station);

System.out.printf("Total readings: %s", readings.length);

String[][] readings2D = new String[readings.length][8];

for(int i = 0; i < readings.length; ++i){
System.out.printf("Reading #%s",i);
for (int i = 0; i < readings.length; ++i) {
System.out.printf("Reading #%s", i);
Reading reading = readings[i];
if(reading == null){
if (reading == null) {
continue;
}

System.out.println(reading == null);

String[] readingArray = new String[] {reading.getReadingDate().toString(), String.valueOf(reading.getReadingUVindex()),
String.valueOf(reading.getReadingHumidity()),String.valueOf(reading.getReadingTemperature()),
String.valueOf(reading.getReadingPressure()),String.valueOf(reading.getReadingRainfall()),
String.valueOf(reading.getReadingWindSpeed()), String.valueOf(reading.getReadingWindDirection())};
String[] readingArray = new String[]{reading.getReadingDate().toString(), String.valueOf(reading.getReadingUVindex()),
String.valueOf(reading.getReadingHumidity()), String.valueOf(reading.getReadingTemperature()),
String.valueOf(reading.getReadingPressure()), String.valueOf(reading.getReadingRainfall()),
String.valueOf(reading.getReadingWindSpeed()), String.valueOf(reading.getReadingWindDirection())};

readings2D[i] = readingArray;
}
Expand Down
6 changes: 1 addition & 5 deletions www-weather/src/GUI/Panels/StartScreen.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package GUI.Panels;

import GUI.Window;
import com.sun.glass.ui.Size;
import database.WeatherStation;

import javax.imageio.ImageIO;
import javax.naming.ldap.Control;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

public class StartScreen extends JPanel {
public StartScreen() {
Expand Down Expand Up @@ -81,7 +77,7 @@ public StartScreen() {
//Event listeners
weatherStations.addActionListener((e) -> {
StationViewer.backToMap = false;
Window.showStationViewer(new WeatherStation(1, "test", 1, 0,0));
Window.showStationViewer(new WeatherStation(1, "test", 1, 0, 0));
});

loginButton.addActionListener((e -> {
Expand Down
4 changes: 0 additions & 4 deletions www-weather/src/GUI/Panels/StationChartViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@

import javax.swing.*;
import java.awt.*;
import java.sql.Array;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class StationChartViewer extends JPanel {

Expand Down
31 changes: 14 additions & 17 deletions www-weather/src/GUI/Panels/StationMaintenance.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package GUI.Panels;

import GUI.*;
import GUI.Window;
import database.MaintenanceSchedule;
import database.WeatherStation;
import sun.font.TrueTypeFont;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.ExpandVetoException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand Down Expand Up @@ -47,23 +44,23 @@ public StationMaintenance() {
add(notificationButton);
final JPopupMenu menu = new JPopupMenu("Notification menu");

try{
try {
MaintenanceSchedule[] scheduleList = database.databaseInterface.getMaintenanceSchedules(true);
WeatherStation[] stations = database.databaseInterface.getWeatherStationList();

for(MaintenanceSchedule schedule: scheduleList){
for(WeatherStation station: stations){
if(schedule.getStationID() == station.getStationID()){
JMenuItem item = new JMenuItem(String.format("%s station requires repair on %s",station.getStationName(), schedule.getDate().toString()));
item.addActionListener((e)->{
for (MaintenanceSchedule schedule : scheduleList) {
for (WeatherStation station : stations) {
if (schedule.getStationID() == station.getStationID()) {
JMenuItem item = new JMenuItem(String.format("%s station requires repair on %s", station.getStationName(), schedule.getDate().toString()));
item.addActionListener((e) -> {
String message = String.format("<html><h1>%s Station</h1><br><b>Repair notes: </b>%s<br>Is completed: %s<br><b>Scheduled date: </b>%s</html>",
station.getStationName(),schedule.getNotes(),schedule.isCompleted(),schedule.getDate().toString());
String[] options = new String[] {"Completed", "OK"};
int response = JOptionPane.showOptionDialog(null,message,"title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
station.getStationName(), schedule.getNotes(), schedule.isCompleted(), schedule.getDate().toString());
String[] options = new String[]{"Completed", "OK"};
int response = JOptionPane.showOptionDialog(null, message, "title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
switch (response) {
case 0:
try{
database.databaseInterface.setMaintenanceScheduleCompleted(schedule,true);
try {
database.databaseInterface.setMaintenanceScheduleCompleted(schedule, true);
menu.remove(item);
if (menu.getComponentCount() == 0) {
try {
Expand All @@ -73,7 +70,7 @@ public StationMaintenance() {
ex.printStackTrace();
}
}
}catch (Exception ex){
} catch (Exception ex) {
ex.printStackTrace();
}

Expand All @@ -89,7 +86,7 @@ public StationMaintenance() {
}

}
}catch (Exception ex){
} catch (Exception ex) {

}
if (menu.getComponentCount() > 0) {
Expand All @@ -111,7 +108,7 @@ public void actionPerformed(ActionEvent e) {
stationListPanel.setBackground(Color.cyan);
JScrollPane scrollPanel = new JScrollPane(stationListPanel);
stationListPanel.setAutoscrolls(true);
scrollPanel.setBounds(25, 110, size.width-50, size.height-(150+25));
scrollPanel.setBounds(25, 110, size.width - 50, size.height - (150 + 25));
add(scrollPanel);

populateStationListPanel();
Expand Down
Loading

0 comments on commit 0f393f1

Please sign in to comment.