added a debbuging option for our co2 data
This commit is contained in:
		
							parent
							
								
									1464df11cf
								
							
						
					
					
						commit
						036911b58c
					
				
					 22 changed files with 362 additions and 25 deletions
				
			
		|  | @ -1,7 +1,9 @@ | |||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
| public class App { | ||||
|     // #region Fields | ||||
|     private static final Scanner scanner = new Scanner(System.in); | ||||
| 
 | ||||
|     private static final int ROOM_COUNT = 3; | ||||
|  | @ -11,6 +13,18 @@ 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()]; | ||||
| 
 | ||||
|     // Get data from the last 8'000 entries seeing as that is the maximum | ||||
|     private static final List<Co2Data> room39Data = Co2Data.getData( | ||||
|             "https://api.thingspeak.com/channels/1521262/feeds.csv?results=8000", | ||||
|             39); | ||||
|     private static final List<Co2Data> room38Data = Co2Data.getData( | ||||
|             "https://api.thingspeak.com/channels/1364580/feeds.csv?results=8000", | ||||
|             38); | ||||
|     private static final List<Co2Data> room37Data = Co2Data.getData( | ||||
|             "https://api.thingspeak.com/channels/1521263/feeds.csv?results=8000", | ||||
|             37); | ||||
| 
 | ||||
|     // #region Initialization | ||||
|     private static void initializeTeachers() { | ||||
|         int index = 0; | ||||
|         for (String initial : Teacher.nameMap.keySet()) { | ||||
|  | @ -24,6 +38,7 @@ public class App { | |||
|         FillTable.fill39TimeTable(); | ||||
|     } | ||||
| 
 | ||||
|     // #region Calculations | ||||
|     private static void calculatePoints() { | ||||
|         // Point calculation logic | ||||
|     } | ||||
|  | @ -33,6 +48,7 @@ public class App { | |||
|                 (a, b) -> Integer.compare(b.getPoints().getTotalPoints(), a.getPoints().getTotalPoints())); | ||||
|     } | ||||
| 
 | ||||
|     // #region Input - Output handler | ||||
|     private static void printTeachers() { | ||||
|         int rank = 1; | ||||
|         int previousPoints = -1; | ||||
|  | @ -88,25 +104,46 @@ public class App { | |||
|         System.out.println("Goodbye!"); | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         fillInTimeTable(); | ||||
|         initializeTeachers(); | ||||
|         calculatePoints(); | ||||
|         sortTeachers(); | ||||
|         printTeachers(); | ||||
| 
 | ||||
|         int userInput = getUserInput( | ||||
|                 "Do you want to see how the points were calculated? (Yes 1, No 0; anything is an error)"); | ||||
| 
 | ||||
|         if (userInput == 1) { | ||||
|             printExplanation(); | ||||
|             printShutDown(); | ||||
|         } else if (userInput == 0) { | ||||
|             printShutDown(); | ||||
|         } else { | ||||
|             System.out.println("Invalid input. Please enter 1 for Yes or 0 for No."); | ||||
|     // @TODO: remove this before hand-in deadline | ||||
|     public static void debbugingValueLists(List<Co2Data> data) { | ||||
|         // Debugging co2Data | ||||
|         int index = 0; | ||||
|         for (Co2Data a : room37Data) { | ||||
|             System.out.println(a.toString()); | ||||
|             index++; | ||||
|         } | ||||
| 
 | ||||
|         scanner.close(); | ||||
|         System.out.println("-----------------"); | ||||
|         System.out.println(index); | ||||
|     } | ||||
| 
 | ||||
|     // #region Main | ||||
|     public static void main(String[] args) { | ||||
|         boolean debbugingList = true; | ||||
|         if (debbugingList) { | ||||
|             debbugingValueLists(room37Data); | ||||
|             // debbugingValueLists(room38Data); | ||||
|             // debbugingValueLists(room39Data); | ||||
|         } else { | ||||
|             fillInTimeTable(); | ||||
|             initializeTeachers(); | ||||
|             calculatePoints(); | ||||
|             sortTeachers(); | ||||
|             printTeachers(); | ||||
| 
 | ||||
|             int userInput = getUserInput( | ||||
|                     "Do you want to see how the points were calculated? (Yes 1, No 0; anything is an error)"); | ||||
| 
 | ||||
|             if (userInput == 1) { | ||||
|                 printExplanation(); | ||||
|                 printShutDown(); | ||||
|             } else if (userInput == 0) { | ||||
|                 printShutDown(); | ||||
|             } else { | ||||
|                 System.out.println("Invalid input. Please enter 1 for Yes or 0 for No."); | ||||
|             } | ||||
| 
 | ||||
|             scanner.close(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -9,11 +9,13 @@ public class Co2Data { | |||
|     private Date date; | ||||
|     private int co2Level; | ||||
| 
 | ||||
|     // #region Constructor | ||||
|     public Co2Data(Date date, int co2Level) { | ||||
|         this.date = date; | ||||
|         this.co2Level = co2Level; | ||||
|     } | ||||
| 
 | ||||
|     // #region Getters Setters | ||||
|     public Date getDate() { | ||||
|         return date; | ||||
|     } | ||||
|  | @ -30,9 +32,13 @@ public class Co2Data { | |||
|         this.co2Level = co2Level; | ||||
|     } | ||||
| 
 | ||||
|     // #region Fetching & Parsing | ||||
|     public static List<Co2Data> getData(String csvURL, int classRoomNumber) { | ||||
|         List<Co2Data> dataList = new ArrayList<>(); | ||||
| 
 | ||||
|         // Reference date: August 11, 2024 | ||||
|         Date referenceDate = new Date(11, 8, 2024, 0, 0); // Set time to 00:00 as we only care about the date | ||||
| 
 | ||||
|         try { | ||||
|             URL url = new URL(csvURL); | ||||
|             HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||||
|  | @ -52,7 +58,7 @@ public class Co2Data { | |||
| 
 | ||||
|             while ((output = br.readLine()) != null) { | ||||
|                 Co2Data data = parseData(output, classRoomNumber); | ||||
|                 if (data != null) { | ||||
|                 if (data != null && isNewerThanReferenceDate(data.getDate(), referenceDate)) { | ||||
|                     dataList.add(data); | ||||
|                 } | ||||
|             } | ||||
|  | @ -65,6 +71,21 @@ public class Co2Data { | |||
|         return dataList; // Return the list of Co2Data objects | ||||
|     } | ||||
| 
 | ||||
|     // Helper method to compare dates | ||||
|     private static boolean isNewerThanReferenceDate(Date dataDate, Date referenceDate) { | ||||
|         // Compare year, month, and day only (ignoring the time part) | ||||
|         if (dataDate.getYear() > referenceDate.getYear()) { | ||||
|             return true; | ||||
|         } else if (dataDate.getYear() == referenceDate.getYear()) { | ||||
|             if (dataDate.getMonth() > referenceDate.getMonth()) { | ||||
|                 return true; | ||||
|             } else if (dataDate.getMonth() == referenceDate.getMonth()) { | ||||
|                 return dataDate.getDay() > referenceDate.getDay(); | ||||
|             } | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     private static Co2Data parseData(String csvLine, int classRoomNumber) { | ||||
|         String[] fields = csvLine.split(","); | ||||
|         if (fields.length < 5) { | ||||
|  | @ -95,4 +116,10 @@ public class Co2Data { | |||
|             return null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // #region toString Override | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return this.date.toString() + "\n" + this.co2Level; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,19 +1,27 @@ | |||
| import java.util.Calendar; | ||||
| import java.util.GregorianCalendar; | ||||
| 
 | ||||
| public class Date { | ||||
|     // #region Fields | ||||
|     private int day; | ||||
|     private int month; | ||||
|     private int year; | ||||
|     private int hour; | ||||
|     private int minute; | ||||
| 
 | ||||
|     // #region Constructor | ||||
|     public Date(int day, int month, int year, int hour, int minute) { | ||||
|         this.day = day; | ||||
|         this.month = month; | ||||
|         this.year = year; | ||||
|         this.hour = hour; | ||||
|         this.minute = minute; | ||||
| 
 | ||||
|         // Automatically adjust for Switzerland time after initialization | ||||
|         adjustForSwitzerlandTime(); | ||||
|     } | ||||
| 
 | ||||
|     // Getters | ||||
|     // #region Getters | ||||
|     public int getDay() { | ||||
|         return day; | ||||
|     } | ||||
|  | @ -34,6 +42,62 @@ 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) { | ||||
|             hour -= 24; | ||||
|             day++; | ||||
|             if (day > 31) { // Simple month/day rollover, could be more sophisticated | ||||
|                 day = 1; | ||||
|                 month++; | ||||
|                 if (month > 12) { | ||||
|                     month = 1; | ||||
|                     year++; | ||||
|                 } | ||||
|             } | ||||
|         } else if (hour < 0) { | ||||
|             hour += 24; | ||||
|             day--; | ||||
|             if (day < 1) { | ||||
|                 month--; | ||||
|                 if (month < 1) { | ||||
|                     month = 12; | ||||
|                     year--; | ||||
|                 } | ||||
|                 day = 31; // Simple month/day rollover, could be more sophisticated | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // #region toString Override | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return String.format("%04d-%02d-%02d %02d:%02d", year, month, day, hour, minute); | ||||
|  |  | |||
|  | @ -1,5 +1,5 @@ | |||
| 
 | ||||
| public class FillTable { | ||||
|     // #region Constants | ||||
|     private static final String[] START_TIMES = { | ||||
|             "7:45", "8:35", "9:40", "10:30", "11:20", "12:10", "12:50", | ||||
|             "13:35", "14:25", "15:15", "16:15", "17:05" | ||||
|  | @ -10,6 +10,7 @@ public class FillTable { | |||
|             "14:20", "15:10", "16:10", "17:00", "17:50" | ||||
|     }; | ||||
| 
 | ||||
|     // #region Helper Methods | ||||
|     private static void fillTable(String[] teacherShortNames, String day, String[] startTime, String[] endTime, | ||||
|             int roomIndex) { | ||||
|         int dayIndex = getDayIndex(day); | ||||
|  | @ -37,6 +38,7 @@ public class FillTable { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // #region Fill 37 | ||||
|     static void fill37TimeTable() { | ||||
|         int roomIndex = 0; | ||||
|         fillTable(new String[] { "Hm", "Hm", "Hi", "Hm", "Hm", "Lunch", "Bd", "Gi", "Gi", "Ts", "Ts", "" }, | ||||
|  | @ -51,6 +53,7 @@ public class FillTable { | |||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| 
 | ||||
|     // #region Fill 38 | ||||
|     static void fill38TimeTable() { | ||||
|         int roomIndex = 1; | ||||
|         fillTable(new String[] { "Bz", "Bz", "Bz", "Bz", "Bz", "Lunch", "Lunch", "Hn", "Hn", "Bu", "Hn", "Hn" }, | ||||
|  | @ -65,6 +68,7 @@ public class FillTable { | |||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| 
 | ||||
|     // #region Fill 39 | ||||
|     static void fill39TimeTable() { | ||||
|         int roomIndex = 2; | ||||
|         fillTable(new String[] { "Bd", "Bd", "Bd", "Bd", "Bd", "Lunch", "Lunch", "Lu", "Lu", "Lu", "Lu", "" }, | ||||
|  | @ -78,4 +82,5 @@ public class FillTable { | |||
|         fillTable(new String[] { "Gi", "Gi", "Gr", "Gr", "Gi", "Lunch", "Lunch", "Hi", "Hi", "Hi", "", "" }, | ||||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -1,10 +1,12 @@ | |||
| public class Lesson { | ||||
|     // #region Fields | ||||
|     private int roomNumberNumber; | ||||
|     private String teacherInitials; | ||||
|     private String startTime; | ||||
|     private String endTime; | ||||
|     private String weekweekDay; | ||||
| 
 | ||||
|     // #region Constructor | ||||
|     // Constructor to initialize all fields | ||||
|     public Lesson(int roomNumber, String teacherInitials, String startTime, String endTime, String weekweekDay) { | ||||
|         this.roomNumberNumber = roomNumber; | ||||
|  | @ -14,7 +16,7 @@ public class Lesson { | |||
|         this.weekweekDay = weekweekDay; | ||||
|     } | ||||
| 
 | ||||
|     // Getters | ||||
|     // #region Getters | ||||
|     public int getroomNumber() { | ||||
|         return roomNumberNumber; | ||||
|     } | ||||
|  | @ -34,4 +36,5 @@ public class Lesson { | |||
|     public String getweekDay() { | ||||
|         return weekweekDay; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -1,9 +1,11 @@ | |||
| // Points class for managing point categories | ||||
| public class Points { | ||||
|     // #region Fields | ||||
|     private int fiveMinuteBreak; | ||||
|     private int longerBreak; | ||||
|     private int bonusPoints; | ||||
| 
 | ||||
|     // #region Getters Setters | ||||
|     public int getFiveMinuteBreak() { | ||||
|         return fiveMinuteBreak; | ||||
|     } | ||||
|  | @ -28,8 +30,10 @@ public class Points { | |||
|         this.bonusPoints = bonusPoints; | ||||
|     } | ||||
| 
 | ||||
|     // #region Calculation | ||||
|     // Method to calculate total points | ||||
|     public int getTotalPoints() { | ||||
|         return fiveMinuteBreak + longerBreak + bonusPoints; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -2,10 +2,13 @@ import java.util.HashMap; | |||
| import java.util.Map; | ||||
| 
 | ||||
| public class Teacher { | ||||
|     // #region Fields | ||||
|     private String name; | ||||
|     private Points points; | ||||
| 
 | ||||
|     public static final Map<String, String> nameMap = new HashMap<>(); | ||||
| 
 | ||||
|     // #region Initialization | ||||
|     static { | ||||
|         nameMap.put("Hm", "Hummel"); | ||||
|         nameMap.put("Bd", "Bender"); | ||||
|  | @ -29,11 +32,14 @@ public class Teacher { | |||
|         nameMap.put("Zu", "Zuniga"); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     // #region Constructor | ||||
|     public Teacher(String name) { | ||||
|         this.name = nameMap.getOrDefault(name, "Unknown"); | ||||
|         this.points = new Points(); | ||||
|     } | ||||
| 
 | ||||
|     // #region Getters | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
|  | @ -41,4 +47,5 @@ public class Teacher { | |||
|     public Points getPoints() { | ||||
|         return points; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Sage The DM
						Sage The DM