Creation of the ReadingFile method
This commit is contained in:
parent
d40eea9e73
commit
e9ec0251af
2 changed files with 65 additions and 3 deletions
|
@ -1,8 +1,49 @@
|
|||
package noteBook;
|
||||
|
||||
import input.Gender;
|
||||
import input.Input;
|
||||
import input.Person;
|
||||
import input.Society;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class NoteBook {
|
||||
Input inputs[];
|
||||
Input selected[];
|
||||
}
|
||||
public Input[] inputs;
|
||||
public Input[] selected;
|
||||
|
||||
public void FileReading(String fileName) throws IOException {
|
||||
File file = new File(fileName);
|
||||
Scanner fileScanner = new Scanner(file);
|
||||
int lines = 0;
|
||||
while(fileScanner.hasNextLine()){
|
||||
lines++;
|
||||
}
|
||||
inputs = new Input[lines];
|
||||
fileScanner.reset();
|
||||
for (int i = 0; i<lines; i++) {
|
||||
String line = fileScanner.nextLine();
|
||||
int idIndex = line.lastIndexOf(";");
|
||||
int typeIndex = line.lastIndexOf(";", idIndex);
|
||||
String type = line.substring(idIndex, typeIndex);
|
||||
if (type.equals("PERSON")) {
|
||||
int firstNameIndex = line.lastIndexOf(";", typeIndex);
|
||||
int lastNameIndex = line.lastIndexOf(";", firstNameIndex);
|
||||
int genderIndex = line.lastIndexOf(";", lastNameIndex);
|
||||
int spouseIdIndex = line.lastIndexOf(";", genderIndex);
|
||||
int societyIndex = line.lastIndexOf(";", spouseIdIndex);
|
||||
Gender gender;
|
||||
if (line.substring(lastNameIndex, genderIndex).equals("MEN"))
|
||||
gender = Gender.MEN;
|
||||
else
|
||||
gender = Gender.WOMEN;
|
||||
Person n = new Person(line.substring(firstNameIndex, lastNameIndex), line.substring(0, firstNameIndex).split(","), gender, null, null, line.substring(societyIndex));
|
||||
inputs[i] = n;
|
||||
} else {
|
||||
Society n = new Society(line.substring(typeIndex));
|
||||
inputs[i] = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
21
src/test/TestReading.java
Normal file
21
src/test/TestReading.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package test;
|
||||
|
||||
import input.Gender;
|
||||
import input.Person;
|
||||
import input.Society;
|
||||
import noteBook.NoteBook;
|
||||
|
||||
public class TestReading {
|
||||
public static void main(String[] args) {
|
||||
String flifloo_firstNames[] = {"Florian", "Alain"};
|
||||
Person flifloo = new Person("Charlaix", flifloo_firstNames, Gender.MEN, null, null, "Student");
|
||||
String hugo_firstNames[] = {"Hugo"};
|
||||
Person hugo = new Person("Torres", hugo_firstNames, Gender.MEN, flifloo, null, "Student");
|
||||
flifloo.spouse = hugo;
|
||||
|
||||
Society society1 = new Society("LeBonCoin");
|
||||
|
||||
NoteBook noteBook = new NoteBook();
|
||||
noteBook.inputs = new input.Input[]{flifloo, hugo, society1};
|
||||
}
|
||||
}
|
Reference in a new issue