total pain

This commit is contained in:
sageTheDM 2024-11-19 16:10:52 +01:00
parent bcebce2a6a
commit a3f6e8c4dc
10 changed files with 294 additions and 168 deletions

View file

@ -12,14 +12,37 @@ public class App {
public static final Lesson[][][] timeTable = new Lesson[ROOM_COUNT][DAY_COUNT][LESSON_COUNT];
private static final Teacher[] teachers = new Teacher[Teacher.nameMap.size()];
// URLs for fetching CO2 data
private static final String ROOM_39_URL = "https://api.thingspeak.com/channels/1521262/feeds.csv?results=8000";
private static final String ROOM_38_URL = "https://api.thingspeak.com/channels/1364580/feeds.csv?results=8000";
private static final String ROOM_37_URL = "https://api.thingspeak.com/channels/1521263/feeds.csv?results=8000";
// Date constants
private static final int START_YEAR = 2024;
private static final int START_MONTH = 11; // November
private static final int START_DAY = 4;
private static final String START_DATE = String.format("%d-%02d-%02d%%2000:00:00", START_YEAR, START_MONTH,
START_DAY);
private static final List<Co2Data> room39Data = Co2Data.getData(ROOM_39_URL, 39);
private static final List<Co2Data> room38Data = Co2Data.getData(ROOM_38_URL, 38);
private static final List<Co2Data> room37Data = Co2Data.getData(ROOM_37_URL, 37);
private static final int END_YEAR = 2024;
private static final int END_MONTH = 11; // November
private static final int END_DAY = 8;
private static final String END_DATE = String.format("%d-%02d-%02d%%2000:00:00", END_YEAR, END_MONTH, END_DAY);
// 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 URLs
private static final String ROOM_39_URL = createUrl(ROOM_39_NUMBER);
private static final String ROOM_38_URL = createUrl(ROOM_38_NUMBER);
private static final String ROOM_37_URL = createUrl(ROOM_37_NUMBER);
// Room data
private static final List<Co2Data> ROOM_39_DATA = Co2Data.getData(ROOM_39_URL, 39);
private static final List<Co2Data> ROOM_38_DATA = Co2Data.getData(ROOM_38_URL, 38);
private static final List<Co2Data> ROOM_37_DATA = Co2Data.getData(ROOM_37_URL, 37);
private static String createUrl(int channelNumber) {
return String.format("https://api.thingspeak.com/channels/%d/feeds.csv?start=%s&end=%s", channelNumber,
START_DATE, END_DATE);
}
// #region Initialization
private static void initializeTeachers() {
@ -36,21 +59,21 @@ public class App {
}
// #region Calculation
private static void calculatePoints(List<Co2Data> data) {
private static void calculatePoints(List<Co2Data> data, int roomNumber) {
for (Co2Data co2Data : data) {
Date temp = co2Data.getDate();
int intHour = temp.getHour();
int intMinute = temp.getMinute();
int intDay = temp.getDay();
if (FillTable.isBreak(intHour, intMinute)) {
String whatBreak = FillTable.whatBreakIsIt(intHour, intMinute, intDay);
String whatBreak = FillTable.whatBreakIsIt(intHour, intMinute);
int[] time = FillTable.whatTime(intHour, intMinute);
switch (whatBreak) {
case "short":
calculateFiveMinuteBreakPoints(co2Data);
calculateFiveMinuteBreakPoints(data, time, roomNumber);
break;
case "long":
calculateLongerBreakPoints(co2Data);
calculateLongerBreakPoints(data, time, roomNumber);
break;
default:
@ -60,18 +83,106 @@ public class App {
}
}
private static int calculateFiveMinuteBreakPoints(Co2Data data) {
calculateBonusPoints(data);
return 5;
private static int calculateFiveMinuteBreakPoints(List<Co2Data> data, int[] time, int roomNumber) {
// Calculate the time range for the break (e.g., a 5-minute break)
int startMinute = time[0];
int endMinute = time[1];
int points = 5; // Start with maximum points
// Loop over each minute in the break and check the CO2 level
for (int i = startMinute; i < endMinute; i++) {
// Get the CO2 data for the current minute (using the data list)
Co2Data minuteData = getCo2DataForMinute(data, i); // data is already a List<Co2Data>
if (minuteData != null) {
int currentCo2 = minuteData.getCo2Level();
Co2Data previousMinuteData = getCo2DataForMinute(data, i - 1); // data is a List<Co2Data>
if (previousMinuteData != null) {
int previousCo2 = previousMinuteData.getCo2Level();
// If CO2 level doesn't decrease, subtract points
if (currentCo2 >= previousCo2) {
points--;
}
}
}
}
private static int calculateLongerBreakPoints(Co2Data data) {
calculateBonusPoints(data);
return 10;
// After calculating the points, consider any bonus points for teacher switch or
// other criteria
points += calculateBonusPoints(data, time, roomNumber);
return Math.max(points, 0); // Ensure that points don't go below zero
}
private static int calculateBonusPoints(Co2Data data) {
return 5;
private static int calculateLongerBreakPoints(List<Co2Data> data, int[] time, int roomNumber) {
// Take the time for the entire break
int startMinute = time[0];
int endMinute = time[1];
int points = 10; // Start with maximum points for longer break
// Loop over each minute in the break and check the CO2 level
for (int i = startMinute; i < endMinute; i++) {
// Get the CO2 data for the current minute (using the data list)
Co2Data minuteData = getCo2DataForMinute(data, i); // data is already a List<Co2Data>
if (minuteData != null) {
int currentCo2 = minuteData.getCo2Level();
Co2Data previousMinuteData = getCo2DataForMinute(data, i - 1); // data is a List<Co2Data>
if (previousMinuteData != null) {
int previousCo2 = previousMinuteData.getCo2Level();
// If CO2 level doesn't decrease, subtract points
if (currentCo2 >= previousCo2) {
points--;
}
}
}
}
// After calculating the points, consider any bonus points for teacher switch or
// other criteria
points += calculateBonusPoints(data, time, roomNumber);
return Math.max(points, 0); // Ensure that points don't go below zero
}
private static int calculateBonusPoints(List<Co2Data> data, int[] time, int roomNumber) {
// Check if the next lesson isn't lunch or the same teacher (if the next lesson
// is lunch, no bonus points possible)
boolean isTeacherChange = isTeacherChange(data, roomNumber); // Check if the teacher is changing
boolean isNextLessonLunch = isNextLessonLunch(data, roomNumber);
if (!isNextLessonLunch && isTeacherChange) {
return 5; // Bonus points for teacher switch
} else {
return 0; // No bonus points
}
}
private static boolean isTeacherChange(List<Co2Data> data, int roomNumber) {
return false; // Placeholder logic
}
private static boolean isNextLessonLunch(List<Co2Data> data, int roomNumber) {
// Logic to check if the next lesson is lunch (can be based on time and lesson
// schedule)
return false; // Placeholder logic
}
private static Co2Data getCo2DataForMinute(List<Co2Data> data, int minute) {
// Logic to get the CO2 data for a specific minute in the break
for (Co2Data co2Data : data) {
if (co2Data.getDate().getMinute() == minute) {
return co2Data;
}
}
return null; // Return null if no data matches
}
// #region Sorting Printing
@ -109,10 +220,8 @@ public class App {
while (true) {
if (scanner.hasNextInt()) {
return scanner.nextInt();
} else {
System.out.println("Invalid input. Please enter a number.");
scanner.next();
}
scanner.next(); // Clear invalid input
}
}
@ -120,7 +229,8 @@ public class App {
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("3. 5 bonus points for teacher switches in the room.");
System.out.println("3. 5 bonus points for teacher switches in classrooms.");
System.out.println("4. Deduct points if CO2 levels are too high.");
}
private static void printShutDown() {
@ -130,17 +240,16 @@ public class App {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
System.out.println("Goodbye!");
}
// @TODO: remove this before hand-in deadline
public static void debbugingValueLists(List<Co2Data> data) {
// Debugging co2Data
int index = 0;
for (Co2Data a : room37Data) {
for (Co2Data a : data) {
System.out.println(a.toString());
index++;
}
@ -153,16 +262,16 @@ public class App {
public static void main(String[] args) {
boolean debbugingList = false;
if (debbugingList) {
debbugingValueLists(room37Data);
debbugingValueLists(ROOM_37_DATA);
// debbugingValueLists(room38Data);
// debbugingValueLists(room39Data);
} else {
System.out.println("Calculations in process please do not shut off...");
fillInTimeTable();
initializeTeachers();
calculatePoints(room37Data);
calculatePoints(room38Data);
calculatePoints(room39Data);
calculatePoints(ROOM_37_DATA, 37);
calculatePoints(ROOM_38_DATA, 38);
calculatePoints(ROOM_39_DATA, 39);
sortTeachers();
printTeachers();
while (true) {

View file

@ -1,6 +1,3 @@
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Date {
// #region Fields
private int day;
@ -42,34 +39,9 @@ public class Date {
return minute;
}
// #region Helper Method to Check Daylight Saving Time
private boolean isDaylightSavingTime() {
Calendar calendar = new GregorianCalendar();
calendar.set(year, month - 1, day); // Month is 0-based in Calendar
// Last Sunday in March (DST starts)
Calendar lastSundayInMarch = new GregorianCalendar(year, Calendar.MARCH, 31);
lastSundayInMarch.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
lastSundayInMarch.add(Calendar.DATE, -7); // Go back to last Sunday of March
// Last Sunday in October (DST ends)
Calendar lastSundayInOctober = new GregorianCalendar(year, Calendar.OCTOBER, 31);
lastSundayInOctober.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
lastSundayInOctober.add(Calendar.DATE, -7); // Go back to last Sunday of October
// Check if the current date is within the DST period
return calendar.after(lastSundayInMarch) && calendar.before(lastSundayInOctober);
}
// #region Adjust UTC to Switzerland Time
private void adjustForSwitzerlandTime() {
if (isDaylightSavingTime()) {
// Switzerland is UTC +2 during DST (CEST)
hour += 2;
} else {
// Switzerland is UTC +1 during Standard Time (CET)
hour += 1;
}
// Adjust if the hour exceeds 24 or falls below 0
if (hour >= 24) {

View file

@ -110,11 +110,56 @@ public class FillTable {
return false; // Time is outside of the school hours (7 AM to 6 PM roughly)
}
static String whatBreakIsIt(int intHour, int intMinute, int intDay) {
boolean isABreak = false;
static String whatBreakIsIt(int intHour, int intMinute) {
String whatBreak = "";
for (int i = 0; i < END_TIMES.length; i++) {
String[] tempEnd = END_TIMES[i].split(":");
String[] tempStart = START_TIMES[i].split(":");
if (intHour >= Integer.parseInt(tempStart[0]) && intHour <= Integer.parseInt(tempEnd[0])) {
if (intMinute >= Integer.parseInt(tempStart[1])
&& intMinute <= Integer.parseInt(tempEnd[1])) {
if (Integer.parseInt(tempEnd[1]) - Integer.parseInt(tempStart[1]) == 5) {
whatBreak = "short";
} else if (Integer.parseInt(tempEnd[1]) - Integer.parseInt(tempStart[1]) > 5) {
whatBreak = "long";
} else {
whatBreak = "No Break";
}
}
}
return "No Break"; // If no break is found, return "No Break"
}
return whatBreak; // If no break is found, return "No Break"
}
static int[] whatTime(int intHour, int intMinute) {
int[] result = new int[2]; // [most recent end time, next start time]
result[0] = -1; // Initialize to -1 (no previous end time found)
result[1] = -1; // Initialize to -1 (no next start time found)
for (int i = 0; i < END_TIMES.length; i++) {
// Parse END_TIMES[i]
String[] endParts = END_TIMES[i].split(":");
int endHour = Integer.parseInt(endParts[0]);
int endMinute = Integer.parseInt(endParts[1]);
// Parse START_TIMES[i]
String[] startParts = START_TIMES[i].split(":");
int startHour = Integer.parseInt(startParts[0]);
int startMinute = Integer.parseInt(startParts[1]);
// Check for the most recent end time
if ((intHour > endHour) || (intHour == endHour && intMinute >= endMinute)) {
result[0] = i; // Track the most recent lesson's end time index
}
// Check for the next start time
if ((intHour < startHour) || (intHour == startHour && intMinute < startMinute)) {
result[1] = i; // Track the next lesson's start time index
break; // No need to continue since START_TIMES is sorted
}
}
return result;
}
}