Question

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());` 
Was it helpful?

Solution

You can put everything in a csv file, something like:

L1412,10,100,Nissan,Sedan

and create a static factory method to create the instance you want:

 public static Vehical createVehical(String line){
        String[] parts = line.split(",");
        String type = parts[parts.length -1]; //get the last column
        switch (type) {
                    case "Van":
                        return new Car(a, b, c,...);
                    case "Sedan":
                        return new Van(a, b, c...);
                    default:
                        return null;
                }
    }

For furture checking subclass, use

if(vehical instanceof Sedan){

}else if(vehical instanceof Van){

}else...

Or you can look into Java Generic Collections, it may help you to solve your problem, hope it helps

OTHER TIPS

You have several class that Inherits from Vehicle.
In your file, you must delineate type of your object then all of attribute like:

van
xxxx //licenseNo
xxxx //engineCap
xxxx //cc
xxxx //brand
xxxx //model
xxxx //passengers
xxxx //price
xxxx //luggageCap
xxxx //colorV 

Now you must declare FileReader, BufferedReader and Scanner...
Ok!
Now you have several way to read file and store in array that the easiest is:

Van[] arrVan = new Van[50];
Car[] arrCar = new Car[50];
//etc
while (scan.hasNext()) 
    {
        String str = scan.next();
        if(str == "van")
        {
           Van van = new Van();
           van.setlicenseNo(scan.next());
           van.setEngineCap(scan.next());
           //etc
           // add van to arrVan
        }
        else if(str == "car")
        {
           Car car = new Car();
           //etc
           //add car to arrCar
        }   
        //etc
    }

Thank you for your effort :) Managed to solve the issue i was having, by simply changing the format of the file

while (scan.hasNext()) 
    {
        if(scan.next().toLowerCase() == "car")
        {
            carsdata[count++] = new Car(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next());
        }
        else if(scan.next().toLowerCase() == "motorcycle")
        {
            motorcyclesdata[count++] = new Motorcycle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(),  scan.next(), scan.next(), scan.next());
        }
        else if(scan.next().toLowerCase() == "van")
        {
            vansdata[count++] = new Van(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.nextInt(), scan.next());
        }
        else if(scan.next().toLowerCase() == "pickup")
        {
            pickupsdata[count++] = new Pickup(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.nextInt(), scan.nextInt(), scan.next());
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top