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 WeatherDataString date, int highTemperature, int lowTemperature, String weatherCondition
this.date date;
this.highTemperature highTemperature;
this.lowTemperature lowTemperature;
this.weatherCondition weatherCondition;
class DailyWeatherData extends WeatherData
public DailyWeatherDataString date, int highTemperature, int lowTemperature, String weatherCondition
superdate highTemperature, lowTemperature, weatherCondition;
class WeatherDataAnalyzer
private ArrayList weatherDataList;
public WeatherDataAnalyzer
weatherDataList new ArrayList;
public void addWeatherDataWeatherData weatherData
weatherDataList.addweatherData;
public void showStatsForLastDays
showStatsForNDays;
public void showStatsForLastDays
showStatsForNDays;
public void showStatsForLastDays
showStatsForNDays;
private void showStatsForNDaysint n
if weatherDataListisEmpty
System.out.printlnNo weather data available.";
return;
if weatherDataListsize n
System.out.printlnInsufficient data for n days.";
return;
List lastNDaysData weatherDataList.subListweatherDataListsize n weatherDataList.size;
int totalHighTemp ;
int totalLowTemp ;
int sunnyDays ;
int rainyDays ;
int cloudyDays ;
int windyDays ;
for WeatherData data : lastNDaysData
totalHighTemp data.highTemperature;
totalLowTemp data.lowTemperature;
switch dataweatherCondition
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.printlnAverage High Temperature: averageHighTemp;
System.out.printlnAverage Low Temperature: averageLowTemp;
System.out.printlnSunny Days: sunnyDays;
System.out.printlnRainy Days: rainyDays;
System.out.printlnCloudy Days: cloudyDays;
System.out.printlnWindy Days: windyDays;
public class Main
public static void mainString args
WeatherDataAnalyzer analyzer new WeatherDataAnalyzer;
Scanner scanner new ScannerSystemin;
while true
System.out.printlnEnter the date YYYYMMDD:;
String date scanner.nextLine;
System.out.printlnEnter the high temperature:";
int highTemp scanner.nextInt;
System.out.printlnEnter the low temperature:";
int lowTemp scanner.nextInt;
scanner.nextLine; consume the newline character
System.out.printlnEnter the weather condition sunny rainy, cloudy, windy:;
String condition scanner.nextLine;
DailyWeatherData dailyData new DailyWeatherDatadate highTemp, lowTemp, condition;
analyzer.addWeatherDatadailyData;
System.out.printlnDo you want to quit? yesno;
String quit scanner.nextLine;
if yesequalsIgnoreCasequit
break;
System.out.printlnWould you like to pull statistics? days or no;
String statsOption scanner.nextLine;
switch statsOption
case :
analyzer.showStatsForLastDays;
break;
case :
analyzer.showStatsForLastDays;
break;
case :
analyzer.showStatsForLastDays;
break;
default:
System.out.printlnContinuing data collection...";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
