First off, here is the code I will be referencing:
package com.airamerica.dataConversion;
import com.airamerica.Airports;
import com.airamerica.Person;
import com.airamerica.Products.Products;
import com.airamerica.Products.Tickets;
public class FindObject {
public static Airports findAirport(String airportCode, Airports [] airportsArray){
Airports airport = null;
for(int j = 0; j < airportsArray.length; j++) {
if (airportCode.equals(airportsArray[j].getCode())){
airport = airportsArray[j];
break;
}
}
return airport;
}
public static Tickets findTicket(String productCode, Products[] productArray){
Tickets product= null;
String matchCode = null;
for(int j = 0; j < productArray.length; j++) {
matchCode = productArray[j].getCode();
if (matchCode.equals(productCode)){
product = (Tickets) productArray[j];
break;
}
}
return product;
}
public static Person findPerson(String personCode, Person[] personArray){
Person person= null;
String matchCode = null;
for(int j = 0; j < personArray.length; j++) {
matchCode = personArray[j].getCode();
if (matchCode.equals(personCode)){
person = personArray[j];
break;
}
}
return person;
}
}
With this code a partner and I are finding different objects in our object arrays that we can then you to get information from that object for reporting and other data manipulations. In this FindObject class we have 3 methods that each search by a different code unique to the type of object they are searching through. As an example if we take our Airports object and use it within the findAirport method we can pass in the code we want to find as well as the array of Airports objects created in a different class file. The method will sift through the Array of Airport Objects (airportsArray) and use the .getCode method in the Airport object to find the specific code associated with that object, if it equals the code passed we return that object, easy enough. The question we have is if we can use some sort of generic method to simplify these 3 almost identical (logically identical) methods into one method. We talked about polymorphism in class as well as generic methods and talked to our instructor and we are now left with no ideas. Is this code as efficient as it gets? Can it be better? If so what is something we could do. To be clear, we are not looking for a solution, we don't want code that we could just copy and paste, what we do want is resources we can read that will point us in the right direction, if you do provide code please keep it generic because this is something we both really need and want to learn and we all know giving someone the answer isn't the best way to do that.
Note:
The .getCode method is different for each method, they used to be named .getAirportCode, .getPersonCode and .getProductCode but we changed them all to .getCode because one of our ideas was if the method call was the same we could possible make one method for all 3 but we again weren't sure if it'd work or not and after researching and talking to TA's and our professor I don't think it will influence the solution any.
Thanks!