Sample Demo code developed by Don Silvasi-Patchin

The Function Implementation

Click here for a description of the problem to be solved
Click here for a downloadable full solution
Click here to return to the home page

    public class PriceDeterminator
    {
	public decimal DetermineCarPrice(Car car)
	{
    	    const int MONTHS_LIMIT = (int)12 * (int)10;    // Max age adjustment: 10 years in months
    	    const decimal AGE_FACTOR = 0.005m;             // Adjustment per month of age
    	    const int MILEAGE_LIMIT = 150;                 // Max mileage adjustment in 1000 mile units
	    const decimal MILEAGE_FACTOR = 0.002m;         // Adjustment per 1000 miles driven
	    const decimal MULTIPLE_OWNERS_FACTOR = 0.25m;  // Adjustment for more than 2 owners
	    const decimal NO_OWNERS_FACTOR = 0.10m;        // Adjustment for 0 owners
	    const int COLLISIONS_LIMIT = 5;                // Max number of collisions to count
	    const decimal COLLISIONS_FACTOR = 0.02m;       // Adjustment per collision

	    decimal PriceDetermined = car.PurchaseValue;
	    decimal EffectiveMonths = Math.Min(MONTHS_LIMIT, car.AgeInMonths);
	    int ThousandsOfMiles = Math.Min(MILEAGE_LIMIT, car.NumberOfMiles / (int)1000);
	    int EffectiveCollisions = Math.Min(COLLISIONS_LIMIT, car.NumberOfCollisions); ;

	    PriceDetermined -= (AGE_FACTOR * EffectiveMonths * PriceDetermined);            // Step 1
	    PriceDetermined -= (MILEAGE_FACTOR * ThousandsOfMiles * PriceDetermined);       // Step 2
	    if (car.NumberOfPreviousOwners > 2)
	    {
		PriceDetermined -= (MULTIPLE_OWNERS_FACTOR * PriceDetermined);              // Step 3
	    }
	    PriceDetermined -= (COLLISIONS_FACTOR * EffectiveCollisions * PriceDetermined); // Step 4
	    if (car.NumberOfPreviousOwners == 0)
	    {
		PriceDetermined += (NO_OWNERS_FACTOR * PriceDetermined);                    // Step 5
	    }

	    return PriceDetermined;
	}
    }

Click here for a description of the problem to be solved
Click here for a downloadable full solution
Click here to return to the home page