im creating little program which produces cars, tractors and mopeds. car has 4 wheels and 4 cilinder engine, tractor has same, and moped has 2 wheels and 1 cilinder engine.
this is what i have at the moment.
next thing i need to do that with 2 factories.. home factory produces vehicles with these quantity of wheels and engine cilinders and foreignfactory produces tractors with 3 cilinder engine and cars with 2 cilinder engines.
here is my code so far
package Vehicles;
public class Car extends Vehicle {
private int wheels = 4;
private int engine = 4;
/**
* Method over-ridden from Vehicle
* @return wheels
*/
public int getWheels() {
return wheels;
}
/**
* Method over-ridden from Vehicle, returns
* @return Parts
*/
public int getEngine() {
return engine;
}
/**
* @return returns full specification of the car
*/
public String getSpecification() {
return getClass()+ "\n"+wheels+" wheels\n"
+engine+" cilinder engine\n"+getTax();
}
} // End of class
tractor is same as car class
package Vehicles;
public class Moped extends Vehicle {
private int wheels = 2;
private int engine = 1;
/**
* Method overridden from Vehicle, returns
* @return Parts
*/
public int getWheels() {
return wheels;
}
/**
* Method overridden from Vehicle, returns
* @return Parts
*/
public int getEngine() {
return engine;
}
/**
* @return returns full specification of the car
*/
public String getSpecification() {
return getClass()+ "\n"+wheels+" wheels\n"
+engine+" cilinder engine\n"+getTax();
}
} // End of class
package Vehicles;
public abstract class Vehicle {
public abstract int getWheels();
public abstract int getEngine();
public abstract String getSpecification();
public final int getTax() {
int sum=0;
if (getClass()!=Moped.class) sum+=getEngine()*15;
if (getWheels()>2) sum+=getWheels()*7;
if (getClass()==Tractor.class) sum+=getWheels()*7;
return sum;
}
} // End of class
package Vehicles;
public class VehicleType {
private Vehicle vehic;
public Vehicle getVehicle(String vehicleType) {
if (vehicleType.equals("Car")) vehic = new Car();
else if(vehicleType.equals("Tractor")) vehic = new Tractor();
else if(vehicleType.equals("Moped")) vehic = new Moped();
else vehic = new NullVehicle();
return vehic;
}
} // End of class
i've googled alot for abstract factory pattern but there are examples with different products for factories. that would be copy-paste programming if i used same (creating classes HomeCar, ForeignCar and so on).
what would be the better way to create abstract factory?
package Factories;
public abstract class Factories {
public Factories() {
// TODO Auto-generated constructor stub
}
} // End of class
class ChineseFactory extends Factories {
public ChineseFactory() {
// TODO Auto-generated constructor stub
}
}
class HomeFactory extends Factories {
public HomeFactory() {
// TODO Auto-generated constructor stub
}
}