#csv #exception #refactoring
Вопрос:
Я работаю с csv-файлами и создаю разные типы людей. Теперь я переработал свой код, и когда я запускаю основную программу, программа выдает это исключение, которое говорит мне, что prgramm не может привести человека к dkkrPerson:
Exception in thread "main" java.lang.ClassCastException: class com.csvmaker.model.outputModell.Person cannot be cast to class com.csvmaker.model.outputModell.DkkrPerson (com.csvmaker.model.outputModell.Person and com.csvmaker.model.outputModell.DkkrPerson are in unnamed module of loader 'app')
at com.csvmaker.controller.GenerateDkkrPerson.generatePerson(GenerateDkkrPerson.java:27)
at com.csvmaker.controller.GenerateDkkrPerson.generatePersons(GenerateDkkrPerson.java:57)
at com.csvmaker.Main.main(Main.java:75)
Вот класс, который создает исключение:
public class GenerateDkkrPerson extends GeneratePersonData {
int matchingDiagnoseCity = randomItem.nextInt(cities.size());
int matchingParentsCityData = randomItem.nextInt(cities.size());
int matchingParentsStreetData = randomItem.nextInt(streets.size());
int matchingParentsHouseNumber = generateRandomNumber(1, 200);
public GenerateDkkrPerson(List<PersonFirstName> personFirstNames, List<Street> streets, List<PersonLastName> personLastNames, List<City> cities) {
super(personFirstNames, streets, personLastNames, cities);
}
@Override
public Person generatePerson(int id) {
DkkrPerson dkkrPerson = (DkkrPerson) super.generatePerson(id);
dkkrPerson.setDiagnoseStreetName(streets.get(randomItem.nextInt(streets.size())).getStreetName());
dkkrPerson.setDiagnoseHouseNumber(generateRandomNumber(1, 200));
dkkrPerson.setDiagnoseCityName(cities.get(matchingDiagnoseCity).getCity());
dkkrPerson.setDiagnoseZipCode(cities.get(matchingDiagnoseCity).getZipCode());
dkkrPerson.setParentsStreetName(streets.get(matchingParentsStreetData).getStreetName());
dkkrPerson.setParentsHouseNumber(matchingParentsHouseNumber);
dkkrPerson.setParentsCityName(cities.get(matchingParentsCityData).getCity());
dkkrPerson.setParentsZipCode(cities.get(matchingParentsCityData).getZipCode());
dkkrPerson.setMotherStreetName(streets.get(matchingParentsStreetData).getStreetName());
dkkrPerson.setMotherHouseNumber(matchingParentsHouseNumber);
dkkrPerson.setMotherCityName(cities.get(matchingParentsCityData).getCity());
dkkrPerson.setMotherZipCode(cities.get(matchingParentsCityData).getZipCode());
dkkrPerson.setFatherStreetName(streets.get(matchingParentsStreetData).getStreetName());
dkkrPerson.setFatherHouseNumber(matchingParentsHouseNumber);
dkkrPerson.setFatherCityName(cities.get(matchingParentsCityData).getCity());
dkkrPerson.setFatherZipCode(cities.get(matchingParentsCityData).getZipCode());
return dkkrPerson;
}
@Override
public List<Person> generatePersons() {
List<Person> persons = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.printf("Number of persons, who will be generated: ");
int numberOfRecords = in.nextInt();
for (int i = 0; i < numberOfRecords; i ) {
persons.add(generatePerson(i));
}
return persons;
}
И вот высший класс, из которого происходит порожденный Криперон:
public abstract class GeneratePersonData {
List<PersonFirstName> personFirstNames;
List<Street> streets;
List<PersonLastName> personLastNames;
List<City> cities;
Random randomItem = new Random();
public GeneratePersonData(List<PersonFirstName> personFirstNames, List<Street> streets, List<PersonLastName> personLastNames, List<City> cities) {
this.personFirstNames = personFirstNames;
this.streets = streets;
this.personLastNames = personLastNames;
this.cities = cities;
}
public Person generatePerson(int id) {
int matchingFirstNameGender = randomItem.nextInt(personFirstNames.size());
int matchingBirthData = randomItem.nextInt(cities.size());
int matchingCityZipCode = randomItem.nextInt(cities.size());
int birthYear = generateRandomNumber(1976, 2021);
double birthPlaceChance = Math.random() * 10;
Person person = new Person();
if(birthPlaceChance <= 1.0) {
person.setBirthPlaceCityName(cities.get(matchingBirthData).getCity());
person.setBirthPlaceZipCode(cities.get(matchingBirthData).getZipCode());
}
person.setId(id 1);
person.setFirstName(personFirstNames.get(matchingFirstNameGender).getFirstName());
person.setGender(personFirstNames.get(matchingFirstNameGender).getGender());
person.setLastName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
person.setFirstBirthName(personFirstNames.get(matchingFirstNameGender).getFirstName());
person.setLastBirthName(personLastNames.get(randomItem.nextInt(personLastNames.size())).getLastName());
person.setBirthDate(generateBirthDate() birthYear);
person.setBirthYear(birthYear);
person.setLivingPlaceCityNow(cities.get(matchingCityZipCode).getCity());
person.setLivingPlaceStreetNameNow(streets.get(randomItem.nextInt(streets.size())).getStreetName());
person.setLivingPlaceHouseNumberNow(generateRandomNumber(1, 200));
person.setLivingPlaceZipCodeNow(cities.get(matchingCityZipCode).getZipCode());
return person;
}
public List<Person> generatePersons() {
List<Person> persons = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.printf("Number of persons, who will be generated: ");
int numberOfRecords = in.nextInt();
for (int i = 0; i < numberOfRecords; i ) {
persons.add(generatePerson(i));
}
return persons;
}
Может быть, кто-нибудь подскажет мне, почему возникает такое исключение и как его исправить, заранее спасибо 😀