CSV generation changes

This commit is contained in:
Louis Calas 2019-10-03 22:35:02 +02:00
parent 875b39ac61
commit f367068e74
4 changed files with 64 additions and 212 deletions

View File

@ -1,74 +0,0 @@
package csv;
public class CSV {
private String studentNumber; // we put here the idendificator number for each students
private double grade; // we put here the grade of each copies.
public CSV() {
this.studentNumber = null;
this.grade = .0;
}
public CSV(String s, double d) {
if (checkStudentNumber(s)) {
this.studentNumber = s;
this.grade = d;
} else {
System.err.println(
"The student number is not correct, please enter a valid number (from 1 to 16 characters)");
}
}
public CSV(String s, String d) {
if (checkStudentNumber(s)) {
this.studentNumber = s;
this.grade = Double.parseDouble(d);
} else {
System.err.println(
"The student number is not correct, please enter a valid number (from 1 to 16 characters)");
}
}
public static boolean checkStudentNumber(String s) {
int i = 0;
boolean retour = false;
if (s.length() >= 1 && s.length() <= 16) {
while (i < s.length())
{
if (s.charAt(i) == '0' || s.charAt(i) == '1' || s.charAt(i) == '2' || s.charAt(i) == '3'
|| s.charAt(i) == '4' || s.charAt(i) == '5' || s.charAt(i) == '6' || s.charAt(i) == '7'
|| s.charAt(i) == '8' || s.charAt(i) == '9')
{
retour = true;
} else {
retour = false;
break;
}
i++;
}
}
return retour;
}
@Override
public String toString() {
return "The student " + getStudentNumber() + " gets a " + getGrade() + " grade !";
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String s) {
this.studentNumber = s;
}
public double getGrade() {
return grade;
}
public void setGrade(double d) {
this.grade = d;
}
}

View File

@ -1,78 +0,0 @@
package csv;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Generate {
private static String path = "grades";
BufferedReader csvReader;
private static ArrayList<CSV> csvList = new ArrayList<CSV>();
public static void start() {
createFile();
}
public static void start(String p) {
createFile(p);
}
public static void addList(String n, String g) {
CSV o = new CSV(n, g);
csvList.add(o);
}
public static void createFile(String p) {
SetPath(p);
createFile();
}
public static void createFile() {
try (PrintWriter writer = new PrintWriter(new File(path + ".csv"))) {
StringBuilder sb = new StringBuilder();
sb.append("Student number");
sb.append(',');
sb.append("Grade");
sb.append('\n');
writer.write(sb.toString());
for (int i = 0; i < csvList.size(); i++) {
sb.delete(0, sb.length());
sb.append(csvList.get(i).getStudentNumber());
sb.append("; ");
sb.append(csvList.get(i).getGrade());
writer.write(sb + System.getProperty("line.separator"));
}
System.out.println("Create File Done!");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
}
}
public String getPath() {
return Generate.path;
}
public static void SetPath(String s) {
path = s;
}
public ArrayList<CSV> getCsvList() {
return csvList;
}
public void setCsvList(ArrayList<CSV> csvList) {
Generate.csvList = csvList;
}
}

View File

@ -11,33 +11,37 @@ public class GenerateCSV {
int numLength;
String path = "../export";
public GenerateCSV(HashMap<String,String> map, String s, String p) {
public GenerateCSV(HashMap<String,String> map, String length, String pth) {
this.etudiants = map;
this.numLength = Integer.parseInt(s);
this.path = path+"/"+p;
this.numLength = Integer.parseInt(length);
this.path = path+"/"+pth;
}
// public boolean isValid(String s) {
// int i = 0;
// if (s.length() == this.numLength) {
// while (i < s.length())
// {
// if (s.charAt(i) != '0' && s.charAt(i) == '1' || s.charAt(i) == '2' || s.charAt(i) == '3'
// || s.charAt(i) == '4' || s.charAt(i) == '5' || s.charAt(i) == '6' || s.charAt(i) == '7'
// || s.charAt(i) == '8' || s.charAt(i) == '9')
// {
// return true;
//
// } else {
// return false;
// break;
// }
// i++;
// }
// }
//
// return true;
// }
// TO DO : exploiter dans generation
public boolean isValid(String s) {
int i = 0;
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");
return false;
}
else {
i++;
}
}
return true;
}
else {
System.err.println("Student id's length is not correct");
return false;
}
}

View File

@ -7,41 +7,41 @@ import org.junit.jupiter.api.Test;
public class TestCSV {
@Test
public void createNullCSV() {
//We check if a CSV object cannot be created !
CSV c = new CSV();
assertEquals(null, c.getStudentNumber());
assertEquals(.0, c.getGrade());
}
@Test
public void checkFalseStudentnumber() {
//We check if the student number is full composed of numeric digits !
String s = "056498a198";
assertEquals(false, CSV.checkStudentNumber(s));
}
@Test
public void checkStudentNumberTooShort() {
//We check if the student number is enought long !
String s = "";
assertEquals(false, CSV.checkStudentNumber(s));
}
@Test
public void checkStudentNumberTooLong() {
//We check if the student number is enought short !
String s = "012345678910111213141516";
assertEquals(false, CSV.checkStudentNumber(s));
}
@Test
public void checkCorrectStudentNumber() {
//We check if the student number is correct !
String s = "123456789";
assertEquals(true, CSV.checkStudentNumber(s));
}
// @Test
// public void createNullCSV() {
// //We check if a CSV object cannot be created !
// CSV c = new CSV();
// assertEquals(null, c.getStudentNumber());
// assertEquals(.0, c.getGrade());
// }
//
// @Test
// public void checkFalseStudentnumber() {
// //We check if the student number is full composed of numeric digits !
// String s = "056498a198";
// assertEquals(false, CSV.checkStudentNumber(s));
// }
//
// @Test
// public void checkStudentNumberTooShort() {
// //We check if the student number is enought long !
// String s = "";
// assertEquals(false, CSV.checkStudentNumber(s));
// }
//
//
// @Test
// public void checkStudentNumberTooLong() {
// //We check if the student number is enought short !
// String s = "012345678910111213141516";
// assertEquals(false, CSV.checkStudentNumber(s));
// }
//
// @Test
// public void checkCorrectStudentNumber() {
// //We check if the student number is correct !
// String s = "123456789";
// assertEquals(true, CSV.checkStudentNumber(s));
// }
}