0

I have been trying to figure it out but I find problematic switching on Controller.vehicle as it is a floated value. I tried converting it to String but it dosent work and converting it to an integer I am afraid it could loose some precision.

    if(Controller.vehicle instanceof Boat) {
        file = new File(System.getProperty("user.dir")+"/src/img/boat.png");
    }
    else if(Controller.vehicle instanceof Ship) {
        file = new File(System.getProperty("user.dir")+"/src/img/ship.png");
    }
    else if(Controller.vehicle instanceof Truck) {
        file = new File(System.getProperty("user.dir")+"/src/img/truck.png");
    }
    else if(Controller.vehicle instanceof Motorcycle) {
        file = new File(System.getProperty("user.dir")+"/src/img/motorcycle.png");
    }
    else if(Controller.vehicle instanceof Bus) {
        file = new File(System.getProperty("user.dir")+"/src/img/bus.png");
    }
    else if(Controller.vehicle instanceof Car) {
        file = new File(System.getProperty("user.dir")+"/src/img/car.png");
    }
    else if(Controller.vehicle instanceof Bicycle) {
        file = new File(System.getProperty("user.dir")+"/src/img/bicycle.png");
    }
    else if(Controller.vehicle instanceof Helicopter) {
        file = new File(System.getProperty("user.dir")+"/src/img/helicopter.png");
    }
    else if(Controller.vehicle instanceof Airplane) {
        file = new File(System.getProperty("user.dir")+"/src/img/airplane.png");
    }
    else if(Controller.vehicle instanceof Tram) {
        file = new File(System.getProperty("user.dir")+"/src/img/tram.png");
    }
    else if(Controller.vehicle instanceof Train) {
        file = new File(System.getProperty("user.dir")+"/src/img/train.png");
    }
Vebbie
  • 1,669
  • 2
  • 12
  • 18
Lore
  • 17
  • 5

4 Answers4

4

using multiple instanceof operations usually means that you have a bad design.

Whatever the vehicle class is, it should have an abstract getImageFile() method, and each its subclasses (Ship, Truck, etc.) should override that method to get the correct image. So your method above would contain only one line:

  file = Controller.vehicle.getImageFile();

This would accommodate the possibility that some of these image files might be .jpg, some might be .png, etc.

FredK
  • 4,094
  • 1
  • 9
  • 11
0

In this particular case you can try to do the following:

file = new File(System.getProperty("user.dir") + "/src/img/" + 
Controller.vehicle.getClass().getSimpleName().toLowerCase() + ".png");
Lev Leontev
  • 2,538
  • 2
  • 19
  • 31
0

Don't switch on class, instead let the class expose the file name.

Some code:

public interface ThingWithImage
{
  public string getImageFileName();
}


public class Boat implements ThingWithImage
{
  @Override
  public string getImageFileName()
  {
     return "boat.png";
  }
}

Wit a little work, you can also make the image file name for each class configurable. For example (with Spring):

public class Boat implements ThingWithImage
{
  @Value("${image.file.name.boat}")
  private String boatImageFileName;

  @Override
  public string getImageFileName()
  {
     return boatImageFileName;
  }
}
DwB
  • 37,124
  • 11
  • 56
  • 82
0

I think for best practice, you should read this post. And as Leo and Fred wrote, vehicle class(probably abstract or interface) should have a function called getName() and each implantation should override this method and return its own name.

abstract class Vehicle{

public abstract String getName();

}

class Airplane extends Vehicle{

public String getName(){ return "airplane";}

}

and you should use it like this:

file = new File(System.getProperty("user.dir")+"/src/img/"+Controller.vehicle.getName()+".png");
Tomarlin
  • 63
  • 7