我的任务是从文件中读取一些数据,将其拆分,然后将其解析为正确的类实例。到目前为止一直很好,但我对班级Education
有问题,因为每个人都有不同的教育程度,所以我不知道如何检查。 Insurance
课程也存在同样的问题,因为对于每个人,我们都有不同时期的信息。所以这是我的基本问题。
我不知道我必须创建多少Education
个实例。
以下是我必须阅读和解析的两个数据示例:
--1--
"Plamen;Stoichev;Izmirliev;M;16.7.1980;206;Bulgaria;Sofiya;Studentski;1016;Opalchenska;21;;;P; William Gladstoine;15.9.1986;15.6.1993;S;William Gladstoine ;15.9.1993;30.6.1998;3.343;B; William Gladstoine;1.10.1999;1.6.2003;3.045" +
"2016;11;1015.20;2016;10;605.93;2016;9;701.61;2016;7;981.86;2016;4;1042.57;2016;3;919.87;2016;2;720.00;2015;12;969.75;2015;6;641.16;2015;3;811.76;2015;2;757.07;2014;12;1321.31;2014;11;863.39;2014;9;1539.51;2014;7;1159.62;2014;5;1295.59;2014;3;910.59;2014;1;1033.80";
--2--
Violeta;Konstantinova;Orlova;F;23.8. 1982;148;Bulgaria;Sofia;Izgrev;1008;King Boris III;123;5;26;P;William Gladstoine ;15.9.1988;15.6.1995;S;William Gladstoine ;15.9.1995;30.6.2000;4.069
2016;11;1587.70;2016;8;1524.04;2016;6;1273.10;2016;4;1129.08; 2016;2;1469.79;2015;12;927.91;2015;10;1116.83;2015;6;1143.05;2015;4;1348.82;
SocialInsuranceRecord
类有三个参数year
,month
,tax amount
。
Education
班级有level of degree
,name of school
,enrollment date
,graduation date
,average grade
;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import DataObjects.address.Address;
import DataObjects.education.Education;
import DataObjects.insurance.SocialInsuranceRecord;
import DataObjects.personaldetails.Citizen;
import DataObjects.personaldetails.Gender;
public class GetDataFromFile {
private List<Citizen> citizents = new ArrayList<>();
private static final String FILE_PATH = "D:\\data.dat";
public List<Citizen> getCitizens() {
List<Citizen> list = new ArrayList<>();
try {
Scanner scanner = new Scanner(new String(Files.readAllBytes(Paths.get(FILE_PATH))));
while(scanner.hasNext()) {
Citizen citizen = parseCitizent(scanner.nextLine());
list.add(citizen);
}
}catch(Exception ex) {
ex.printStackTrace();
}
return list;
}
public Citizen parseCitizent(String nextLine) {
String[] split = nextLine.split("\\s*;");
Citizen citizen = new Citizen(split[0], split[1], split[2], Gender.valueOf(split[3]), LocalDate.parse(split[4]), Integer.parseInt(split[5]));
Address address = new Address(split[6], split[7], split[8], split[9], split[10], split[11], Integer.parseInt(split[11]), Integer.parseInt(split[12]));
citizen.setAddress(address);
List<Education> educations = new ArrayList<>();
// to do...
citizen.set_educations(educations);
List<SocialInsuranceRecord> socilInsuranceRecords = new ArrayList<>();
// to do...
citizen.set_socialInsuranceRecords(socilInsenter code hereuranceRecords);
return citizen;
}
}