fixed your code

This commit is contained in:
Patrick_Pluto 2024-11-26 11:47:06 +01:00
parent f240381468
commit e192af4647
10 changed files with 85 additions and 164 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,3 +1,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
@ -7,59 +10,34 @@ public class App {
private static final Scanner scanner = new Scanner(System.in);
private static final Teacher[] teachers = new Teacher[Teacher.nameMap.size()];
// Room channel numbers
private static final int ROOM_39_NUMBER = 1521262;
private static final int ROOM_38_NUMBER = 1364580;
private static final int ROOM_37_NUMBER = 1521263;
// Room channel numbers, sorted asc. 37, 38, 39
private static final int[] ROOM_NUMBERS = { 1521263, 1364580, 1521262 };
// Date and time constants for Start and End Dates
private static final String START_DATE = "2024-11-04";
private static final String END_DATE = "2024-11-08";
private static final String[] DATES = { "2024-11-04", "2024-11-05", "2024-11-06", "2024-11-07", "2024-11-08" };
private static ArrayList<Break[]> SHORT_BREAKS = new ArrayList<Break[]>();
private static ArrayList<Break[]> LONG_BREAKS = new ArrayList<Break[]>();
// #region Initialization
public static String generateLink(int channelNumber, String date, String startTime, String endTime) {
public static URL generateLink(int channelNumber, String date) throws MalformedURLException {
String baseUrl = "https://api.thingspeak.com/channels/";
String formattedStartDateTime = date + " " + startTime;
String formattedEndDateTime = date + " " + endTime;
String formattedStartDateTime = date + " 07:00:00";
String formattedEndDateTime = date + " 19:00:00";
return baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20")
+ "&end=" + formattedEndDateTime.replace(" ", "%20"); // %20 --> " "
return new URL(baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20")
+ "&end=" + formattedEndDateTime.replace(" ", "%20")); // %20 --> " "
}
private static double getDataAverageForMinute(int minute, int hour, int number, String date) {
// get the url
String startTime = "";
if (hour < 10) {
startTime += "0";
}
startTime += String.valueOf(hour);
startTime += ":";
if (minute < 10) {
startTime += "0";
}
startTime += String.valueOf(minute);
String calcStarttime = startTime + ":00";
String calcEndTime = startTime + "59";
String url = generateLink(number, date, calcStarttime, calcEndTime);
double averageCO2 = 0.0;
List<Co2Data> co2DataList = Co2Data.getData(url);
for (Co2Data co2Data : co2DataList) {
averageCO2 += co2Data.getCo2Level();
}
return averageCO2 / co2DataList.size(); // Example CO2 average level
}
private static void calculateBreakPoints(double[] minuteData, Break calcBreak, Teacher teacher) {
int duration = calcBreak.getEnd().getMinute() - calcBreak.getStart().getMinute();
private static void calculateBreakPoints(Break calcBreak, Teacher teacher) {
int duration = calcBreak.getEnd() - calcBreak.getStart();
Co2Data[] minuteData = calcBreak.getCo2Datas();
int breakPoints = minuteData.length;
if (duration == minuteData.length) {
for (int i = 0; i < duration; i++) {
if (minuteData[i] < minuteData[i + 1]) {
if (i + 1 != duration && (minuteData[i].getCo2Level() < minuteData[i + 1].getCo2Level())) {
breakPoints--;
}
}
@ -86,7 +64,6 @@ public class App {
}
}
// #region User Interaction
private static int getUserInput(String textOutput) {
System.out.println(textOutput);
@ -102,7 +79,7 @@ public class App {
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 pause.");
System.out.println("2. Up to 10 points for long pauses, depending on window usage.");
System.out.println("2. Up to 15-20 points for long pauses, depending on window usage.");
System.out.println("3. 5 bonus points for teacher switches in classrooms.");
}
@ -164,23 +141,32 @@ public class App {
}
// #region Main
public static void main(String[] args) {
public static void main(String[] args) throws MalformedURLException {
System.out.println("Calculations in process please do not shut off...");
initializeTeachers();
sortTeachers();
printTeachers();
ArrayList<Break> co2Datas = new ArrayList<Break>();
for (int classrooms = 0; classrooms < 3; classrooms++) {
for (int weekday = 0; weekday < 5; weekday++) {
// get the url and data
// calculate points
List<Co2Data> tempData = Co2Data.getData(generateLink(ROOM_NUMBERS[classrooms], DATES[weekday]));
SHORT_BREAKS.add(BreakSchedule.createBreaks(BreakSchedule.START_SMALL_BREAK,
BreakSchedule.END_SMALL_BREAK, tempData));
LONG_BREAKS.add(BreakSchedule.createBreaks(BreakSchedule.START_LONG_BREAK,
BreakSchedule.END_LONG_BREAK, tempData));
}
}
calculateBreakPoints(SHORT_BREAKS.get(0)[0], teachers[0]);
// Loop threw each day with a specific classroom and after the weekdays are over
// go to the next of the 3 classroms
// go threw every break calculate the point and give them to the teacher
// directly
// remember the point class
// breakShedule needed
// BreakSchedule needed
sortTeachers();
printTeachers();
while (true) {
int userInput = getUserInput(

View file

@ -1,56 +1,36 @@
public class Break {
private Time start;
private Time end;
private int start;
private int end;
private Co2Data co2Datas[];
public Break(Time start, Time end) {
this.start = start;
this.end = end;
}
public Time getStart() {
public int getStart() {
return start;
}
public void setStart(Time start) {
public void setStart(int start) {
this.start = start;
}
public Time getEnd() {
public int getEnd() {
return end;
}
public void setEnd(Time end) {
public void setEnd(int end) {
this.end = end;
}
/**
* Generates a Thingspeak API link based on channel number, date, and break
* period.
*
* @param channelNumber The Thingspeak channel number.
* @param date The date in format "YYYY-MM-DD".
* @param breakPeriod The Break object containing start and end times.
* @return A formatted URL for fetching data from the Thingspeak API.
*/
public static String generateLink(int channelNumber, String date, Break breakPeriod) {
String baseUrl = "https://api.thingspeak.com/channels/";
String formattedStartDateTime = date + " " + breakPeriod.getStart().toString();
String formattedEndDateTime = date + " " + breakPeriod.getEnd().toString();
return baseUrl + channelNumber + "/feeds.csv?start=" + formattedStartDateTime.replace(" ", "%20")
+ "&end=" + formattedEndDateTime.replace(" ", "%20");
public Co2Data[] getCo2Datas() {
return co2Datas;
}
public static void main(String[] args) {
// Example usage
Time start = Time.valueOf("00:00:00");
Time end = Time.valueOf("23:59:59");
Break breakPeriod = new Break(start, end);
String date = "2024-11-17";
int channelNumber = 1364580;
String link = Break.generateLink(channelNumber, date, breakPeriod);
System.out.println(link);
public void setCo2Datas(Co2Data[] co2Datas) {
this.co2Datas = co2Datas;
}
public Break(int start, int end, Co2Data[] co2Datas) {
this.start = start;
this.end = end;
this.co2Datas = co2Datas;
}
}

View file

@ -1,47 +1,34 @@
import java.util.List;
public class BreakSchedule {
private static final String[] START_SMALL_BREAK = {
"8:30", "10:25", "11:15", "12:05", "13:30", "14:20", "16:10", "17:00", "17:50"
public static final int[] START_SMALL_BREAK = {
830, 1025, 1115, 1205, 1330, 1420, 1610, 1700, 1750
};
private static final String[] END_SMALL_BREAK = {
"8:35", "10:30", "11:20", "12:10", "13:35", "14:25", "16:15", "17:05", "17:55"
public static final int[] END_SMALL_BREAK = {
835, 1030, 1120, 1210, 1335, 1425, 1615, 1705, 1755
};
private static final String[] START_LONG_BREAK = {
"9:20", "15:10"
public static final int[] START_LONG_BREAK = {
920, 1510
};
private static final String[] END_LONG_BREAK = {
"9:40", "15:25"
public static final int[] END_LONG_BREAK = {
940, 1525
};
private static final Break[] SMALL_BREAKS;
private static final Break[] LONG_BREAKS;
static {
SMALL_BREAKS = createBreaks(START_SMALL_BREAK, END_SMALL_BREAK);
LONG_BREAKS = createBreaks(START_LONG_BREAK, END_LONG_BREAK);
}
private static Break[] createBreaks(String[] startTimes, String[] endTimes) {
public static Break[] createBreaks(int[] startTimes, int[] endTimes, List<Co2Data> co2Data) {
Break[] breaks = new Break[startTimes.length];
for (int i = 0; i < startTimes.length; i++) {
// Append ":00" to include seconds
String startTimeWithSeconds = startTimes[i];
String endTimeWithSeconds = endTimes[i];
Time start = Time.valueOf(startTimeWithSeconds);
Time end = Time.valueOf(endTimeWithSeconds);
breaks[i] = new Break(start, end);
Co2Data[] co2Datas = new Co2Data[endTimes[i] - startTimes[i]];
for (Co2Data data : co2Data) {
if (data.getTime() >= startTimes[i] && data.getTime() < endTimes[i]) {
System.out.println(startTimes[i] + " " + endTimes[i] + " " + data.getTime());
co2Datas[data.getTime() - startTimes[i]] = data;
}
}
breaks[i] = new Break(startTimes[i], endTimes[i], co2Datas);
}
return breaks;
}
public static Break[] getSmallBreaks() {
return SMALL_BREAKS;
}
public static Break[] getLongBreaks() {
return LONG_BREAKS;
}
}

View file

@ -2,19 +2,19 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Co2Data {
private Date date;
private int time;
private int co2Level;
// Constructor
public Co2Data(Date date, int co2Level) {
this.date = date;
this.co2Level = co2Level;
this.time = date.getHour() * 100 + date.getMinute();
}
// Getters and Setters
@ -34,12 +34,19 @@ public class Co2Data {
this.co2Level = co2Level;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
// Fetch and parse data from a CSV URL
public static List<Co2Data> getData(String csvURL) {
public static List<Co2Data> getData(URL url) {
List<Co2Data> dataList = new ArrayList<>();
try {
URL url = new URL(csvURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/csv");
@ -52,6 +59,7 @@ public class Co2Data {
br.readLine(); // Skip header line
String output;
System.out.println(url);
while ((output = br.readLine()) != null) {
Co2Data data = parseData(output);
if (data != null) {
@ -73,11 +81,7 @@ public class Co2Data {
String[] parts = line.split(",");
String dateStr = parts[0].trim(); // assuming date is in the first column
int co2Level = Integer.parseInt(parts[1].trim()); // assuming CO2 level is in the second column
// Convert the date string into a Date object (assumes CSV date format is
// yyyy-MM-dd HH:mm:ss)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateStr);
Date date = new Date(dateStr);
return new Co2Data(date, co2Level);
} catch (Exception e) {
@ -85,4 +89,5 @@ public class Co2Data {
return null;
}
}
}

View file

@ -1,37 +0,0 @@
public class Time {
private int hour;
private int minute;
public Time(int hour, int minute) {
if (hour >= 1 && hour <= 24 && minute >= 1 && minute <= 60) {
this.hour = hour;
this.minute = minute;
} else {
System.out.println("Failed to initialize the hour");
}
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public static Time valueOf(String time) {
String[] parts = time.split(":");
int hour = Integer.parseInt(parts[0]);
int minute = Integer.parseInt(parts[1]);
Time returnValue = new Time(hour, minute);
return returnValue;
}
}