1
0
Fork 0

Merge branch 'reading' into 'master'

Creation of the ReadingFile method

See merge request p1905458/git-cvda!3
This commit is contained in:
Ethanell 2020-04-04 15:54:11 +02:00
commit 149be98a0a
2 changed files with 60 additions and 0 deletions

View file

@ -1,13 +1,52 @@
package noteBook; package noteBook;
import input.Gender;
import input.Input; import input.Input;
import input.Person; import input.Person;
import input.Society; import input.Society;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class NoteBook { public class NoteBook {
public Input[] inputs; public Input[] inputs;
public Input[] selected; 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;
}
}
}
}
public void addInput(Input in) { public void addInput(Input in) {
Input[] tmp = new Input[inputs.length+1]; Input[] tmp = new Input[inputs.length+1];
System.arraycopy(inputs, 0, tmp, 0, inputs.length); System.arraycopy(inputs, 0, tmp, 0, inputs.length);

21
src/test/TestReading.java Normal file
View 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};
}
}