Smaller task & refactoring co2
This commit is contained in:
		
							parent
							
								
									e45d52037f
								
							
						
					
					
						commit
						1464df11cf
					
				
					 29 changed files with 683 additions and 0 deletions
				
			
		
							
								
								
									
										112
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/App.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/App.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,112 @@ | |||
| import java.util.Arrays; | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
| public class App { | ||||
|     private static final Scanner scanner = new Scanner(System.in); | ||||
| 
 | ||||
|     private static final int ROOM_COUNT = 3; | ||||
|     private static final int DAY_COUNT = 5; | ||||
|     private static final int LESSON_COUNT = 12; | ||||
| 
 | ||||
|     public static final Lesson[][][] timeTable = new Lesson[ROOM_COUNT][DAY_COUNT][LESSON_COUNT]; | ||||
|     private static final Teacher[] teachers = new Teacher[Teacher.nameMap.size()]; | ||||
| 
 | ||||
|     private static void initializeTeachers() { | ||||
|         int index = 0; | ||||
|         for (String initial : Teacher.nameMap.keySet()) { | ||||
|             teachers[index++] = new Teacher(initial); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private static void fillInTimeTable() { | ||||
|         FillTable.fill37TimeTable(); | ||||
|         FillTable.fill38TimeTable(); | ||||
|         FillTable.fill39TimeTable(); | ||||
|     } | ||||
| 
 | ||||
|     private static void calculatePoints() { | ||||
|         // Point calculation logic | ||||
|     } | ||||
| 
 | ||||
|     private static void sortTeachers() { | ||||
|         Arrays.sort(teachers, | ||||
|                 (a, b) -> Integer.compare(b.getPoints().getTotalPoints(), a.getPoints().getTotalPoints())); | ||||
|     } | ||||
| 
 | ||||
|     private static void printTeachers() { | ||||
|         int rank = 1; | ||||
|         int previousPoints = -1; | ||||
|         int currentRank = 1; | ||||
| 
 | ||||
|         for (int i = 0; i < teachers.length; i++) { | ||||
|             Teacher teacher = teachers[i]; | ||||
|             int teacherPoints = teacher.getPoints().getTotalPoints(); | ||||
| 
 | ||||
|             if (teacherPoints == previousPoints) { | ||||
|                 System.out.println(currentRank + ". " + teacher.getName() + " " + teacherPoints); | ||||
|             } else { | ||||
|                 if (i > 0) { | ||||
|                     rank += (i - (currentRank - 1)); | ||||
|                 } | ||||
|                 currentRank = rank; | ||||
|                 System.out.println(rank + ". " + teacher.getName() + " " + teacherPoints); | ||||
|             } | ||||
| 
 | ||||
|             previousPoints = teacherPoints; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private static int getUserInput(String textOutput) { | ||||
|         System.out.println(textOutput); | ||||
|         while (true) { | ||||
|             if (scanner.hasNextInt()) { | ||||
|                 return scanner.nextInt(); | ||||
|             } else { | ||||
|                 System.out.println("Invalid input. Please enter a number."); | ||||
|                 scanner.next(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     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 break."); | ||||
|         System.out.println("2. Up to 10 points for long breaks, depending on window usage."); | ||||
|         System.out.println("3. 5 bonus points for teacher switches in the room."); | ||||
|     } | ||||
| 
 | ||||
|     private static void printShutDown() { | ||||
|         System.out.println("Shutting down..."); | ||||
|         for (int i = 3; i > 0; i--) { | ||||
|             System.out.print(i + "..."); | ||||
|             try { | ||||
|                 Thread.sleep(1000); | ||||
|             } catch (InterruptedException e) { | ||||
|                 Thread.currentThread().interrupt(); | ||||
|             } | ||||
|         } | ||||
|         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."); | ||||
|         } | ||||
| 
 | ||||
|         scanner.close(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										98
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Co2Data.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Co2Data.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,98 @@ | |||
| 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 co2Level; | ||||
| 
 | ||||
|     public Co2Data(Date date, int co2Level) { | ||||
|         this.date = date; | ||||
|         this.co2Level = co2Level; | ||||
|     } | ||||
| 
 | ||||
|     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 static List<Co2Data> getData(String csvURL, int classRoomNumber) { | ||||
|         List<Co2Data> dataList = new ArrayList<>(); | ||||
| 
 | ||||
|         try { | ||||
|             URL url = new URL(csvURL); | ||||
|             HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||||
|             conn.setRequestMethod("GET"); | ||||
|             conn.setRequestProperty("Accept", "application/csv"); | ||||
|             if (conn.getResponseCode() != 200) { | ||||
|                 throw new RuntimeException("Failed : HTTP Error code : " | ||||
|                         + conn.getResponseCode()); | ||||
|             } | ||||
| 
 | ||||
|             InputStreamReader in = new InputStreamReader(conn.getInputStream()); | ||||
|             BufferedReader br = new BufferedReader(in); | ||||
|             String output; | ||||
| 
 | ||||
|             // Skip header line | ||||
|             br.readLine(); | ||||
| 
 | ||||
|             while ((output = br.readLine()) != null) { | ||||
|                 Co2Data data = parseData(output, classRoomNumber); | ||||
|                 if (data != null) { | ||||
|                     dataList.add(data); | ||||
|                 } | ||||
|             } | ||||
|             conn.disconnect(); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             System.out.println("Exception in NetClientGet: " + e); | ||||
|         } | ||||
| 
 | ||||
|         return dataList; // Return the list of Co2Data objects | ||||
|     } | ||||
| 
 | ||||
|     private static Co2Data parseData(String csvLine, int classRoomNumber) { | ||||
|         String[] fields = csvLine.split(","); | ||||
|         if (fields.length < 5) { | ||||
|             return null; // Handle error or log it if needed | ||||
|         } | ||||
| 
 | ||||
|         try { | ||||
|             // Extract date and time from created_at field | ||||
|             String createdAt = fields[0]; | ||||
|             String[] dateTime = createdAt.split(" "); | ||||
|             String[] dateParts = dateTime[0].split("-"); | ||||
|             String[] timeParts = dateTime[1].split(":"); | ||||
| 
 | ||||
|             // Create a Date object | ||||
|             int year = Integer.parseInt(dateParts[0]); | ||||
|             int month = Integer.parseInt(dateParts[1]); | ||||
|             int day = Integer.parseInt(dateParts[2]); | ||||
|             int hour = Integer.parseInt(timeParts[0]); | ||||
|             int minute = Integer.parseInt(timeParts[1]); | ||||
|             Date date = new Date(day, month, year, hour, minute); | ||||
| 
 | ||||
|             // Parse CO2 level (field1) | ||||
|             int co2Level = Integer.parseInt(fields[2]); | ||||
| 
 | ||||
|             return new Co2Data(date, co2Level); | ||||
|         } catch (Exception e) { | ||||
|             System.out.println("Error parsing data: " + e); | ||||
|             return null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										41
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Date.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Date.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | |||
| public class Date { | ||||
|     private int day; | ||||
|     private int month; | ||||
|     private int year; | ||||
|     private int hour; | ||||
|     private int minute; | ||||
| 
 | ||||
|     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; | ||||
|     } | ||||
| 
 | ||||
|     // Getters | ||||
|     public int getDay() { | ||||
|         return day; | ||||
|     } | ||||
| 
 | ||||
|     public int getMonth() { | ||||
|         return month; | ||||
|     } | ||||
| 
 | ||||
|     public int getYear() { | ||||
|         return year; | ||||
|     } | ||||
| 
 | ||||
|     public int getHour() { | ||||
|         return hour; | ||||
|     } | ||||
| 
 | ||||
|     public int getMinute() { | ||||
|         return minute; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return String.format("%04d-%02d-%02d %02d:%02d", year, month, day, hour, minute); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										81
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/FillTable.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/FillTable.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,81 @@ | |||
| 
 | ||||
| public class FillTable { | ||||
|     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" | ||||
|     }; | ||||
| 
 | ||||
|     private static final String[] END_TIMES = { | ||||
|             "8:30", "9:20", "10:25", "11:15", "12:05", "12:50", "13:30", | ||||
|             "14:20", "15:10", "16:10", "17:00", "17:50" | ||||
|     }; | ||||
| 
 | ||||
|     private static void fillTable(String[] teacherShortNames, String day, String[] startTime, String[] endTime, | ||||
|             int roomIndex) { | ||||
|         int dayIndex = getDayIndex(day); | ||||
|         for (int i = 0; i < teacherShortNames.length && i < startTime.length && i < endTime.length; i++) { | ||||
|             Teacher teacher = new Teacher(teacherShortNames[i]); // Initialize Teacher with shortform | ||||
|             App.timeTable[roomIndex][dayIndex][i] = new Lesson(roomIndex, teacher.getName(), startTime[i], endTime[i], | ||||
|                     day); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private static int getDayIndex(String day) { | ||||
|         switch (day) { | ||||
|             case "Monday": | ||||
|                 return 0; | ||||
|             case "Tuesday": | ||||
|                 return 1; | ||||
|             case "Wednesday": | ||||
|                 return 2; | ||||
|             case "Thursday": | ||||
|                 return 3; | ||||
|             case "Friday": | ||||
|                 return 4; | ||||
|             default: | ||||
|                 return -1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     static void fill37TimeTable() { | ||||
|         int roomIndex = 0; | ||||
|         fillTable(new String[] { "Hm", "Hm", "Hi", "Hm", "Hm", "Lunch", "Bd", "Gi", "Gi", "Ts", "Ts", "" }, | ||||
|                 "Monday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Ts", "Ts", "Ts", "Ts", "Le", "Lunch", "Lunch", "Fh", "Fh", "Fh", "Fh", "" }, | ||||
|                 "Tuesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Lu", "Lu", "Lu", "Lu", "Cg", "Cg", "Lunch", "Se", "Se", "Se", "Se", "" }, | ||||
|                 "Wednesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Gi", "Gi", "Ba", "Ba", "Ba", "Lunch", "Bd", "Du", "Lz", "Lz" }, | ||||
|                 "Thursday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Kp", "KP", "Or", "Vt", "Vt", "Lunch", "Lunch", "Du", "Du", "Du", "", "" }, | ||||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| 
 | ||||
|     static void fill38TimeTable() { | ||||
|         int roomIndex = 1; | ||||
|         fillTable(new String[] { "Bz", "Bz", "Bz", "Bz", "Bz", "Lunch", "Lunch", "Hn", "Hn", "Bu", "Hn", "Hn" }, | ||||
|                 "Monday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Kg", "Kg", "Eh", "Re", "Re", "Lunch", "Lunch", "Bt", "Kh", "Kh", "", "" }, | ||||
|                 "Tuesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Cg", "Cg", "Cg", "Cg", "Es", "Lunch", "Lunch", "Cg", "Cg", "", "", "" }, | ||||
|                 "Wednesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Do", "Do", "Gr", "Gr", "Or", "Lunch", "Lunch", "Bu", "Bu", "Zu", "", "" }, | ||||
|                 "Thursday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "", "Hu", "Ge", "Eh", "Eh", "Bu", "Lunch", "Eh", "Eh", "", "", "" }, | ||||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| 
 | ||||
|     static void fill39TimeTable() { | ||||
|         int roomIndex = 2; | ||||
|         fillTable(new String[] { "Bd", "Bd", "Bd", "Bd", "Bd", "Lunch", "Lunch", "Lu", "Lu", "Lu", "Lu", "" }, | ||||
|                 "Monday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Do", "Do", "Zu", "Zu", "Zu", "Lunch", "Lunch", "Se", "Se", "Se", "Se", "" }, | ||||
|                 "Tuesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Cg", "Cg", "Cg", "Cg", "Bu", "Lunch", "Lunch", "Gi", "Gi", "Gi", "Gi", "" }, | ||||
|                 "Wednesday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Bd", "Bd", "Bd", "Bd", "Or", "Lunch", "Lunch", "Le", "Le", "Le", "", "" }, | ||||
|                 "Thursday", START_TIMES, END_TIMES, roomIndex); | ||||
|         fillTable(new String[] { "Gi", "Gi", "Gr", "Gr", "Gi", "Lunch", "Lunch", "Hi", "Hi", "Hi", "", "" }, | ||||
|                 "Friday", START_TIMES, END_TIMES, roomIndex); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										37
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Lesson.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Lesson.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | |||
| public class Lesson { | ||||
|     private int roomNumberNumber; | ||||
|     private String teacherInitials; | ||||
|     private String startTime; | ||||
|     private String endTime; | ||||
|     private String weekweekDay; | ||||
| 
 | ||||
|     // Constructor to initialize all fields | ||||
|     public Lesson(int roomNumber, String teacherInitials, String startTime, String endTime, String weekweekDay) { | ||||
|         this.roomNumberNumber = roomNumber; | ||||
|         this.teacherInitials = teacherInitials; | ||||
|         this.startTime = startTime; | ||||
|         this.endTime = endTime; | ||||
|         this.weekweekDay = weekweekDay; | ||||
|     } | ||||
| 
 | ||||
|     // Getters | ||||
|     public int getroomNumber() { | ||||
|         return roomNumberNumber; | ||||
|     } | ||||
| 
 | ||||
|     public String getteacherInitials() { | ||||
|         return teacherInitials; | ||||
|     } | ||||
| 
 | ||||
|     public String getStartTime() { | ||||
|         return startTime; | ||||
|     } | ||||
| 
 | ||||
|     public String getEndTime() { | ||||
|         return endTime; | ||||
|     } | ||||
| 
 | ||||
|     public String getweekDay() { | ||||
|         return weekweekDay; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										35
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Points.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Points.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | |||
| // Points class for managing point categories | ||||
| public class Points { | ||||
|     private int fiveMinuteBreak; | ||||
|     private int longerBreak; | ||||
|     private int bonusPoints; | ||||
| 
 | ||||
|     public int getFiveMinuteBreak() { | ||||
|         return fiveMinuteBreak; | ||||
|     } | ||||
| 
 | ||||
|     public void setFiveMinuteBreak(int fiveMinuteBreak) { | ||||
|         this.fiveMinuteBreak = fiveMinuteBreak; | ||||
|     } | ||||
| 
 | ||||
|     public int getLongerBreak() { | ||||
|         return longerBreak; | ||||
|     } | ||||
| 
 | ||||
|     public void setLongerBreak(int longerBreak) { | ||||
|         this.longerBreak = longerBreak; | ||||
|     } | ||||
| 
 | ||||
|     public int getBonusPoints() { | ||||
|         return bonusPoints; | ||||
|     } | ||||
| 
 | ||||
|     public void setBonusPoints(int bonusPoints) { | ||||
|         this.bonusPoints = bonusPoints; | ||||
|     } | ||||
| 
 | ||||
|     // Method to calculate total points | ||||
|     public int getTotalPoints() { | ||||
|         return fiveMinuteBreak + longerBreak + bonusPoints; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										44
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Teacher.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Code/Steiner/CO2-Daten-Projekt-V2/src/Teacher.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,44 @@ | |||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| public class Teacher { | ||||
|     private String name; | ||||
|     private Points points; | ||||
| 
 | ||||
|     public static final Map<String, String> nameMap = new HashMap<>(); | ||||
|     static { | ||||
|         nameMap.put("Hm", "Hummel"); | ||||
|         nameMap.put("Bd", "Bender"); | ||||
|         nameMap.put("Bu", "Burger"); | ||||
|         nameMap.put("Cg", "Chung"); | ||||
|         nameMap.put("Do", "Doe"); | ||||
|         nameMap.put("Eh", "Ehrlich"); | ||||
|         nameMap.put("Fh", "Fischer"); | ||||
|         nameMap.put("Gi", "Giordano"); | ||||
|         nameMap.put("Gr", "Graham"); | ||||
|         nameMap.put("Hi", "Higgins"); | ||||
|         nameMap.put("Kg", "Kang"); | ||||
|         nameMap.put("Kh", "Khan"); | ||||
|         nameMap.put("Lz", "Lozano"); | ||||
|         nameMap.put("Lu", "Lund"); | ||||
|         nameMap.put("Or", "Ortega"); | ||||
|         nameMap.put("Re", "Reyes"); | ||||
|         nameMap.put("Se", "Seng"); | ||||
|         nameMap.put("Ts", "Tanaka"); | ||||
|         nameMap.put("Vt", "Vetter"); | ||||
|         nameMap.put("Zu", "Zuniga"); | ||||
|     } | ||||
| 
 | ||||
|     public Teacher(String name) { | ||||
|         this.name = nameMap.getOrDefault(name, "Unknown"); | ||||
|         this.points = new Points(); | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public Points getPoints() { | ||||
|         return points; | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Sage The DM
						Sage The DM