I have created an array of events with a combination of the following event types. I must sort through this array find the ones of type Festival and copy them to a new array using the copy constructor of the respective class. When I copy it, it does not allow me to since they are of event type. Assuming the problem is that the object being passed in the subclass copy constructor is thought to be an Event object which is not specific enough. Hopefully not too complicated.
events2[0] = new Culturalfiesta(2018, 8, 5, "Crazy Fiesta", 49, 5);
events2[1] = new Culturalfiesta(2018, 3, 6, "Very Crazy Fiesta", 33, 1);
events2[2] = new Event(2019,3,2);
events2[3] = new Event(2020,12,8);
events2[4] = new Fair(2019,6,3,5, typeOfFair.CAREER);
events2[5] = new Festival(2019, 5, 3, "Cool Festival", 52);
events2[6] = new Festival(2021, 3, 6, "Even Cooler Festival", 95);
events2[7] = new Musicfiesta(2018, 4, 20, "Crazy Musicfiesta", 33, 18);
events2[8] = new Musicfiesta(2019, 7, 22, "Crazy Musicfiesta Part 2", 33, 20);
events2[9] = new SportCompetition(2020,11, 3, 4, Seasons.FALL);
events2[10] = new Musicfiesta(2020, 7, 22, "Crazy Musicfiesta Part 3", 33, 19);
events2[11] = new SportCompetition(2024,12, 3, 4, Seasons.WINTER);
public static void copyFestival(Event[] arrayOfEvents) {
int j = 0;
System.out.println("List of festivals from array");
int numberOfEvents = arrayOfEvents.length;
for(int i = 0; i < numberOfEvents; i++) {
if( arrayOfEvents[i] instanceof Festival) {
if(arrayOfEvents[i] instanceof Culturalfiesta) {
festivals[j] = new Festival((Culturalfiesta)arrayOfEvents[i]);
}else if(arrayOfEvents[i] instanceof Musicfiesta) {
festivals[j] = new Musicfiesta((Musicfiesta) arrayOfEvents[i]);
}else {
festivals[j] = new Festival((Festival)arrayOfEvents[i]);
}
j++;
}
}
for (int i = 0; i < festivals.length; i++) {
System.out.println(festivals[i].toString());
}
}
Aucun commentaire:
Enregistrer un commentaire