// Arup Guha
// 4/10/2026
// Program to illustrate file input - Weather Data

/*** Edited on 11/9/2026 in class to show an example where we
     try to ask some questions and get answers from an input file
	 with data.
***/

import java.util.*;
import java.io.*;

public class weather_inclass {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		System.out.println("Please enter your weather file.");
		String filename = stdin.next();
		Scanner fin = null;
		
		// So we get a valid file.
		while (true) {
		
			// Try opening the file.
			try {
				fin = new Scanner(new File(filename));
			}
			catch (FileNotFoundException e) {
				System.out.println("Sorry, that file wasn't found. Try again.");
				filename = stdin.next();
			}
			
			// We're good.
			if (fin != null) break;
		}
		
		// Create the object, close the file.
		city mine = new city(fin);
		fin.close();
		
		// Print it!
		System.out.println(mine);

		/*** Here is where we added stuff. The first method prints a ton which
		     is why it's commented out.
		***/
		//mine.printAllBigChanges(10.0);
		System.out.println("Biggest single dat change was "+mine.getMaxChange());
		
		// These were relatively straight-forward edits.
		mine.printReading(mine.getIndexOfHighestTemp());
		mine.printReading(mine.getIndexOfLowestTemp());
		
		// This took a bit more finagling.
		mine.printMonthlyVolatility();
	}
}

class city {

	final private static int TOTALYEARS = 2027;
	final private static int NUMMONTHS = 12;
	
	private double[] yearSum;
	private int[] yearNumReadings;
	private double[] monthSum;
	private int[] monthNumReadings;
	
	/*** Added these. ***/
	// Will put each reading here.
	private ArrayList<Double> allReadings;
	private ArrayList<MyDate> allDates;
	
	// Constructor must be given a Scanner object pointing to the beginning
	// of a file with the appropriate format.
	public city(Scanner fin) {
		
		// Using indexes as actual years and months.
		yearSum = new double[TOTALYEARS];
		yearNumReadings = new int[TOTALYEARS];
		monthSum = new double[NUMMONTHS+1];
		monthNumReadings = new int[NUMMONTHS+1];
		allReadings = new ArrayList<Double>();
		allDates = new ArrayList<MyDate>();
		
		// Go through all lines.
		while (fin.hasNextLine()) {
			
			// Get the next line and tokenize it.
			StringTokenizer tok = new StringTokenizer(fin.nextLine());
			
			// Get everything from the tokenizer.
			int month = Integer.parseInt(tok.nextToken());
			int day = Integer.parseInt(tok.nextToken());
			int year = Integer.parseInt(tok.nextToken());
			double temp = Double.parseDouble(tok.nextToken());
			
			// File uses -99 to indicate no reading that day.
			if (temp > -98) {
			
				// Update our four accumulators.
				yearSum[year] += temp;
				monthSum[month] += temp;
				yearNumReadings[year]++;
				monthNumReadings[month]++;
				
				/*** Added this so we store daily readings. ***/
				allReadings.add(temp);
				allDates.add(new MyDate(year, month, day));
			
			}
		}
	}
	
	/*** Added this methid ***/
	// Prints all consecutive days when the absolute vaue of the change in temperature
	// exceeds delta.
	public void printAllBigChanges(double delta) {
		
		// Looking at consecutive pairs of days.
		for (int i=0; i<allReadings.size()-1; i++) {
			
			// Change in temperature.
			double diff = Math.abs(allReadings.get(i+1)-allReadings.get(i));
			
			// This is bigger than delta so print it!
			if (diff > delta) {
				System.out.print("From "+allDates.get(i)+" temp was "+allReadings.get(i));
				System.out.println(" to "+ allDates.get(i+1)+" temp was "+allReadings.get(i+1));
			}
		}
	}
	
	/*** Added this ***/
	// Prints all consecutive days when the absolute vaue of the change in temperature
	// exceeds delta.
	public double getMaxChange() {
		
		// Store best answer here.
		double res = 0;
		
		// Same loop as previous method.
		for (int i=0; i<allReadings.size()-1; i++) {
			
			// This is the same, but no printing just update res as needed.
			double diff = Math.abs(allReadings.get(i+1)-allReadings.get(i));
			res = Math.max(res, diff);
		}
		
		return res;
	}
	
	/*** Added this ***/
	// Gets the index into the ArrayList of the highest temperature.
	public int getIndexOfHighestTemp() {
		
		// Initially store index 0, update as needed.
		int res = 0;
		for (int i=1; i<allReadings.size(); i++)
			if (allReadings.get(i) > allReadings.get(res))
				res = i;
			
		return res;
	}
	
	/*** Added this. ***/
	// Does same but for lowest temp as method above.
	public int getIndexOfLowestTemp() {
		
		int res = 0;
		for (int i=1; i<allReadings.size(); i++)
			if (allReadings.get(i) < allReadings.get(res))
				res = i;
			
		return res;
	}
	
	/*** Added ***/
	// Prints out the data at the index idx.
	public void printReading(int idx) {
		System.out.println(allDates.get(idx)+": "+allReadings.get(idx));
	}
	
	/*** Added ***/
	public void printMonthlyVolatility() {
		
		// My start index for a streak.
		int i = 0;
		
		// Till I am done.
		while (i < allReadings.size()) {
			
			// i is start, j is end, exclusive.
			int j = i;
			while (j<allReadings.size() && allDates.get(i).sameMonthYear(allDates.get(j)))
				j++;
			
			// Add number of difference readings and sum of differences.
			double sumDiff = 0;
			int cnt = 0;
			for (int z=i; z<j-1; z++) {
				sumDiff += Math.abs(allReadings.get(z+1)-allReadings.get(z));
				cnt++;
			}
			
			// Get the average volatility for this month and print it.
			double avgVolatility = sumDiff/cnt;
			System.out.println(allDates.get(i).getMonth()+"/"+allDates.get(i).getYear()+": "+avgVolatility);
			
			// Go to next interval.
			i = j;
		}
	}
	
	// Returns a string representation of our object.
	public String toString() {
	
		// This is what our chart will have in the beginning.
		String chart = "Year\tAvg Temp\n";
		for (int i=0; i<yearSum.length; i++) {
		
			// Skip.
			if (yearNumReadings[i] == 0) continue;
		
			// Add this row.
			chart = chart + i + "\t" + (yearSum[i]/yearNumReadings[i]) + "\n";
		}
		
		// Put a bit of separation.
		chart = chart + "\n\n";
		chart = chart + "Month\tAvg Temp\n";
		
		// Now go through the months.
		for (int i=1; i<=NUMMONTHS; i++) {
			
			// Skip.
			if (monthNumReadings[i] == 0) continue;
			
			// Add this row.
			chart = chart + i + "\t" + (monthSum[i]/monthNumReadings[i]) + "\n";
		}
		
		return chart;
	}
	
}

/*** Added this because Java's seems annoying. ***/
class MyDate {
	
	private int year;
	private int month;
	private int day;
	
	public MyDate(int myYear, int myMonth, int myDay) {
		year = myYear;
		month = myMonth;
		day = myDay;
	}
	
	public int getMonth() {
		return month;
	}
	
	public int getYear() {
		return year;
	}
	
	public String toString() {
		return month+"/"+day+"/"+year;
	}
	
	public boolean sameMonthYear(MyDate other) {
		return year == other.year && month == other.month;
	}
}