I have a class called Vehicle. It has four sub classes: Car, Motorcycle, Van and Pickup. Here are the declarations of each:
//vehicle
String licenseNo = null;
int engineCap = 0;
int cc = 0;
String brand = null;
String model = null;
int passengers = 0;
double price = 0;
//car
String material = null;
String paintType = null;
String colorC = null;
//motorcycle
String mainColor = null;
String seatColor = null;
String seatMaterial = null;
//van
int luggageCap = 0;
String colorV = null;
//pickup
int tonsCap = 0;
int yearsUsed = 0;
String tyreQuality = null;
Note: It's not the way i declared them in the actual classes. That how i initialized them in the method im trying to write.
Here is the rest of the method:
FileReader fileReader = new FileReader(filename);
BufferedReader in = new BufferedReader(fileReader);
int count = 0;
Scanner scan = new Scanner(in);
while (scan.hasNext())
{
vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());
}
I have an array :
static Vehicle vehicledata[] = new Vehicle[50];
to hold the values from the file. The file looks something like this: Text file im reading from
I could read up the the values in a normal Vehicle class. But i need to separate the sub-classes, so that i can sort them by their types. (like: List of cars, list of vans)
I am at the:
vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());
part.. and I don't how i can differentiate the cars and other stuff at this point. (From the file after the last attribute in vehicle, there will be the type of vehicle, followed by the attribute of that subclass)
Any clues as to how i can do that? An if condition? But how can i implement it inside the
vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());`