Before explaining my problem, I should do a bit of theory...
For vehicle I mean everything that can transport goods by road: cars, vans, trucks, trailer trucks (or road trains, I don't know the right term), semi-trailer trucks (also called articulated trucks or tractor-trailers).
Concerning cars, vans and trucks, there are no problems: they have a plate, a weight and a volume transportable, and other data.
But the other two vehicles are more complex. They may have an additional license plate, and other additional data. In particular:
The semi-trailer trucks (also called tractor-trailer) is a complex vehicular, formed by a road tractor and a semitrailer.
A trailer trucks (also called...) is a complex vehicular, formed by a driving part (a car, a van, a truck, ...) and a driven part (a trailer, a cart appendix ...). In europe, max TWO assembled parts.
So we also have to handle semi-trailers, trailers and carts.
In my software, they can be assembled each other in different ways. For example, can I take a trailer and pull it from trailer-truck and obtain only the single truck and trailer. Still, I could take that same truck and, if compatible, attack another and different trailer. And so on...
It seems to me quite obvious that we can not manage everything with a class Vehicle...
I'm wondering what is the best way to handle it all. Initially I had an enum in the Vehicle class that showed me the type of vehicle. But when I have to handle even the complex vehicular believe that it is much more complicated ... I also wonder how this will affect the rest of the software.
This is how, at first, I was managing the Vehicle class:
public class Vehicle {
// kind of
enum TipoVeicolo {
AUTO, // car
FURGONE, // van
AUTOCARRO, // truck
/*
// for semi-trailer truck:
TRATTORE_STRADALE // road tractor
SEMIRIMORCHIO // semi-trailer
AUTOARTICOLATO // semi-trailer truck
// for trailer truck:
RIMORCHIO // trailer
AUTOTRENO // trailer truck
...
*/
};
enum Stato {
DISPONIBILE, // Available
NON_DISPONIBILE,// not available
IN_VIAGGIO // on road...
// ... ?
}
private String targa; // plate
private String mark;
private Stato stato;
private TipoVeicolo tipoVeicolo;
private float portata;
private float volume;
private short europallet;
//Date immatricolationDate;
//String assurance;
public Vehicle(String targa, TipoVeicolo tipoVeicolo, String mark, Stato stato, float ptt) {
this.targa=targa;
this.tipoVeicolo=tipoVeicolo;
this.mark=mark;
this.stato=stato;
this.ptt=ptt;
}
//////////////////////////////////////////////
// GET and SET methods
// ...
}
How should I handle it? With inheritance?