private void initialiseVehicle(String vehicleName) {
if (vehicleName == null) {
} else {
switch (vehicleName) {
case "Boat":
vehicle = new Boat("Apollo ");
break;
case "Ship":
vehicle = new Ship("Cruizz");
break;
case "Truck":
vehicle = new Truck("Ford F-650");
break;
case "Motorcycle":
vehicle = new Motorcycle("Suzuki");
break;
case "Bus":
vehicle = new Bus("Aero");
break;
case "Car":
vehicle = new Car("BMW");
break;
case "Bicycle":
vehicle = new Bicycle("A-bike");
break;
case "Helicopter":
vehicle = new Helicopter("Eurocopter");
break;
case "Airplane":
vehicle = new Airplane("BA");
break;
case "Tram":
vehicle = new Tram("EdinburghTram");
break;
case "Train":
vehicle = new Train("Virgin",4);
break;
}
}
}
I have edited this from an if-else statement to a switch, but I have tried to reduce the CBO metric by doing this and was wondering if there was any way to reduce the coupling here and I can provide more information if needed.

switch expression (not that different):
A
Mapfrom name toSupplier:A
Mapfrom name to template (just an idea):Register the vehicle-providing classes as service, using for example the
ServiceLoaderframeworkjust some ideas
(there are eventually better ways to populate a map) e.g. each object register itself to the map