Commented the code

This commit is contained in:
Sage The DM 2024-11-26 20:42:04 +01:00
parent cc1fb07f90
commit 7c6c6b2c2c
24 changed files with 55 additions and 24 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
src/App.class Normal file

Binary file not shown.

View file

@ -48,6 +48,13 @@ public class App {
teacher.addPoints(breakPoints, 0, 0); teacher.addPoints(breakPoints, 0, 0);
} }
// if it is smaller than 2 points the teacher does not deserve bonus points
if (breakPoints > 2) {
calculateBonusPoints(calcBreak, teacher, nextTeacher);
}
}
private static void calculateBonusPoints(Break calcBreak, Teacher teacher, Teacher nextTeacher) {
if (teacher.getShortName() != nextTeacher.getShortName()) { if (teacher.getShortName() != nextTeacher.getShortName()) {
teacher.addPoints(0, 0, 5); teacher.addPoints(0, 0, 5);
} }
@ -112,6 +119,7 @@ public class App {
while (true) { while (true) {
int userInput = getUserInput("Do you want to see how the points were calculated? (1 = Yes, 0 = No)"); int userInput = getUserInput("Do you want to see how the points were calculated? (1 = Yes, 0 = No)");
if (userInput == 1) { if (userInput == 1) {
printTeachers(true);
printExplanation(); printExplanation();
break; break;
} else if (userInput == 0) { } else if (userInput == 0) {
@ -150,7 +158,7 @@ public class App {
(a, b) -> Integer.compare(b.getTotalPoints(), a.getTotalPoints())); (a, b) -> Integer.compare(b.getTotalPoints(), a.getTotalPoints()));
} }
private static void printTeachers() { private static void printTeachers(boolean detailed) {
int rank = 1; int rank = 1;
int previousPoints = -1; int previousPoints = -1;
@ -162,7 +170,11 @@ public class App {
rank = i + 1; rank = i + 1;
previousPoints = teacherPoints; previousPoints = teacherPoints;
} }
System.out.println(rank + ". " + teacher.getName() + " - Points: " + teacherPoints); if (detailed) {
System.out.println(rank + ". " + teacher.getName() + " - " + teacher.detailedPointString());
} else {
System.out.println(rank + ". " + teacher.getName() + " - Points: " + teacherPoints);
}
} }
} }
@ -233,14 +245,14 @@ public class App {
calculateBreakPoints(longBreaks[i], teacher, nextTeacher); calculateBreakPoints(longBreaks[i], teacher, nextTeacher);
} }
} }
} catch (Exception e) { } catch (MalformedURLException e) {
System.out.println("Error processing data for date: " + date + ". " + e.getMessage()); System.out.println("Error processing data for date: " + date + ". " + e);
} }
} }
} }
sortTeachers(); sortTeachers();
printTeachers(); printTeachers(false);
interactWithUser(); interactWithUser();
} catch (Exception e) { } catch (Exception e) {

BIN
src/Break.class Normal file

Binary file not shown.

View file

@ -1,8 +1,10 @@
public class Break { public class Break {
// #region Fields
private int start; private int start;
private int end; private int end;
private Co2Data co2Datas[]; private Co2Data[] co2Datas;
// #region Getters and Setters
public int getStart() { public int getStart() {
return start; return start;
} }
@ -27,10 +29,10 @@ public class Break {
this.co2Datas = co2Datas; this.co2Datas = co2Datas;
} }
// #region Constructor
public Break(int start, int end, Co2Data[] co2Datas) { public Break(int start, int end, Co2Data[] co2Datas) {
this.start = start; this.start = start;
this.end = end; this.end = end;
this.co2Datas = co2Datas; this.co2Datas = co2Datas;
} }
} }

BIN
src/BreakSchedule.class Normal file

Binary file not shown.

View file

@ -1,6 +1,7 @@
import java.util.List; import java.util.List;
public class BreakSchedule { public class BreakSchedule {
// #region Break Times Constants
public static final int[] START_SMALL_BREAK = { public static final int[] START_SMALL_BREAK = {
830, 1025, 1115, 1205, 1330, 1420, 1610, 1700, 1750 830, 1025, 1115, 1205, 1330, 1420, 1610, 1700, 1750
}; };
@ -17,6 +18,7 @@ public class BreakSchedule {
940, 1525 940, 1525
}; };
// #region Create Breaks Method
public static Break[] createBreaks(int[] startTimes, int[] endTimes, List<Co2Data> co2Data) { public static Break[] createBreaks(int[] startTimes, int[] endTimes, List<Co2Data> co2Data) {
Break[] breaks = new Break[startTimes.length]; Break[] breaks = new Break[startTimes.length];
for (int i = 0; i < startTimes.length; i++) { for (int i = 0; i < startTimes.length; i++) {
@ -30,4 +32,4 @@ public class BreakSchedule {
} }
return breaks; return breaks;
} }
} }

BIN
src/Co2Data.class Normal file

Binary file not shown.

View file

@ -6,10 +6,12 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Co2Data { public class Co2Data {
// #region Fields
private Date date; private Date date;
private int time; private int time;
private int co2Level; private int co2Level;
// #region Constructor
// Constructor // Constructor
public Co2Data(Date date, int co2Level) { public Co2Data(Date date, int co2Level) {
this.date = date; this.date = date;
@ -17,6 +19,7 @@ public class Co2Data {
this.time = date.getHour() * 100 + date.getMinute(); this.time = date.getHour() * 100 + date.getMinute();
} }
// #region Getters and Setters
// Getters and Setters // Getters and Setters
public Date getDate() { public Date getDate() {
return date; return date;
@ -42,6 +45,7 @@ public class Co2Data {
this.time = time; this.time = time;
} }
// #region Data Fetching
// Fetch and parse data from a CSV URL // Fetch and parse data from a CSV URL
public static List<Co2Data> getData(URL url) { public static List<Co2Data> getData(URL url) {
List<Co2Data> dataList = new ArrayList<>(); List<Co2Data> dataList = new ArrayList<>();
@ -74,6 +78,7 @@ public class Co2Data {
return dataList; return dataList;
} }
// #region Data Parsing
// Helper method to parse CSV data and create a Co2Data object // Helper method to parse CSV data and create a Co2Data object
private static Co2Data parseData(String line) { private static Co2Data parseData(String line) {
try { try {
@ -88,5 +93,4 @@ public class Co2Data {
return null; return null;
} }
} }
} }

BIN
src/Date.class Normal file

Binary file not shown.

View file

@ -1,10 +1,12 @@
public class Date { public class Date {
// #region Fields
private int year; private int year;
private int month; private int month;
private int day; private int day;
private int hour; private int hour;
private int minute; private int minute;
// #region Constructors
// Constructor to parse date string // Constructor to parse date string
public Date(String dateStr) { public Date(String dateStr) {
String[] dateTime = dateStr.split(" "); String[] dateTime = dateStr.split(" ");
@ -33,6 +35,7 @@ public class Date {
adjustForSwitzerlandTime(); adjustForSwitzerlandTime();
} }
// #region Time Adjustment
// Adjust time for Switzerland timezone (UTC+1 or UTC+2 during daylight saving // Adjust time for Switzerland timezone (UTC+1 or UTC+2 during daylight saving
// time) // time)
private void adjustForSwitzerlandTime() { private void adjustForSwitzerlandTime() {
@ -46,7 +49,7 @@ public class Date {
} }
} }
// Getter methods // #region Getters
public int getYear() { public int getYear() {
return year; return year;
} }
@ -67,6 +70,7 @@ public class Date {
return minute; return minute;
} }
// #region String Representation
// Method to print date in a readable format // Method to print date in a readable format
public String toString() { public String toString() {
return String.format("%04d-%02d-%02d %02d:%02d", year, month, day, hour, minute); return String.format("%04d-%02d-%02d %02d:%02d", year, month, day, hour, minute);

BIN
src/Points.class Normal file

Binary file not shown.

View file

@ -4,7 +4,7 @@ public class Points {
private int longerBreak; private int longerBreak;
private int bonusPoints; private int bonusPoints;
// #region Constructor // #region Constructors
public Points() { public Points() {
this.fiveMinuteBreak = 0; // Default initialization this.fiveMinuteBreak = 0; // Default initialization
this.longerBreak = 0; // Default initialization this.longerBreak = 0; // Default initialization
@ -73,7 +73,7 @@ public class Points {
return fiveMinuteBreak + longerBreak + bonusPoints; return fiveMinuteBreak + longerBreak + bonusPoints;
} }
// #region String Representation (Optional for debugging or output) // #region String Representation
@Override @Override
public String toString() { public String toString() {
return String.format("Five Minute Break: %d, Longer Break: %d, Bonus Points: %d, Total: %d", return String.format("Five Minute Break: %d, Longer Break: %d, Bonus Points: %d, Total: %d",

BIN
src/Teacher.class Normal file

Binary file not shown.

View file

@ -2,19 +2,19 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class Teacher { public class Teacher {
// #region Instance Attributes
public static final Map<String, String> nameMap = new HashMap<>(); public static final Map<String, String> nameMap = new HashMap<>();
private String shortName; private String shortName;
private String name; private String name;
private int totalPoints; private Points points;
// Constructor
public Teacher(String shortName) { public Teacher(String shortName) {
this.shortName = shortName; this.shortName = shortName;
this.name = nameMap.get(shortName); this.name = nameMap.get(shortName);
this.totalPoints = 0; this.points = new Points();
} }
// Getters // #region Getters
public String getShortName() { public String getShortName() {
return shortName; return shortName;
} }
@ -24,16 +24,19 @@ public class Teacher {
} }
public int getTotalPoints() { public int getTotalPoints() {
return totalPoints; return this.points.getTotalPoints();
} }
// Setters // #region Public Methods
public void setTotalPoints(int points) {
this.totalPoints = points;
}
// Method to add points
public void addPoints(int smallBreakPoints, int longBreakPoints, int bonusPoints) { public void addPoints(int smallBreakPoints, int longBreakPoints, int bonusPoints) {
this.totalPoints += smallBreakPoints + longBreakPoints + bonusPoints; points.addBonusPoints(bonusPoints);
points.addFiveMinuteBreakPoints(smallBreakPoints);
points.addLongerBreakPoints(longBreakPoints);
}
public String detailedPointString() {
return "5 minute points: " + this.points.getFiveMinuteBreak() + "\nlonger break points: "
+ this.points.getLongerBreak() + "\nBonus points: " + this.points.getBonusPoints()
+ "\nTotal points: " + this.points.getTotalPoints() + "\n-------------\n";
} }
} }

BIN
src/TimeTable.class Normal file

Binary file not shown.

View file

@ -1,11 +1,14 @@
public class TimeTable { public class TimeTable {
// #region Attributes
private String[][] shortTeacher; private String[][] shortTeacher;
// #region Constructor
public TimeTable(int roomIndex) { public TimeTable(int roomIndex) {
shortTeacher = new String[11][5]; // 11 lessons, 5 weekdays shortTeacher = new String[11][5]; // 11 lessons, 5 weekdays
initializeTimeTable(roomIndex); initializeTimeTable(roomIndex);
} }
// #region Initialization Methods
private void initializeTimeTable(int roomIndex) { private void initializeTimeTable(int roomIndex) {
switch (roomIndex) { switch (roomIndex) {
case 0: // Room 37 case 0: // Room 37
@ -70,6 +73,7 @@ public class TimeTable {
}; };
} }
// #region Public Methods
public String getLesson(int lesson, int day) { public String getLesson(int lesson, int day) {
if (lesson < 0 || lesson >= 11 || day < 0 || day >= 5) { if (lesson < 0 || lesson >= 11 || day < 0 || day >= 5) {
throw new IllegalArgumentException("Invalid lesson or day"); throw new IllegalArgumentException("Invalid lesson or day");