Question: using the following code, add the scanner input into a file, and if the file already exists on the computer, open it and allow the

using the following code, add the scanner input into a file, and if the file already exists on the computer, open it and allow the user to access information from past entries.
---------
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class WeatherData {
protected String date;
protected int highTemperature;
protected int lowTemperature;
protected String weatherCondition;
public WeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition){
this.date = date;
this.highTemperature = highTemperature;
this.lowTemperature = lowTemperature;
this.weatherCondition = weatherCondition;
}
}
class DailyWeatherData extends WeatherData {
public DailyWeatherData(String date, int highTemperature, int lowTemperature, String weatherCondition){
super(date, highTemperature, lowTemperature, weatherCondition);
}
}
class WeatherDataAnalyzer {
private ArrayList weatherDataList;
public WeatherDataAnalyzer(){
weatherDataList = new ArrayList<>();
}
public void addWeatherData(WeatherData weatherData){
weatherDataList.add(weatherData);
}
public void showStatsForLast7Days(){
showStatsForNDays(7);
}
public void showStatsForLast14Days(){
showStatsForNDays(14);
}
public void showStatsForLast30Days(){
showStatsForNDays(30);
}
private void showStatsForNDays(int n){
if (weatherDataList.isEmpty()){
System.out.println("No weather data available.");
return;
}
if (weatherDataList.size()< n){
System.out.println("Insufficient data for "+ n +" days.");
return;
}
List lastNDaysData = weatherDataList.subList(weatherDataList.size()- n, weatherDataList.size());
int totalHighTemp =0;
int totalLowTemp =0;
int sunnyDays =0;
int rainyDays =0;
int cloudyDays =0;
int windyDays =0;
for (WeatherData data : lastNDaysData){
totalHighTemp += data.highTemperature;
totalLowTemp += data.lowTemperature;
switch (data.weatherCondition){
case "sunny":
sunnyDays++;
break;
case "rainy":
rainyDays++;
break;
case "cloudy":
cloudyDays++;
break;
case "windy":
windyDays++;
break;
}
}
double averageHighTemp =(double) totalHighTemp / n;
double averageLowTemp =(double) totalLowTemp / n;
System.out.println("Average High Temperature: "+ averageHighTemp);
System.out.println("Average Low Temperature: "+ averageLowTemp);
System.out.println("Sunny Days: "+ sunnyDays);
System.out.println("Rainy Days: "+ rainyDays);
System.out.println("Cloudy Days: "+ cloudyDays);
System.out.println("Windy Days: "+ windyDays);
}
}
public class Main {
public static void main(String[] args){
WeatherDataAnalyzer analyzer = new WeatherDataAnalyzer();
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Enter the date (YYYY-MM-DD):");
String date = scanner.nextLine();
System.out.println("Enter the high temperature:");
int highTemp = scanner.nextInt();
System.out.println("Enter the low temperature:");
int lowTemp = scanner.nextInt();
scanner.nextLine(); // consume the newline character
System.out.println("Enter the weather condition (sunny, rainy, cloudy, windy):");
String condition = scanner.nextLine();
DailyWeatherData dailyData = new DailyWeatherData(date, highTemp, lowTemp, condition);
analyzer.addWeatherData(dailyData);
System.out.println("Do you want to quit? (yes/no)");
String quit = scanner.nextLine();
if ("yes".equalsIgnoreCase(quit)){
break;
}
System.out.println("Would you like to pull statistics? (7,14,30 days or no)");
String statsOption = scanner.nextLine();
switch (statsOption){
case "7":
analyzer.showStatsForLast7Days();
break;
case "14":
analyzer.showStatsForLast14Days();
break;
case "30":
analyzer.showStatsForLast30Days();
break;
default:
System.out.println("Continuing data collection...");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!