OpenDataPhillyFinal / src / edu / upenn / cit594 / ui / UserInterface.java
UserInterface.java
Raw
package edu.upenn.cit594.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

import edu.upenn.cit594.logging.Logger;
import edu.upenn.cit594.processor.*;
import edu.upenn.cit594.util.CovidData;
import edu.upenn.cit594.util.Population;
import edu.upenn.cit594.util.Property;

public class UserInterface {

	// protected Processor processor;
	protected Processor processor;

	public UserInterface(Processor processor) {
		// this.processor = processor;
		this.processor = processor;
	}
	
	private ArrayList<CovidData> covidData = null;
	private ArrayList<Property> propData =null;
	private ArrayList<Population> popData=null;

	/*
	 * Function to print strings
	 */
	public static void print(String str) {
		System.out.println(str);
	}
	
	public void UIProcess(Processor processor) {
		this.covidData = processor.getcovidDataObj();
		this.popData = processor.getpopulationObj();
		this.propData = processor.getpropertyObj();;
		
		startUI(this.popData, this.covidData, this.propData);
	}

	public static void startUI(ArrayList<Population> popData, ArrayList<CovidData> covidData, ArrayList<Property> propData) {
		Scanner scanner = new Scanner(System.in);
		int choice = -1;

		while (choice != 0) {
			System.out.println("0. Exit the program.");
			System.out.println("1. Show the available actions.");
			System.out.println("2. Show the total population for all ZIP Codes.");
			System.out.println("3. Show the total vaccinations per capita for each ZIP Code for the specified date.");
			System.out.println("4. Show the average market value for properties in a specified ZIP Code.");
			System.out.println("5. Show the average total livable area for properties in a specified ZIP Code.");
			System.out.println("6. Show the total market value of properties, per capita, for a specified ZIP Code.");
			System.out.println("7. Show the results of total market value per vaccination per capita ratio (custom feature).");
			System.out.print("> ");
			try {
				choice = Integer.parseInt(scanner.nextLine());
				//log user input
				Logger.getInstance().log(String.valueOf(choice));
			} catch (NumberFormatException e) {
				System.out.println("Invalid input. Please enter an integer between 0-7.");
				continue;
			}
			switch (choice) {
			case 0:
				System.out.println("Exiting program.");
				scanner.close();
				break;
			case 1:
				Set<Integer> set = new HashSet<Integer>();
				set.add(0);
				set.add(1);
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				
				if (popData != null) {
					set.add(2);
				}
				if (popData != null && covidData != null) {
					set.add(3);
				}
				if (propData !=null)  {
					set.add(4);
					set.add(5);
				}
				if(popData != null && propData != null) {
					set.add(6);
				}
				if (popData != null && covidData != null && propData !=null) {
					set.add(7);
				}
			
				//print available actions
				for (int i : set) {
					System.out.println(i);
				}
				System.out.println("END OUTPUT");
				break;
			case 2:
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				//If data not available: print a message and re-prompt the entire menu
				if (popData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				int totalPop = totalPopulation.getTotalPopulation(popData);
				
				System.out.println(totalPop);
			
				System.out.println("END OUTPUT");
				System.out.flush();
				break;
			case 3:
				//If data not available: print a message and re-prompt the entire menu
				if (popData == null || covidData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				System.out.print("Please enter 'partial' or 'full': ");
				String partOrFull = scanner.nextLine();
				if (!partOrFull.equals("partial") && !partOrFull.equals("full")) {
					System.out.println("Invalid input. Please enter 'partial' or 'full'.");
					break;
				}
				//log input partOrFull
				Logger.getInstance().log(partOrFull);
				
				System.out.print("Please enter a date in the format 'YYYY-MM-DD': ");
				String timeStamp = scanner.nextLine();
				if (!timeStamp.matches("\\d{4}-\\d{2}-\\d{2}")) {
					System.out.println("Invalid date format. Please enter a date in the format 'YYYY-MM-DD'.");
					break;
				}
				//log input timestamp
				Logger.getInstance().log(timeStamp);
				// Step 3: Create a cache object
				HashMap<String, TreeMap<Integer, Double>> cache = new HashMap<>();

				// Step 4 and 5: Modify the code to use memoization
				String key = timeStamp + "_" + partOrFull;
				TreeMap<Integer, Double> vaxPerCapita;
				if (cache.containsKey(key)) {
				    vaxPerCapita = cache.get(key);
				} else {
				    HashMap<Integer, Integer> vaxData = totalVaxPerCapita.getVax(timeStamp, partOrFull, covidData);
				    vaxPerCapita = totalVaxPerCapita.getVaxPerCapita(vaxData, popData);
				    cache.put(key, vaxPerCapita);
				}

				// Step 6: Print the results
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				for (Entry<Integer, Double> entry : vaxPerCapita.entrySet()) {
				    //log output vax per capita
				    Logger.getInstance().log(String.valueOf(entry.getKey() + " " + String.valueOf(entry.getValue())));

				    System.out.println(entry.getKey() + " " + entry.getValue());
				}
				System.out.println("END OUTPUT");
				System.out.flush();

				break;
			case 4:
				//If data not available: print a message and re-prompt the entire menu
				if (propData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				System.out.print("Please enter a ZIP Code: ");
				String zipCode = scanner.nextLine();
				if (!zipCode.matches("\\d{5}")) {
					System.out.println("Invalid ZIP Code format. Please enter a ZIP Code in");
					break;
				}
				
				//log input avgMarketValue
				Logger.getInstance().log(zipCode);
				
				int castZipCode2 = Integer.valueOf(zipCode);
				AverageMarketValue avgMarketValue = new AverageMarketValue();
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				int avgMarketValueFinal = avgMarketValue.calculateAverage(castZipCode2, propData);
				//log output averageMarketValue
				//Logger.getInstance().log(String.valueOf(avgMarketValueFinal));
				
				System.out.println(avgMarketValueFinal);
				System.out.println("END OUTPUT");
				System.out.flush();
				break;
			case 5:
				//If data not available: print a message and re-prompt the entire menu
				if (propData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				System.out.print("Please enter a ZIP Code: ");
				String zipCode1 = scanner.nextLine();
				if (!zipCode1.matches("\\d{5}")) {
					System.out.println("Invalid ZIP Code format. Please enter a ZIP Code in the format '#####'.");
					break;
				}
				
				//log input avgTotalLivableArea
				Logger.getInstance().log(zipCode1);
				
				int castZipCode = Integer.valueOf(zipCode1);
				AverageTotalLivableArea avgLivableArea = new AverageTotalLivableArea();
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
			
				int avgTotalLivableArea = avgLivableArea.calculateAverage(castZipCode, propData);
				
				//log output  avg total livable area
				//Logger.getInstance().log(String.valueOf(avgTotalLivableArea));
				
				System.out.println(avgTotalLivableArea);
				System.out.println("END OUTPUT");
				System.out.flush();
				break;
			case 6:
				//If data not available: print a message and re-prompt the entire menu
				if (propData == null || popData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				System.out.print("Please enter a ZIP Code: ");
				String zipCode11 = scanner.nextLine();
				if (!zipCode11.matches("\\d{5}")) {
					System.out.println("Invalid ZIP Code format. Please enter a ZIP Code in the format '#####'.");
					break;
				}
				
				//log input totalMarket
				Logger.getInstance().log(zipCode11);
				
				
				double totalMarket = totalMarketPerCapita.getTotalMarketPerCapita(zipCode11, propData, popData);
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				
				//log output
				//Logger.getInstance().log(String.valueOf(totalMarket));
				System.out.println(totalMarket);
				System.out.println("END OUTPUT");
				System.out.flush();
				break;
			case 7:
				//If data not available: print a message and re-prompt the entire menu
				if (propData == null || popData == null || covidData == null) {
					System.out.println("Sorry, this data is not available. Please select another option.");
					break;
				}
				System.out.print("Please enter a ZIP Code: ");
				String zipCodeAddFeature = scanner.nextLine();
				if (!zipCodeAddFeature.matches("\\d{5}")) {
					System.out.println("Invalid ZIP Code format. Please enter a ZIP Code in the format '#####'.");
					break;
				}
				
				//log input totalMarket
				Logger.getInstance().log(zipCodeAddFeature);
				
				int castZipCodeAddFeature = Integer.valueOf(zipCodeAddFeature);
				System.out.println();
				System.out.println("\nBEGIN OUTPUT");
				String totalMarketValuePerVaxPerCapita = 		
				TotalMarketValuePerVaccinationPerCapita.calculateTotalMarketValuePerVaccinationPerCapita(castZipCodeAddFeature, 
								covidData, popData, propData);
				System.out.println(totalMarketValuePerVaxPerCapita);
				System.out.println("END OUTPUT");
				System.out.flush();
			
			}

		}
	}

}