fix bugs
This commit is contained in:
@@ -4,58 +4,53 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class GenerateCSV {
|
||||
|
||||
HashMap<String,String> etudiants;
|
||||
|
||||
Map<String, String> etudiants;
|
||||
int numLength;
|
||||
String path = "../export";
|
||||
Logger logger;
|
||||
|
||||
public GenerateCSV(HashMap<String,String> map, String length, String pth, Logger lg) {
|
||||
|
||||
public GenerateCSV(Map<String, String> map, String length, String pth, Logger lg) {
|
||||
this.etudiants = map;
|
||||
this.numLength = Integer.parseInt(length);
|
||||
this.path = path+"/"+pth;
|
||||
this.path = path + "/" + pth;
|
||||
this.logger = lg;
|
||||
}
|
||||
|
||||
|
||||
// Teste validité du numero etudiant (selon param de la config passé : numLength)
|
||||
|
||||
|
||||
// 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) {
|
||||
logger.fatal("Student id's characters are not recognized");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
logger.debug("String validity ok");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
logger.fatal("Student id's length is not correct");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void createFile() {
|
||||
try (PrintWriter writer = new PrintWriter(new File(this.path))) {
|
||||
try (PrintWriter writer = new PrintWriter(new File("E:\\S3T\\Projet\\pt-s3t-g4\\pdf"))) {
|
||||
|
||||
logger.info("Creating csv file");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -65,37 +60,33 @@ public class GenerateCSV {
|
||||
sb.append(System.getProperty("line.separator"));
|
||||
|
||||
writer.write(sb.toString());
|
||||
|
||||
|
||||
if (!etudiants.isEmpty()) {
|
||||
|
||||
|
||||
for (String etud : this.etudiants.keySet()) {
|
||||
|
||||
|
||||
// Si etudiant HashMap est null, pas ecrit
|
||||
if (!(etud == null)) {
|
||||
|
||||
if (etud != null) {
|
||||
|
||||
if (this.isValid(etud)) {
|
||||
writer.write(etud+";"+etudiants.get(etud)+System.getProperty("line.separator"));
|
||||
writer.write(etud + ";" + etudiants.get(etud) + System.getProperty("line.separator"));
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
logger.debug("Null id not added to csv");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
logger.info("File creation succeed");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
logger.fatal("Students list for csv generation is empty");
|
||||
}
|
||||
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.fatal(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user