Project work

This commit is contained in:
Sage The DM 2024-11-05 14:52:41 +01:00
parent b806b03aba
commit e45d52037f
9 changed files with 173 additions and 80 deletions

View file

@ -1,32 +1,24 @@
import java.util.List;
import java.util.Arrays; import java.util.Arrays;
import java.util.Scanner; import java.util.Scanner;
// Main application class
public class App { public class App {
private static final Scanner scanner = new Scanner(System.in); // Initialize the scanner private static final Scanner scanner = new Scanner(System.in); // Initialize the scanner
// Constants for rooms and timetable dimensions // Constants for rooms and timetable dimensions
private static final int ROOM_COUNT = 3; private static final int ROOM_COUNT = 3;
private static final int DAY_COUNT = 5; private static final int DAY_COUNT = 5;
private static final int LESSON_COUNT = 12; private static final int LESSON_COUNT = 12; // Updated to match the lengths of START_TIMES and END_TIMES
// Data sources for different rooms
private static final List<Data> room39Data = Data.getData("https://api.thingspeak.com/channels/1521262/feeds.csv",
39);
private static final List<Data> room38Data = Data.getData("https://api.thingspeak.com/channels/1364580/feeds.csv",
38);
private static final List<Data> room37Data = Data.getData("https://api.thingspeak.com/channels/1521263/feeds.csv",
37);
// Time table // Time table
public static final Lesson[][][] timeTable = new Lesson[ROOM_COUNT][DAY_COUNT][LESSON_COUNT]; public static final Lesson[][][] timeTable = new Lesson[ROOM_COUNT][DAY_COUNT][LESSON_COUNT];
// Teacher initials and array // Teacher initials and array
private static final String[] TEACHER_INITIALS = { private static final String[] TEACHER_INITIALS = {
"", "Bd", "Bu", "Cg", "Di", "Do", "Eh", "Es", "Fh", "Gi", "Bd", "Bu", "Cg", "Do", "Eh", "Fh", "Gi", "Gr", "Hm", "Hi",
"Gr", "Hm", "Hi", "Kg", "Kh", "Lz", "Lu", "Or", "Re", "Se", "Kg", "Kh", "Lz", "Lu", "Or", "Re", "Se", "Ts", "Vt", "Zu"
"Ts", "Vt", "Zu"
}; };
private static final Teacher[] teachers = new Teacher[TEACHER_INITIALS.length]; private static final Teacher[] teachers = new Teacher[TEACHER_INITIALS.length];
// Initialization of teachers // Initialization of teachers
@ -45,14 +37,28 @@ public class App {
// Calculate points based on criteria // Calculate points based on criteria
private static void calculatePoints() { private static void calculatePoints() {
// TODO: Implement point calculation logic based on specific criteria. for (int day = 0; day < DAY_COUNT; day++) {
// Example: If a teacher opens the window during a small break (entire break 5 for (int room = 0; room < ROOM_COUNT; room++) {
// points - can for (int lessonIndex = 0; lessonIndex < LESSON_COUNT - 1; lessonIndex++) {
// be reduced by the amount the window was open). Lesson currentLesson = timeTable[room][day][lessonIndex];
// Maximum points for a long break: 10 points. Lesson nextLesson = timeTable[room][day][lessonIndex + 1];
// Plus 5 bonus points if the teachers are switching after.
// Ensure both current and next lessons are not null
if (currentLesson != null && nextLesson != null) {
// Calculate points for the current lesson based on the next lesson
int points = currentLesson.calculatePoints(nextLesson);
// Update the points for the teacher associated with the current lesson
String currentTeacherName = currentLesson.getTeacherName();
for (Teacher teacher : teachers) { for (Teacher teacher : teachers) {
teacher.setPoints(0); if (teacher.getName().equals(currentTeacherName)) {
teacher.addPoints(points); // Update teacher points
break;
}
}
}
}
}
} }
} }
@ -64,22 +70,20 @@ public class App {
// Print the teachers and their points // Print the teachers and their points
private static void printTeachers() { private static void printTeachers() {
System.out.println("Teachers and their points:"); System.out.println("Teachers and their points:");
int rank = 1;
int rank = 1; // Start with rank 1 int previousPoints = -1;
int previousPoints = -1; // Track the points of the previous teacher int currentRankCount = 0;
int currentRankCount = 0; // Count how many teachers share the same rank
for (Teacher teacher : teachers) { for (Teacher teacher : teachers) {
if (teacher.getPoints() != previousPoints) { if (teacher.getPoints() != previousPoints) {
rank += currentRankCount; // Update rank if points are different rank += currentRankCount;
currentRankCount = 1; // Reset count for the new points currentRankCount = 1;
} else { } else {
currentRankCount++; // Increment count for same points currentRankCount++;
} }
// Print the teacher with their rank and points
System.out.printf("%d. %s - %d points%n", rank, teacher.getName(), teacher.getPoints()); System.out.printf("%d. %s - %d points%n", rank, teacher.getName(), teacher.getPoints());
previousPoints = teacher.getPoints(); // Update previous points previousPoints = teacher.getPoints();
} }
} }
@ -88,17 +92,18 @@ public class App {
System.out.println(textOutput); System.out.println(textOutput);
while (!scanner.hasNextInt()) { while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a number."); System.out.println("Invalid input. Please enter a number.");
scanner.next(); // Clear the invalid input scanner.next();
} }
return scanner.nextInt(); // Read user input return scanner.nextInt();
} }
// Print explanation of point calculations // Print explanation of point calculations
private static void printExplanation() { private static void printExplanation() {
System.out.println("Point calculation explanation:"); System.out.println("Point calculation explanation:");
System.out.println("1. 5 points for keeping the window open during a small break."); 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, with deductions for window usage."); System.out.println(
System.out.println("3. Bonus points for switching teachers."); "2. Up to 10 points for long breaks, depending on the duration the window is open and deductions for usage.");
System.out.println("3. Additional 5 bonus points for switching teachers if another teacher is in the room.");
} }
// Print shutdown animation // Print shutdown animation
@ -107,7 +112,7 @@ public class App {
for (int i = 3; i > 0; i--) { for (int i = 3; i > 0; i--) {
System.out.print(i + "..."); System.out.print(i + "...");
try { try {
Thread.sleep(1000); // Sleep for 1 second Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -131,10 +136,9 @@ public class App {
} else if (userInput == 0) { } else if (userInput == 0) {
printShutDown(); printShutDown();
} else { } else {
// Handle invalid input
System.out.println("Invalid input. Please enter 1 for Yes or 0 for No."); System.out.println("Invalid input. Please enter 1 for Yes or 0 for No.");
} }
scanner.close(); // Close the scanner to avoid resource leaks scanner.close();
} }
} }

View file

@ -1,6 +1,5 @@
public class FillTable { public class FillTable {
// Define start and end times as class variables
// #region Class Variables
private static final String[] START_TIMES = { private static final String[] START_TIMES = {
"7:45", "8:35", "9:40", "10:30", "11:20", "12:10", "12:50", "7:45", "8:35", "9:40", "10:30", "11:20", "12:10", "12:50",
"13:35", "14:25", "15:15", "16:15", "17:05" "13:35", "14:25", "15:15", "16:15", "17:05"
@ -11,14 +10,13 @@ public class FillTable {
"14:20", "15:10", "16:10", "17:00", "17:50" "14:20", "15:10", "16:10", "17:00", "17:50"
}; };
// #region fillTable private static void fillTable(String[] teacherShortNames, String day, String[] startTime, String[] endTime,
// Example method to fill a timetable with teachers
private static void fillTable(String[] teacherNames, String day, String[] startTime, String[] endTime,
int roomIndex) { int roomIndex) {
int dayIndex = getDayIndex(day); int dayIndex = getDayIndex(day);
for (int i = 0; i < teacherNames.length; i++) { for (int i = 0; i < teacherShortNames.length && i < startTime.length && i < endTime.length; i++) {
// Room is determined by the caller method Teacher teacher = new Teacher(teacherShortNames[i]); // Initialize Teacher with shortform
App.timeTable[roomIndex][dayIndex][i] = new Lesson(37, teacherNames[i], startTime[i], endTime[i], day); App.timeTable[roomIndex][dayIndex][i] = new Lesson(roomIndex, teacher.getName(), startTime[i], endTime[i],
day);
} }
} }
@ -35,36 +33,49 @@ public class FillTable {
case "Friday": case "Friday":
return 4; return 4;
default: default:
return -1; // Invalid day return -1;
} }
} }
// Fill 37 timetable
// #region table 37
static void fill37TimeTable() { static void fill37TimeTable() {
int roomIndex = 0; int roomIndex = 0;
fillTable(new String[] { "Hm", "Py", "Hi", "Hm", "Le", "", "Gi", "Gi", "D", "Ts", "Ts", "" }, fillTable(new String[] { "Hm", "Hm", "Hi", "Hm", "Hm", "Lunch", "Bd", "Gi", "Gi", "Ts", "Ts", "" },
"Monday", START_TIMES, END_TIMES, roomIndex); "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);
} }
// Fill Room 38 timetable
// #region table 38
static void fill38TimeTable() { static void fill38TimeTable() {
int roomIndex = 1; int roomIndex = 1;
fillTable(new String[] { "Bz", "Bz", "Bz", "Bz", "Bz", "", "Hn", "Hn", "Bu", "Bu", "Hn", "Hn" }, fillTable(new String[] { "Bz", "Bz", "Bz", "Bz", "Bz", "Lunch", "Lunch", "Hn", "Hn", "Bu", "Hn", "Hn" },
"Monday", START_TIMES, END_TIMES, roomIndex); "Monday", START_TIMES, END_TIMES, roomIndex);
fillTable(new String[] { "Br", "Kg", "Kh", "Re", "Es", "", "Bt", "EW", "FR", "FR", "VW", "VW" }, fillTable(new String[] { "Kg", "Kg", "Eh", "Re", "Re", "Lunch", "Lunch", "Bt", "Kh", "Kh", "", "" },
"Tuesday", START_TIMES, END_TIMES, roomIndex); "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);
} }
// Fill Room 39 timetable
// #region table 39
static void fill39TimeTable() { static void fill39TimeTable() {
int roomIndex = 2; int roomIndex = 2;
fillTable(new String[] { "", "", "", "", "", "", "", "", "", "", "", "" }, fillTable(new String[] { "Bd", "Bd", "Bd", "Bd", "Bd", "Lunch", "Lunch", "Lu", "Lu", "Lu", "Lu", "" },
"Monday", START_TIMES, END_TIMES, roomIndex); "Monday", START_TIMES, END_TIMES, roomIndex);
fillTable(new String[] { "", "", "", "", "", "", "", "", "", "", "", "" }, fillTable(new String[] { "Do", "Do", "Zu", "Zu", "Zu", "Lunch", "Lunch", "Se", "Se", "Se", "Se", "" },
"Tuesday", START_TIMES, END_TIMES, roomIndex); "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);
} }
} }

View file

@ -1,3 +1,9 @@
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.List;
public class Lesson { public class Lesson {
private int room; private int room;
private String teacherName; private String teacherName;
@ -5,6 +11,20 @@ public class Lesson {
private String endTime; private String endTime;
private String day; private String day;
// CO2 level tracking
private List<Integer> co2Levels; // Assuming this is provided during breaks
private static final int SMALL_BREAK_POINTS = 5;
private static final int BIG_BREAK_POINT_PER_MINUTE = 1;
// Define a DateTimeFormatter that accepts both 'HH:mm' and 'H:mm'
private static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder()
.appendPattern("H:mm") // Matches '8:30'
.optionalStart()
.appendPattern("HH:mm") // Matches '08:30'
.optionalEnd()
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) // Set default minutes to 0 if not provided
.toFormatter();
// Constructor to initialize all fields // Constructor to initialize all fields
public Lesson(int room, String teacherName, String startTime, String endTime, String day) { public Lesson(int room, String teacherName, String startTime, String endTime, String day) {
this.room = room; this.room = room;
@ -14,6 +34,7 @@ public class Lesson {
this.day = day; this.day = day;
} }
// Getters
public int getRoom() { public int getRoom() {
return room; return room;
} }
@ -34,20 +55,76 @@ public class Lesson {
return day; return day;
} }
public boolean isBreak() { public boolean isBreak(Lesson nextLesson) {
// Logic if it is between lessons LocalTime thisEndTime = LocalTime.parse(endTime, TIME_FORMATTER);
return false; LocalTime nextStartTime = LocalTime.parse(nextLesson.getStartTime(), TIME_FORMATTER);
return thisEndTime.equals(nextStartTime);
} }
public boolean isBigBreak() { public boolean isBigBreak(Lesson nextLesson) {
// is the break longer than 5 minutes LocalTime thisEndTime = LocalTime.parse(endTime, TIME_FORMATTER);
// But was not the last lesson of the day LocalTime nextStartTime = LocalTime.parse(nextLesson.getStartTime(), TIME_FORMATTER);
// Is not Lunch break long breakDuration = java.time.Duration.between(thisEndTime, nextStartTime).toMinutes();
return false; boolean notLastLesson = !nextLesson.getEndTime().equals("17:00");
boolean isLunchBreak = "Lunch".equals(nextLesson.getTeacherName());
return breakDuration > 5 && notLastLesson && !isLunchBreak;
} }
public boolean isTeacherSwitch() { public boolean isTeacherSwitch(Lesson nextLesson) {
// is a another teacher in this room return room == nextLesson.getRoom() && !teacherName.equals(nextLesson.getTeacherName());
return false; }
public void setCo2Levels(List<Integer> co2Levels) {
this.co2Levels = co2Levels;
}
public int calculatePoints(Lesson nextLesson) {
int points = 0;
// Check for small break
if (isBreak(nextLesson)) {
points += calculateSmallBreakPoints();
}
// Check for long break
if (isBigBreak(nextLesson)) {
points += calculateBigBreakPoints();
}
// Check for teacher switch
if (isTeacherSwitch(nextLesson)) {
points += 5; // Additional 5 points for switching teachers
}
return points;
}
private int calculateSmallBreakPoints() {
if (co2Levels == null || co2Levels.size() < 5)
return 0;
boolean continuousDecrease = true;
for (int i = 1; i < co2Levels.size(); i++) {
if (co2Levels.get(i) >= co2Levels.get(i - 1)) {
continuousDecrease = false;
break;
}
}
return continuousDecrease ? SMALL_BREAK_POINTS : 0;
}
private int calculateBigBreakPoints() {
if (co2Levels == null)
return 0;
int points = 0;
for (int i = 1; i < co2Levels.size(); i++) {
if (co2Levels.get(i) < co2Levels.get(i - 1)) {
points += BIG_BREAK_POINT_PER_MINUTE;
}
}
return points;
} }
} }

View file

@ -11,14 +11,11 @@ public class Teacher {
// Static block to initialize the name mappings // Static block to initialize the name mappings
static { static {
nameMap.put("Hm", "Hummel"); nameMap.put("Hm", "Hummel");
nameMap.put("", "Bäcker");
nameMap.put("Bd", "Bender"); nameMap.put("Bd", "Bender");
nameMap.put("Bu", "Burger"); nameMap.put("Bu", "Burger");
nameMap.put("Cg", "Chung"); nameMap.put("Cg", "Chung");
nameMap.put("Di", "Dimitrov");
nameMap.put("Do", "Doe"); nameMap.put("Do", "Doe");
nameMap.put("Eh", "Ehrlich"); nameMap.put("Eh", "Ehrlich");
nameMap.put("Es", "Esposito");
nameMap.put("Fh", "Fischer"); nameMap.put("Fh", "Fischer");
nameMap.put("Gi", "Giordano"); nameMap.put("Gi", "Giordano");
nameMap.put("Gr", "Graham"); nameMap.put("Gr", "Graham");
@ -33,17 +30,17 @@ public class Teacher {
nameMap.put("Ts", "Tanaka"); nameMap.put("Ts", "Tanaka");
nameMap.put("Vt", "Vetter"); nameMap.put("Vt", "Vetter");
nameMap.put("Zu", "Zuniga"); nameMap.put("Zu", "Zuniga");
// Add more mappings as needed // No additional mappings beyond those present in the timetable
} }
// Constructor // Constructor
public Teacher(String name) { public Teacher(String name) {
setName(name); setName(name);
points = 0; points = 0; // Initialize points to zero
} }
public String getName() { public String getName() {
return name; return name; // Return the full name of the teacher
} }
public void setName(String name) { public void setName(String name) {
@ -51,10 +48,14 @@ public class Teacher {
} }
public int getPoints() { public int getPoints() {
return points; return points; // Return the current points
} }
public void setPoints(int points) { public void setPoints(int points) {
this.points = points; this.points = points; // Set the points to a specific value
}
public void addPoints(int points) {
this.points += points; // Add points to the current total
} }
} }