From f367068e744019dd38bd2030fce0879ed22d96f8 Mon Sep 17 00:00:00 2001 From: Louis Calas Date: Thu, 3 Oct 2019 22:35:02 +0200 Subject: [PATCH] CSV generation changes --- NGCC/src/csv/CSV.java | 74 --------------------------------- NGCC/src/csv/Generate.java | 78 ----------------------------------- NGCC/src/csv/GenerateCSV.java | 52 ++++++++++++----------- NGCC/tests/csv/TestCSV.java | 72 ++++++++++++++++---------------- 4 files changed, 64 insertions(+), 212 deletions(-) delete mode 100644 NGCC/src/csv/CSV.java delete mode 100644 NGCC/src/csv/Generate.java diff --git a/NGCC/src/csv/CSV.java b/NGCC/src/csv/CSV.java deleted file mode 100644 index 369a326..0000000 --- a/NGCC/src/csv/CSV.java +++ /dev/null @@ -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; - } -} diff --git a/NGCC/src/csv/Generate.java b/NGCC/src/csv/Generate.java deleted file mode 100644 index 95339d6..0000000 --- a/NGCC/src/csv/Generate.java +++ /dev/null @@ -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 csvList = new ArrayList(); - - 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 getCsvList() { - return csvList; - } - - public void setCsvList(ArrayList csvList) { - Generate.csvList = csvList; - } - -} diff --git a/NGCC/src/csv/GenerateCSV.java b/NGCC/src/csv/GenerateCSV.java index 54d1fb0..d30a8ea 100644 --- a/NGCC/src/csv/GenerateCSV.java +++ b/NGCC/src/csv/GenerateCSV.java @@ -11,33 +11,37 @@ public class GenerateCSV { int numLength; String path = "../export"; - public GenerateCSV(HashMap map, String s, String p) { + public GenerateCSV(HashMap 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; + } + } diff --git a/NGCC/tests/csv/TestCSV.java b/NGCC/tests/csv/TestCSV.java index 80d024c..35e9370 100644 --- a/NGCC/tests/csv/TestCSV.java +++ b/NGCC/tests/csv/TestCSV.java @@ -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)); +// } }