Question: create an input file for the following java code, allowing the scanners to accept the input file to use as example inputs - - -

create an input file for the following java code, allowing the scanners to accept the input file to use as example inputs
--------
import java.io.*;
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;
private File dataFile = new File("weather_data.txt");
public WeatherDataAnalyzer(){
weatherDataList = new ArrayList<>();
loadDataFromFile();
}
private void loadDataFromFile(){
if (dataFile.exists()){
try (BufferedReader reader = new BufferedReader(new FileReader(dataFile))){
String line;
while ((line = reader.readLine())!= null){
String[] data = line.split(",");
}
} catch (IOException e){
System.out.println("Failed to read from file: "+ e.getMessage());
}
}
}
public void addWeatherData(WeatherData weatherData){
weatherDataList.add(weatherData);
appendDataToFile(weatherData);
}
private void appendDataToFile(WeatherData weatherData){
try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataFile, true))){
writer.write(weatherData.date +","+ weatherData.highTemperature +","+ weatherData.lowTemperature +","+ weatherData.weatherCondition);
writer.newLine();
} catch (IOException e){
System.out.println("Failed to write to file: "+ e.getMessage());
}
}
// Existing methods for showing statistics...
}
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...");
}
}
scanner.close();
}
}

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!