/** * Instantiable implementation of the IForecast interface. See IForecast for Javadoc. * * @author Tommy F */ public class Forecast implements IForecast { // Class variables private int timecode; private double avgTempKelvin; private double minTempKelvin; private double maxTempKelvin; private String description; private int humidity; private double windSpeed; private int windDirectionDeg; private String timeString; // Constructor public Forecast(int tc, double avgT, double minT, double maxT, String desc, int h, double ws, int wd) { this.timecode = tc; this.avgTempKelvin = avgT; this.minTempKelvin = minT; this.maxTempKelvin = maxT; this.description = desc; this.humidity = h; this.windSpeed = ws; this.windDirectionDeg = wd; this.timeString = "" + timecode; } @Override public int compareTo(IForecast o) { int otherTimecode = o.getTimeCode(); int otherHour = Integer.parseInt(("" + otherTimecode).substring(8, 10)); int thisHour = Integer.parseInt("" + timeString.substring(8, 10)); int otherDay = Integer.parseInt(("" + otherTimecode).substring(6, 8)); int thisDay = Integer.parseInt("" + timeString.substring(6, 8)); return (thisHour + (thisDay * 24)) - (otherHour + (otherDay * 24)); } @Override public String getDate() { String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String s = ""; s += months[Integer.parseInt(timeString.substring(4, 6)) - 1] + " " + timeString.substring(6, 8) + ", " + timeString.substring(0, 4); return s; } @Override public String getShortDate() { String[] shortMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; String s = ""; if (timeString.charAt(6) != '0') { s += shortMonths[Integer.parseInt(timeString.substring(4, 6)) - 1] + " " + timeString.substring(6, 8) + ", " + timeString.substring(0, 4); } else { s += shortMonths[Integer.parseInt(timeString.substring(4, 6)) - 1] + " " + timeString.substring(7, 8) + ", " + timeString.substring(0, 4); } return s; } @Override public String getStartTime(char format, boolean minutes, boolean marker) throws IllegalArgumentException { String s = ""; if (format == '2') { s += timeString.substring(8, 10); } else if (format == '1') { if (timeString.charAt(8) == '0') { if (timeString.charAt(9) == '0') { s += "12"; } else { s += timeString.substring(9, 10); } } else { if (Integer.parseInt(timeString.substring(8, 10)) > 12) { s += Integer.parseInt(timeString.substring(8, 10)) - 12; } else { s += timeString.substring(8, 10); } } } else { throw new IllegalArgumentException("getStartTime: invalid input for time format"); } if (minutes == true) { s += ":00"; } if (marker == true) { if (Integer.parseInt(timeString.substring(8, 10)) < 12) { s += " AM"; } else { s += " PM"; } } return s; } @Override public String getEndTime(char format, boolean minutes, boolean marker) throws IllegalArgumentException { int endTimecode = this.timecode + 3; String endTimeString = "" + endTimecode; String s = ""; if (format == '2') { if (endTimeString.contains("24")) { s += "00"; } else { s += endTimeString.substring(8, 10); } } else if (format == '1') { if (endTimeString.charAt(8) == '0') { if (endTimeString.charAt(9) == '0') { s += "12"; } else { s += endTimeString.substring(9, 10); } } else if (endTimeString.charAt(8) == '1') { if (Integer.parseInt(endTimeString.substring(8, 10)) > 12) { s += Integer.parseInt(endTimeString.substring(8, 10)) - 12; } else { s += endTimeString.substring(8, 10); } } else if (endTimeString.contains("24")) { s += "12"; } else { s += Integer.parseInt(endTimeString.substring(8, 10)) - 12; } } else { throw new IllegalArgumentException("getEndTime: invalid input for time format"); } if (minutes == true) { s += ":00"; } if (marker == true) { if (Integer.parseInt(endTimeString.substring(8, 10)) < 12 || Integer.parseInt(endTimeString.substring(8, 10)) == 24) { s += " AM"; } else { s += " PM"; } } return s; } @Override public int getTimeCode() { return this.timecode; } @Override public String getDescription() { return this.description; } @Override public double getAverageTemp(char unit) throws IllegalArgumentException { double avgTemp; if (unit == 'F' || unit == 'f') { avgTemp = (this.avgTempKelvin - 273.15) * (9.0 / 5.0) + 32; } else if (unit == 'C' || unit == 'c') { avgTemp = this.avgTempKelvin - 273.15; } else { //throw new IllegalArgumentException("getAverageTemp: invalid input for unit format"); avgTemp = this.avgTempKelvin; } return (Math.round(avgTemp * 10)) / 10.00; } @Override public double getMaxTemp(char unit) throws IllegalArgumentException { double maxTemp; if (unit == 'F' || unit == 'f') { maxTemp = (this.maxTempKelvin - 273.15) * (9.0 / 5.0) + 32; } else if (unit == 'C' || unit == 'c') { maxTemp = this.maxTempKelvin - 273.15; } else { //throw new IllegalArgumentException("getMaxTemp: invalid input for unit format"); maxTemp = this.maxTempKelvin; } return (Math.round(maxTemp * 10)) / 10.00; } @Override public double getMinTemp(char unit) throws IllegalArgumentException { double minTemp; if (unit == 'F' || unit == 'f') { minTemp = (this.minTempKelvin - 273.15) * (9.0 / 5.0) + 32; } else if (unit == 'C' || unit == 'c') { minTemp = this.minTempKelvin - 273.15; } else { //throw new IllegalArgumentException("getMinTemp: invalid input for unit format"); minTemp = this.minTempKelvin; } return (Math.round(minTemp * 10)) / 10.00; } @Override public int getHumidityPercentage() { return this.humidity; } @Override public String getWindDirection() { if (windDirectionDeg > 338 || windDirectionDeg < 23) { return "N"; } else if (windDirectionDeg > 23 && windDirectionDeg < 68) { return "NE"; } else if (windDirectionDeg > 68 && windDirectionDeg < 113) { return "E"; } else if (windDirectionDeg > 113 && windDirectionDeg < 158) { return "SE"; } else if (windDirectionDeg > 158 && windDirectionDeg < 203) { return "S"; } else if (windDirectionDeg > 203 && windDirectionDeg < 248) { return "SW"; } else if (windDirectionDeg > 248 && windDirectionDeg < 293) { return "W"; } else { return "NW"; } } @Override public double getWindSpeed(char unit) throws IllegalArgumentException { double speed; if (unit == 'i' || unit == 'I') { speed = (this.windSpeed * 2.237); } else if (unit == 'm' || unit == 'M') { speed = this.windSpeed; } else { throw new IllegalArgumentException("getWindSpeed: invalid input for unit format"); } return (Math.round(speed * 10)) / 10.00; } }