Skip to content

Commit

Permalink
Codes, License, R3.09, Apprentissage BUT2 (08/06/2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
EDM115 committed Jun 8, 2024
1 parent 268ec2e commit 0c92977
Show file tree
Hide file tree
Showing 113 changed files with 44,933 additions and 2 deletions.
67 changes: 67 additions & 0 deletions BUT2/Codes/S4/R4.01/TP1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>model</artifactId>
<groupId>fr.ubs.sporttrack</groupId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>SportTrack</name>
<description>On fait du sport ?</description>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>fr.ubs.sporttrack.model.Main</mainClass>
<arguments>
<argument>src\main\resources\data.json</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>fr.ubs.sporttrack.model.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package fr.ubs.sporttrack.model;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Size;
import org.json.JSONObject;
import org.json.JSONArray;

import java.util.List;
import java.util.ArrayList;

public class Activity {
private static final String DATE_FIELD = "date";
private static final String DESC_FIELD = "description";
private static final String DISTANCE_FIELD = "distance";
private static final String FREQ_MIN_FIELD = "freq_min";
private static final String FREQ_MAX_FIELD = "freq_max";
private static final String DATA_FIELD = "data";

@NotNull
private String date;

@Size(min = 0, max = 200)
private String description;

@Min(0)
private int distance;

@Min(0)
private int freqMin;

@Min(0) @Max(200)
private int freqMax;

@NotNull
private List<@NotNull Data> data;

public Activity() {}

public Activity(String date, String desc, int dist, int fmin, int fmax, List<Data> data) {
this.date = date;
this.description = desc;
this.distance = dist;
this.freqMin= fmin;
this.freqMax = fmax;
this.data = data;
}

public String getDate() {
return this.date;
}

public String getDescription() {
return this.description;
}

public int getFreqMin() {
return this.freqMin;
}

public int getFreqMax() {
return this.freqMax;
}

public int getDistance() {
return this.distance;
}

public List<Data> getData() {
return this.data;
}

public static Activity fromJSON(JSONObject obj) {
List<Data> data = new ArrayList<>();
Activity act = new Activity(obj.getString(DATE_FIELD), obj.getString(DESC_FIELD), obj.getInt(DISTANCE_FIELD), obj.getInt(FREQ_MIN_FIELD), obj.getInt(FREQ_MAX_FIELD), data);
JSONArray dataArray = obj.getJSONArray(DATA_FIELD);
if (dataArray != null) {
for (int i = 0; i < dataArray.length(); i++) {
JSONObject o = dataArray.getJSONObject(i);
data.add(Data.fromJSON(o));
}
}
return act;
}

public JSONObject toJSON() {
JSONObject obj = new JSONObject();
obj.put(DATE_FIELD, this.date);
obj.put(DESC_FIELD, this.description);
obj.put(DISTANCE_FIELD, this.distance);
obj.put(FREQ_MIN_FIELD, this.freqMin);
obj.put(FREQ_MAX_FIELD, this.freqMax);
JSONArray dataArray = new JSONArray();
obj.put(DATA_FIELD, dataArray);
if (this.data != null) {
for (Data d : this.data) {
dataArray.put(d.toJSON());
}
}
return obj;
}

@Override
public String toString() {
return this.toJSON().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package fr.ubs.sporttrack.model;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Max;
import org.json.JSONObject;

public class Data {
private static final String TIME_FIELD = "time";
private static final String CARDIO_FREQ_FIELD = "cardio_frequency";
private static final String LATITUDE_FIELD = "latitude";
private static final String LONGITUDE_FIELD = "longitude";
private static final String ALTITUDE_FIELD = "altitude";

@Pattern(regexp = "^\\d{2}:\\d{2}:\\d{2}$", message = "Time must be in the format HH:MM:SS")
private String time;

@Min(0) @Max(200)
private int cardioFrequency;

@DecimalMin("-90.0") @DecimalMax("90.0")
private float latitude;

@DecimalMin("-180.0") @DecimalMax("180.0")
private float longitude;

@NotNull
private float altitude;

public Data() {}

public Data(String time, int cf, float lat, float lon, float alt) {
this.time = time;
this.cardioFrequency = cf;
this.latitude = lat;
this.longitude = lon;
this.altitude = alt;
}

public String getTime() {
return this.time;
}

public int getCardioFrequency() {
return this.cardioFrequency;
}

public float getLatitude() {
return this.latitude;
}

public float getLongitude() {
return this.longitude;
}

public float getAltitude() {
return this.altitude;
}

public static Data fromJSON(JSONObject obj) {
return new Data(obj.getString(TIME_FIELD), obj.getInt(CARDIO_FREQ_FIELD), obj.getFloat(LATITUDE_FIELD), obj.getFloat(LONGITUDE_FIELD), obj.getFloat(ALTITUDE_FIELD));
}

public JSONObject toJSON() {
JSONObject obj = new JSONObject();
obj.put(TIME_FIELD, this.time);
obj.put(CARDIO_FREQ_FIELD, this.cardioFrequency);
obj.put(LATITUDE_FIELD, this.latitude);
obj.put(LONGITUDE_FIELD, this.longitude);
obj.put(ALTITUDE_FIELD, this.altitude);
return obj;
}

@Override
public String toString() {
return this.toJSON().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fr.ubs.sporttrack.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;

public class JSONFileReader {
private List<Activity> activities;

/**
* Parses the JSON content of the file and stores the objects
* in a list of objects of type <code>Activity</code>.
* @param jsonData The JSON content of the file.
*/
private void parseActivities(String jsonData) {
JSONArray activitiesArray = new JSONArray(jsonData);
for (int i = 0; i < activitiesArray.length(); i++) {
JSONObject activityObject = activitiesArray.getJSONObject(i);
Activity activity = Activity.fromJSON(activityObject);
this.activities.add(activity);
}
}

/**
* Creates a JSONFileReader object that opens and reads the file
* specified as parameter, and stores the content in a list of
* objects of type <code>Activity</code>.
* @param f The file that must be read.
* @exception IOException if the file does not exist or cannot be
* read.
*/
public JSONFileReader(File f) throws IOException {
this.activities = new ArrayList<>();
StringBuilder jsonContent = new StringBuilder();
try (Scanner scanner = new Scanner(f)) {
while (scanner.hasNextLine()) {
jsonContent.append(scanner.nextLine());
}
parseActivities(jsonContent.toString());
} catch (FileNotFoundException e) {
throw new IOException("File not found: " + f.getAbsolutePath(), e);
}
}

/**
* Returns a list of objects of type <code>JSONObject</code> that have
* been read from the file.
* @return a list of objects of type <code>JSONObject</code>.
*/
public List<Activity> getActivities() {
return this.activities;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fr.ubs.sporttrack.model;

import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.util.List;
import org.json.JSONArray;

public class JSONFileWriter {
private BufferedWriter bufferedWriter;

/**
* Creates a JSONFileWriter object that opens the file specified
* as parameter in order to write objects of type
* <code>Activity</code> into it.
* @param f The file that must be opened.
* @exception IOException if the file cannot be open in write mode.
*/
public JSONFileWriter(File f) throws IOException {
if (!f.exists()) {
f.createNewFile();
}
this.bufferedWriter = new BufferedWriter(new FileWriter(f, false));
}


/**
* Writes a list of objects of type <code>Activity</code> into
* the file.
* @param activities The activities that must be written into the file.
* @exception IOException if an error occurs while writting data.
*/
public void writeData(List<Activity> activities) throws IOException {
JSONArray jsonArray = new JSONArray();
for (Activity activity : activities) {
jsonArray.put(activity.toJSON());
}
bufferedWriter.write(jsonArray.toString());
}


/**
* Closes the file.
* @exception IOException if an error occurs while closing the file.
*/
public void close() throws IOException {
if (bufferedWriter != null) {
bufferedWriter.close();
}
}
}
Loading

0 comments on commit 0c92977

Please sign in to comment.