I am facing the following situation:
I have to develop a system that has to calculate the price of a car so i need to calculate the prices of all the Pieces that make up the car and then total them.
We have two main entities:
public class Car
{
public int Id{get;}
public List<PieceOfCar> Pieces{get;}
}
public abstract class PieceOfCar
{
public double UnitCost{get;}
public double Transportation{get;}
public string IdOriginCountry{get;}
public abstract double GetTaxPrice();
{
//how do i design this part?
}
}
Depending on the IdOriginCountry, the PieceOfCar Taxes should be calculated in different ways.
For example in USA:
List<TaxUSA> taxesInUSa= TaxesRepository.GetTaxFromUSA(); //different type of objets are used to calculate
double taxUsa = UnitCost* taxesInUSa.TaxTrump + Transportation* taxesInUSa.TaxClinton;
In India:
List<TaxIndia> taxesInIndia = TaxesRepository.GetTaxFromIndia(); //different type of objets are used to calculate
double taxCost = (UnitCost + Transportation)^ taxesInIndia.BadulakeTax ;
As you see, each piece of car, depending on the IdOriginCountry, have different calculations formulas and different requirements of data. USA parts needs some objects, and Indian parts needs others.
How you will solve that problem?
I tried the factory method pattern, but i was forced to inject the TaxesRepository inside the PieceOfCar entities, so i don't think its a good idea. Pieces of car are actually extracted from a repository, so i want all entities without dependencies to any repository.
Example:
public IndiaPieceOfCar:PieceOfCar()
{
public IndiaPieceOfCar(TaxRepository taxesRepo)
{//TaxRepository injected}
public override double GetTaxPrice();
{
var taxesIndia= TaxesRepository.GetTaxFromIndia(); //different type of objets are used to calculate
return (UnitCost + Transportation)^ taxesIndia.BadulakeTax ;
}
}
I would like to get the tax calculation decoupled. What would you suggest?
Factory method will do the job if the parameter for all the GetTaxPrice() were the same for all IdOriginCountries, but I am not that lucky and each country requires specific set of parameters.
Also the info retrieved by the taxesRepositories is an array of objects, it will be nice if it is not retrieved every time I create a PieceOfCar instance. If you need more info please let me know.
TaxCalculator
orTaxCalculationStrategy
?