diff --git a/bin/App.class b/bin/App.class index d3fe19c..01dfcb4 100644 Binary files a/bin/App.class and b/bin/App.class differ diff --git a/bin/Break.class b/bin/Break.class index 5193bc1..26cebca 100644 Binary files a/bin/Break.class and b/bin/Break.class differ diff --git a/bin/BreakSchedule.class b/bin/BreakSchedule.class index 17a59e2..a590c59 100644 Binary files a/bin/BreakSchedule.class and b/bin/BreakSchedule.class differ diff --git a/bin/Co2Data.class b/bin/Co2Data.class index 805022a..bd02c1e 100644 Binary files a/bin/Co2Data.class and b/bin/Co2Data.class differ diff --git a/bin/Time.class b/bin/Time.class deleted file mode 100644 index 92abf03..0000000 Binary files a/bin/Time.class and /dev/null differ diff --git a/src/App.java b/src/App.java index e75cc80..0b0ede2 100644 --- a/src/App.java +++ b/src/App.java @@ -1,3 +1,6 @@ +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -7,59 +10,34 @@ public class App { private static final Scanner scanner = new Scanner(System.in); private static final Teacher[] teachers = new Teacher[Teacher.nameMap.size()]; - // Room channel numbers - private static final int ROOM_39_NUMBER = 1521262; - private static final int ROOM_38_NUMBER = 1364580; - private static final int ROOM_37_NUMBER = 1521263; + // Room channel numbers, sorted asc. 37, 38, 39 + private static final int[] ROOM_NUMBERS = { 1521263, 1364580, 1521262 }; // Date and time constants for Start and End Dates - private static final String START_DATE = "2024-11-04"; - private static final String END_DATE = "2024-11-08"; + private static final String[] DATES = { "2024-11-04", "2024-11-05", "2024-11-06", "2024-11-07", "2024-11-08" }; + + private static ArrayList SHORT_BREAKS = new ArrayList(); + private static ArrayList LONG_BREAKS = new ArrayList(); // #region Initialization - public static String generateLink(int channelNumber, String date, String startTime, String endTime) { + public static URL generateLink(int channelNumber, String date) throws MalformedURLException { String baseUrl = "https://api.thingspeak.com/channels/"; - String formattedStartDateTime = date + " " + startTime; - String formattedEndDateTime = date + " " + endTime; + String formattedStartDateTime = date + " 07:00:00"; + String formattedEndDateTime = date + " 19:00:00"; - return baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20") - + "&end=" + formattedEndDateTime.replace(" ", "%20"); // %20 --> " " + return new URL(baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20") + + "&end=" + formattedEndDateTime.replace(" ", "%20")); // %20 --> " " } - private static double getDataAverageForMinute(int minute, int hour, int number, String date) { - // get the url - String startTime = ""; - if (hour < 10) { - startTime += "0"; - } - startTime += String.valueOf(hour); - startTime += ":"; - if (minute < 10) { - startTime += "0"; - } - startTime += String.valueOf(minute); - String calcStarttime = startTime + ":00"; - String calcEndTime = startTime + "59"; - - String url = generateLink(number, date, calcStarttime, calcEndTime); - - double averageCO2 = 0.0; - List co2DataList = Co2Data.getData(url); - for (Co2Data co2Data : co2DataList) { - averageCO2 += co2Data.getCo2Level(); - } - - return averageCO2 / co2DataList.size(); // Example CO2 average level - } - - private static void calculateBreakPoints(double[] minuteData, Break calcBreak, Teacher teacher) { - int duration = calcBreak.getEnd().getMinute() - calcBreak.getStart().getMinute(); + private static void calculateBreakPoints(Break calcBreak, Teacher teacher) { + int duration = calcBreak.getEnd() - calcBreak.getStart(); + Co2Data[] minuteData = calcBreak.getCo2Datas(); int breakPoints = minuteData.length; if (duration == minuteData.length) { for (int i = 0; i < duration; i++) { - if (minuteData[i] < minuteData[i + 1]) { + if (i + 1 != duration && (minuteData[i].getCo2Level() < minuteData[i + 1].getCo2Level())) { breakPoints--; } } @@ -86,7 +64,6 @@ public class App { } } - // #region User Interaction private static int getUserInput(String textOutput) { System.out.println(textOutput); @@ -102,7 +79,7 @@ public class App { private static void printExplanation() { System.out.println("Point calculation explanation:"); System.out.println("1. Up to 5 points for keeping the window open during a small pause."); - System.out.println("2. Up to 10 points for long pauses, depending on window usage."); + System.out.println("2. Up to 15-20 points for long pauses, depending on window usage."); System.out.println("3. 5 bonus points for teacher switches in classrooms."); } @@ -164,23 +141,32 @@ public class App { } // #region Main - public static void main(String[] args) { + public static void main(String[] args) throws MalformedURLException { System.out.println("Calculations in process please do not shut off..."); initializeTeachers(); - sortTeachers(); - printTeachers(); + + ArrayList co2Datas = new ArrayList(); for (int classrooms = 0; classrooms < 3; classrooms++) { for (int weekday = 0; weekday < 5; weekday++) { - // get the url and data - // calculate points + List tempData = Co2Data.getData(generateLink(ROOM_NUMBERS[classrooms], DATES[weekday])); + SHORT_BREAKS.add(BreakSchedule.createBreaks(BreakSchedule.START_SMALL_BREAK, + BreakSchedule.END_SMALL_BREAK, tempData)); + LONG_BREAKS.add(BreakSchedule.createBreaks(BreakSchedule.START_LONG_BREAK, + BreakSchedule.END_LONG_BREAK, tempData)); } } + + calculateBreakPoints(SHORT_BREAKS.get(0)[0], teachers[0]); + // Loop threw each day with a specific classroom and after the weekdays are over // go to the next of the 3 classroms // go threw every break calculate the point and give them to the teacher // directly // remember the point class - // breakShedule needed + // BreakSchedule needed + + sortTeachers(); + printTeachers(); while (true) { int userInput = getUserInput( diff --git a/src/Break.java b/src/Break.java index a646d36..2e182f0 100644 --- a/src/Break.java +++ b/src/Break.java @@ -1,56 +1,36 @@ public class Break { - private Time start; - private Time end; + private int start; + private int end; + private Co2Data co2Datas[]; - public Break(Time start, Time end) { - this.start = start; - this.end = end; - } - - public Time getStart() { + public int getStart() { return start; } - public void setStart(Time start) { + public void setStart(int start) { this.start = start; } - public Time getEnd() { + public int getEnd() { return end; } - public void setEnd(Time end) { + public void setEnd(int end) { this.end = end; } - /** - * Generates a Thingspeak API link based on channel number, date, and break - * period. - * - * @param channelNumber The Thingspeak channel number. - * @param date The date in format "YYYY-MM-DD". - * @param breakPeriod The Break object containing start and end times. - * @return A formatted URL for fetching data from the Thingspeak API. - */ - public static String generateLink(int channelNumber, String date, Break breakPeriod) { - String baseUrl = "https://api.thingspeak.com/channels/"; - String formattedStartDateTime = date + " " + breakPeriod.getStart().toString(); - String formattedEndDateTime = date + " " + breakPeriod.getEnd().toString(); - - return baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20") - + "&end=" + formattedEndDateTime.replace(" ", "%20"); + public Co2Data[] getCo2Datas() { + return co2Datas; } - public static void main(String[] args) { - // Example usage - Time start = Time.valueOf("00:00:00"); - Time end = Time.valueOf("23:59:59"); - Break breakPeriod = new Break(start, end); - - String date = "2024-11-17"; - int channelNumber = 1364580; - - String link = Break.generateLink(channelNumber, date, breakPeriod); - System.out.println(link); + public void setCo2Datas(Co2Data[] co2Datas) { + this.co2Datas = co2Datas; } + + public Break(int start, int end, Co2Data[] co2Datas) { + this.start = start; + this.end = end; + this.co2Datas = co2Datas; + } + } diff --git a/src/BreakSchedule.java b/src/BreakSchedule.java index 165913b..7fc7b50 100644 --- a/src/BreakSchedule.java +++ b/src/BreakSchedule.java @@ -1,47 +1,34 @@ +import java.util.List; + public class BreakSchedule { - private static final String[] START_SMALL_BREAK = { - "8:30", "10:25", "11:15", "12:05", "13:30", "14:20", "16:10", "17:00", "17:50" + public static final int[] START_SMALL_BREAK = { + 830, 1025, 1115, 1205, 1330, 1420, 1610, 1700, 1750 }; - private static final String[] END_SMALL_BREAK = { - "8:35", "10:30", "11:20", "12:10", "13:35", "14:25", "16:15", "17:05", "17:55" + public static final int[] END_SMALL_BREAK = { + 835, 1030, 1120, 1210, 1335, 1425, 1615, 1705, 1755 }; - private static final String[] START_LONG_BREAK = { - "9:20", "15:10" + public static final int[] START_LONG_BREAK = { + 920, 1510 }; - private static final String[] END_LONG_BREAK = { - "9:40", "15:25" + public static final int[] END_LONG_BREAK = { + 940, 1525 }; - private static final Break[] SMALL_BREAKS; - private static final Break[] LONG_BREAKS; - - static { - SMALL_BREAKS = createBreaks(START_SMALL_BREAK, END_SMALL_BREAK); - LONG_BREAKS = createBreaks(START_LONG_BREAK, END_LONG_BREAK); - } - - private static Break[] createBreaks(String[] startTimes, String[] endTimes) { + public static Break[] createBreaks(int[] startTimes, int[] endTimes, List co2Data) { Break[] breaks = new Break[startTimes.length]; for (int i = 0; i < startTimes.length; i++) { - // Append ":00" to include seconds - String startTimeWithSeconds = startTimes[i]; - String endTimeWithSeconds = endTimes[i]; - - Time start = Time.valueOf(startTimeWithSeconds); - Time end = Time.valueOf(endTimeWithSeconds); - breaks[i] = new Break(start, end); + Co2Data[] co2Datas = new Co2Data[endTimes[i] - startTimes[i]]; + for (Co2Data data : co2Data) { + if (data.getTime() >= startTimes[i] && data.getTime() < endTimes[i]) { + System.out.println(startTimes[i] + " " + endTimes[i] + " " + data.getTime()); + co2Datas[data.getTime() - startTimes[i]] = data; + } + } + breaks[i] = new Break(startTimes[i], endTimes[i], co2Datas); } return breaks; } - - public static Break[] getSmallBreaks() { - return SMALL_BREAKS; - } - - public static Break[] getLongBreaks() { - return LONG_BREAKS; - } } \ No newline at end of file diff --git a/src/Co2Data.java b/src/Co2Data.java index 3b6a03b..fd8154d 100644 --- a/src/Co2Data.java +++ b/src/Co2Data.java @@ -2,19 +2,19 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; 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 @@ -34,12 +34,19 @@ public class Co2Data { 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(String csvURL) { + public static List getData(URL url) { List dataList = new ArrayList<>(); try { - URL url = new URL(csvURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "text/csv"); @@ -52,6 +59,7 @@ public class Co2Data { br.readLine(); // Skip header line String output; + System.out.println(url); while ((output = br.readLine()) != null) { Co2Data data = parseData(output); if (data != null) { @@ -73,11 +81,7 @@ public class Co2Data { 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 - - // Convert the date string into a Date object (assumes CSV date format is - // yyyy-MM-dd HH:mm:ss) - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date = sdf.parse(dateStr); + Date date = new Date(dateStr); return new Co2Data(date, co2Level); } catch (Exception e) { @@ -85,4 +89,5 @@ public class Co2Data { return null; } } + } diff --git a/src/Time.java b/src/Time.java deleted file mode 100644 index 62eee0e..0000000 --- a/src/Time.java +++ /dev/null @@ -1,37 +0,0 @@ -public class Time { - private int hour; - private int minute; - - public Time(int hour, int minute) { - if (hour >= 1 && hour <= 24 && minute >= 1 && minute <= 60) { - this.hour = hour; - this.minute = minute; - } else { - System.out.println("Failed to initialize the hour"); - } - } - - public int getHour() { - return hour; - } - - public void setHour(int hour) { - this.hour = hour; - } - - public int getMinute() { - return minute; - } - - public void setMinute(int minute) { - this.minute = minute; - } - - public static Time valueOf(String time) { - String[] parts = time.split(":"); - int hour = Integer.parseInt(parts[0]); - int minute = Integer.parseInt(parts[1]); - Time returnValue = new Time(hour, minute); - return returnValue; - } -}