Bug fixes
This commit is contained in:
@@ -5,29 +5,35 @@ import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class GenerateCSV {
|
||||
|
||||
HashMap<String,String> etudiants;
|
||||
int numLength;
|
||||
String path = "../export";
|
||||
Logger logger;
|
||||
|
||||
public GenerateCSV(HashMap<String,String> map, String length, String pth) {
|
||||
public GenerateCSV(HashMap<String,String> map, String length, String pth, Logger lg) {
|
||||
this.etudiants = map;
|
||||
this.numLength = Integer.parseInt(length);
|
||||
this.path = path+"/"+pth;
|
||||
this.logger = lg;
|
||||
}
|
||||
|
||||
|
||||
// TO DO : exploiter dans generation
|
||||
// Teste validité du numero etudiant (selon param de la config passé : numLength)
|
||||
|
||||
public boolean isValid(String s) {
|
||||
int i = 0;
|
||||
logger.debug("Checking string validity");
|
||||
if (s.length() == this.numLength) {
|
||||
while (i < s.length()) {
|
||||
|
||||
int nb = Character.getNumericValue(s.charAt(i));
|
||||
|
||||
if (nb <= 0 || nb >= 9) {
|
||||
System.err.println("Student id's characters are not recognized");
|
||||
logger.fatal("Student id's characters are not recognized");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
@@ -35,10 +41,11 @@ public class GenerateCSV {
|
||||
}
|
||||
|
||||
}
|
||||
logger.debug("String validity ok");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
System.err.println("Student id's length is not correct");
|
||||
else {
|
||||
logger.fatal("Student id's length is not correct");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +57,7 @@ public class GenerateCSV {
|
||||
public void createFile() {
|
||||
try (PrintWriter writer = new PrintWriter(new File(this.path))) {
|
||||
|
||||
logger.info("Creating csv file");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Student number");
|
||||
sb.append(';');
|
||||
@@ -58,15 +66,33 @@ public class GenerateCSV {
|
||||
|
||||
writer.write(sb.toString());
|
||||
|
||||
for (String etud : this.etudiants.keySet()) {
|
||||
|
||||
writer.write(etud+";"+etudiants.get(etud)+System.getProperty("line.separator"));
|
||||
if (!etudiants.isEmpty()) {
|
||||
|
||||
for (String etud : this.etudiants.keySet()) {
|
||||
|
||||
// Si etudiant HashMap est null, pas ecrit
|
||||
if (!(etud == null)) {
|
||||
|
||||
if (this.isValid(etud)) {
|
||||
writer.write(etud+";"+etudiants.get(etud)+System.getProperty("line.separator"));
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
logger.debug("Null id not added to csv");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
logger.info("File creation succeed");
|
||||
}
|
||||
System.out.println("Create File Done!");
|
||||
else {
|
||||
logger.fatal("Students list for csv generation is empty");
|
||||
}
|
||||
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println(e.getMessage());
|
||||
logger.fatal(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user