WeatherForecast / WeatherFrontend.java
WeatherFrontend.java
Raw
// CS400 Project 2
// Name: Ethan Nachreiner
// CSL username: nachreiner
// wisc email: ebnachreiner@wisc.edu

import java.util.Scanner;
import java.util.Date;
import java.util.List;

/**
 * WeatherFrontend class
 * @author ethannachreiner
 *
 */
public class WeatherFrontend implements IWeatherFrontend {
    private Scanner userInput;
    private IWeatherBackend backend;
    private String currentDate;

    WeatherFrontend(Scanner userInput, IWeatherBackend backend) {
        this.userInput = userInput;
        this.backend = backend;
        this.currentDate = backend.getClosestDate();
    }

    char windUnit = 'i'; // 'i' is imperial (mph), 'm' is metric (m/s)
    String windUnitString = "mph";
    char clockFormat = '1'; // '1' is 12hr time, '2' is 24hr time
    boolean clockMarker = true; // includes AM/PM for 12hr time
    char tempUnit = 'f'; // 'f' is Fahrenheit (F), 'c' is Celsius (C)

    @Override
    public void runCommandLoop() {
        System.out.println("Welcome to the Weather Forcecast Application!");
        System.out.println("<><><><><><><><><><><><><><><><><>");
        
        displayMainMenu();

        String input = this.userInput.nextLine();
        while (!input.equals("6")) {    // Input "6" closes application
            switch (input) {
                case "1":               // Input "1" shows current weather
                    IForecast currentForecast = backend.getAverage(backend.getDay(currentDate));
                    System.out.println("The weather on " + currentForecast.getDate() + " from " + currentForecast.getStartTime(clockFormat, true, false) + " to " + currentForecast.getEndTime(clockFormat, true, clockMarker) + " is " + currentForecast.getDescription() + " with a temperature of " + Double.toString(currentForecast.getAverageTemp(tempUnit)) + " degrees " + Character.toUpperCase(tempUnit) + " (high " + Double.toString(currentForecast.getMaxTemp(tempUnit)) + ", low " + Double.toString(currentForecast.getMinTemp(tempUnit)) + "). The humidity is " + Integer.toString(currentForecast.getHumidityPercentage()) + "% with a wind speed of " + Double.toString(currentForecast.getWindSpeed(windUnit)) + " " + windUnitString + " (" + currentForecast.getWindDirection() + ").");
                    break;
                case "2":               // Input "2" shows weather for today
                    List<IForecast> dailyForecasts = backend.getDay(currentDate);
                    displayDailyForecasts(dailyForecasts);
                    break;
                case "3":               // Input "3" shows weather for week
                    List<IForecast> weeklyForecasts = backend.getAverageWeek();
                    displayWeeklyForecasts(weeklyForecasts);
                    break;
                case "4":               // Input "4" shows hottest day of next week
                    IForecast hottestDay = backend.getHottestDay(backend.getAverageWeek());
                    displayWarmestDayNextWeek(hottestDay);
                    break;
                case "5":               // Input "5" displays menu to change scales
                    displayScalesMenu();
                    input = userInput.nextLine();
                    while (!input.equals("4")) {    // Input "4" returns to main menu
                        switch (input) {
                            case "1":               // Input "1" changes temperature unit
                                if (tempUnit == 'f') {
                                    tempUnit = 'c';
                                } else {
                                    tempUnit = 'f';
                                }
                                break;
                            case "2":               // Input "2" chances clock format
                                if (clockFormat == '1') {
                                    clockFormat = '2';
                                    clockMarker = false;
                                } else {
                                    clockFormat = '1';
                                    clockMarker = true;
                                }
                                break;
                            case "3":               // Input "3" chances wind speed unit
                                if (windUnit == 'i') {
                                    windUnit = 'm';
                                    windUnitString = "m/s";
                                } else {
                                    windUnit = 'i';
                                    windUnitString = "mph";
                                }
                                break;
                            default:
                                System.out.println("Invalid input, please try again.");
                        }
                        displayScalesMenu();
                        input = userInput.nextLine();
                    }
                    break;
                default:
                    System.out.println("Invalid input, please try again.");
            }
            displayMainMenu();
            input = userInput.nextLine();
        }
    }

    @Override
    public void displayDailyForecasts(List<IForecast> dailyForecasts) {
        IForecast forecast = backend.getAverage(dailyForecasts);
        System.out.println("Weather for " + forecast.getDate());
        System.out.println("Average | " + forecast.getDescription() + ", " + Double.toString(forecast.getAverageTemp(tempUnit)) + " degrees " + Character.toUpperCase(tempUnit) + " (high " + Double.toString(forecast.getMaxTemp(tempUnit)) + ", low " + Double.toString(forecast.getMinTemp(tempUnit)) + "). " + Integer.toString(forecast.getHumidityPercentage()) + "% humidity, wind " + Double.toString(forecast.getWindSpeed(windUnit)) + " " + windUnitString + "(" + forecast.getWindDirection() + ").");

        for (int i = 0; i < dailyForecasts.size(); i++) {
            forecast = dailyForecasts.get(i);
            System.out.println(forecast.getStartTime(clockFormat, false, false) + "-" + forecast.getEndTime(clockFormat, true, clockMarker) + " | " + forecast.getDescription() + ", " + Double.toString(forecast.getAverageTemp(tempUnit)) + " degrees " + Character.toUpperCase(tempUnit) + " (high " + Double.toString(forecast.getMaxTemp(tempUnit)) + ", low " + Double.toString(forecast.getMinTemp(tempUnit)) + "). " + Integer.toString(forecast.getHumidityPercentage()) + "% humidity, wind " + Double.toString(forecast.getWindSpeed(windUnit)) + " " + windUnitString + "(" + forecast.getWindDirection() + ").");
        }
    }

    @Override
    public void displayWeeklyForecasts(List<IForecast> weeklyForecasts) {
        System.out.println("Weather for this week (showing averages)");

        for (int i = 0; i < weeklyForecasts.size(); i++) {
            IForecast forecast = weeklyForecasts.get(i);
            System.out.println(forecast.getDate() + " | " + forecast.getDescription() + ", " + Double.toString(forecast.getAverageTemp(tempUnit)) + " degrees " + Character.toUpperCase(tempUnit) + " (high " + Double.toString(forecast.getMaxTemp(tempUnit)) + ", low " + Double.toString(forecast.getMinTemp(tempUnit)) + "). " + Integer.toString(forecast.getHumidityPercentage()) + "% humidity, wind " + Double.toString(forecast.getWindSpeed(windUnit)) + " " + windUnitString + "(" + forecast.getWindDirection() + ").");
        }
    }

    @Override
    public void displayWarmestDayNextWeek(IForecast hottestDay) {
        System.out.println("The warmest day next week will be " + hottestDay.getDate() + ". It will have " + hottestDay.getDescription() + " and a temperature of " + Double.toString(hottestDay.getAverageTemp(tempUnit)) + " degrees " + Character.toUpperCase(tempUnit) + " (high " + Double.toString(hottestDay.getMaxTemp(tempUnit)) + ", low " + Double.toString(hottestDay.getMinTemp(tempUnit)) + "). The humidity will be " + Integer.toString(hottestDay.getHumidityPercentage()) + "% with a wind speed of " + Double.toString(hottestDay.getWindSpeed(windUnit)) + " " + windUnitString + " (" + hottestDay.getWindDirection() + ").");
    }

    @Override
    public void displayScalesMenu() {
        System.out.println("\nScale switching menu (input number to switch):");
        if (tempUnit == 'f') {
            System.out.println("1.    Switch to Celcius (currently Fahrenheit)");
        } else {
            System.out.println("1.    Switch to Fahrenheit (currently Celcius)");
        }
        if (clockFormat == '1') {
            System.out.println("2.    Switch to 24-hr time (currently 12-hr time)");
        } else {
            System.out.println("2.    Switch to 12-hr time (currently 24-hr time)");
        }
        if (windUnit == 'i') {
            System.out.println("3.    Switch to meters per second (currently mph)");
        } else {
            System.out.println("3.    Switch to miles per hour (currently m/s)");
        }
        System.out.println("4.    Return to main menu");
    }

    @Override
    public void displayMainMenu() {
        System.out.println("\nMain menu (input number to select):");
        System.out.println("1.    Show current weather");
        System.out.println("2.    Show weather for today");
        System.out.println("3.    Show the weather for this week");
        System.out.println("4.    Show the warmest day next week");
        System.out.println("5.    Switch scales (currently " + Character.toUpperCase(tempUnit) + ", " + clockFormat + Double.toString(Character.getNumericValue(clockFormat) * 2) + "-hr time, " + windUnitString + ")");
        System.out.println("6.    Exit application");
    }
}