import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class Co2Data { private Date date; private int time; private int co2Level; // Constructor public Co2Data(Date date, int co2Level) { this.date = date; this.co2Level = co2Level; this.time = date.getHour() * 100 + date.getMinute(); } // Getters and Setters public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getCo2Level() { return co2Level; } public void setCo2Level(int co2Level) { this.co2Level = co2Level; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } // Fetch and parse data from a CSV URL public static List getData(URL url) { List dataList = new ArrayList<>(); try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "text/csv"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); br.readLine(); // Skip header line String output; System.out.println(url); while ((output = br.readLine()) != null) { Co2Data data = parseData(output); if (data != null) { dataList.add(data); } } conn.disconnect(); } catch (Exception e) { System.out.println("Error in data fetching: " + e); } return dataList; } // Helper method to parse CSV data and create a Co2Data object private static Co2Data parseData(String line) { try { String[] parts = line.split(","); String dateStr = parts[0].trim(); // assuming date is in the first column int co2Level = Integer.parseInt(parts[1].trim()); // assuming CO2 level is in the second column Date date = new Date(dateStr); return new Co2Data(date, co2Level); } catch (Exception e) { System.out.println("Error parsing data line: " + e); return null; } } }