Reputation: 623
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?
Upvotes: 0
Views: 1126
Reputation: 166
For me it could be done this way. First of all you create very generic vehicle class which basically has amount of wheels and name and maybe something else. Then you extend it many times to create motored vehicles, vehicles without motors then cars, trucks and so on.
In addition you could have interfaces towable and many other interfaces that describe some special abilities. This way you could check it for compatibility by checking if a vehicle implements required interface.
Hope it helps. Good luck.
Upvotes: 1