Smaller task & refactoring co2

This commit is contained in:
Sage The DM 2024-11-06 15:41:49 +01:00
parent e45d52037f
commit 1464df11cf
29 changed files with 683 additions and 0 deletions

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

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.

View 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();
}
}

View 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;
}
}
}

View 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);
}
}

View 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);
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,71 @@
import java.util.ArrayList;
import java.util.Collections;
public class App {
public static void main(String[] args) {
int[] array = { 0, 1, 2, 3, 4, 5, 4, -3, -2, 0 };
ArrayList<Integer> list = new ArrayList<>();
initializeListFromArray(array, list);
printList("ArrayList vor Modifikationen:", list);
addValues(list, 10, -5);
removeElementAt(list, 1);
printList("ArrayList nach Modifikationen:", list);
sortList(list);
printList("ArrayList nach Sortierung:", list);
shuffleList(list);
printList("ArrayList nach dem Mischen:", list);
printMinMaxValues(list);
}
// Initialisiert die ArrayList mit Werten aus dem Array
private static void initializeListFromArray(int[] array, ArrayList<Integer> list) {
for (int num : array) {
list.add(num);
}
}
// Fügt der ArrayList zwei Werte hinzu
private static void addValues(ArrayList<Integer> list, int value1, int value2) {
list.add(value1);
list.add(value2);
}
// Entfernt das Element an einer bestimmten Position
private static void removeElementAt(ArrayList<Integer> list, int index) {
if (index >= 0 && index < list.size()) {
list.remove(index);
}
}
// Gibt den Inhalt der ArrayList aus
private static void printList(String message, ArrayList<Integer> list) {
System.out.println(message);
for (int num : list) {
System.out.print(num + " ");
}
System.out.println();
}
// Sortiert die ArrayList
private static void sortList(ArrayList<Integer> list) {
Collections.sort(list);
}
// Mischt die ArrayList
private static void shuffleList(ArrayList<Integer> list) {
Collections.shuffle(list);
}
// Gibt den maximalen und minimalen Wert der ArrayList aus
private static void printMinMaxValues(ArrayList<Integer> list) {
int max = Collections.max(list);
int min = Collections.min(list);
System.out.println("Maximaler Wert: " + max);
System.out.println("Minimaler Wert: " + min);
}
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,28 @@
// Main application class where we create a Teacher object and display some
// info.
public class App {
public static void main(String[] args) throws Exception {
// Sample data for creating a Teacher object.
String firstname = "Karl";
String lastname = "Marx";
int compensation = 1000000;
String[] subjects = { "Economics", "Math", "German" };
// Creating a Teacher object using the data above.
Teacher teacher = new Teacher(firstname, lastname, compensation, subjects);
// Printing the teacher's full name.
System.out.println("Teacher: " + teacher.getFirstname() + " " + teacher.getLastname());
// Printing the teacher's compensation.
System.out.println("Compensation: " + teacher.getCompensation());
// Printing the subjects the teacher teaches.
System.out.print("Subjects: ");
// Loop through each subject in the array and print it.
for (String subject : teacher.getSubjects()) {
System.out.print(subject + " ");
}
}
}

Binary file not shown.

View file

@ -0,0 +1,61 @@
// The Teacher class represents a teacher with a first name, last name, compensation, and subjects they teach.
public class Teacher {
// Private fields to store the teacher's first name, last name, compensation,
// and subjects.
private String firstname;
private String lastname;
private int compensation;
private String[] subjects;
// Constructor for initializing a Teacher object with provided details.
public Teacher(String firstname, String lastname, int compensation, String[] subjects) {
// Assigning the provided first name to the teacher's firstname field.
this.firstname = firstname;
// Assigning the provided last name to the teacher's lastname field.
this.lastname = lastname;
// Assigning the provided compensation to the teacher's compensation field.
this.compensation = compensation;
// Assigning the provided array of subjects to the teacher's subjects field.
this.subjects = subjects;
}
// Getter method for getting the teacher's first name.
public String getFirstname() {
return firstname;
}
// Setter method for setting a new first name for the teacher.
public void setFirstname(String firstname) {
this.firstname = firstname;
}
// Getter method for getting the teacher's last name.
public String getLastname() {
return lastname;
}
// Setter method for setting a new last name for the teacher.
public void setLastname(String lastname) {
this.lastname = lastname;
}
// Getter method for getting the teacher's compensation.
public int getCompensation() {
return compensation;
}
// Setter method for setting a new compensation for the teacher.
public void setCompensation(int compensation) {
this.compensation = compensation;
}
// Getter method for getting the array of subjects the teacher teaches.
public String[] getSubjects() {
return subjects;
}
// Setter method for setting a new array of subjects for the teacher.
public void setSubjects(String[] subjects) {
this.subjects = subjects;
}
}