Today we'll be building a system that will assist a vehicle dealership to calculate prices when a user wants to buy/lease a car. A dealership can also buy vehicles from customers.
There are three types of vehicles in the dealership: Car, Truck and Motorcycle. All these vehicles have the same attributes: maker, model, year, base_price (important, it'll be used to compute sale and purchase final prices), miles (see tests for more details).
There are two types of customers (both extending the class Person): Customer and Employee. An Employee can decide to buy a car from the dealership, and she/he will have a discount. We'll talk about this in the Contracts section below.
A dealership can either buy or sell vehicles. The dealership will set special prices for both, selling and buying vehicles, which will be computed using the base_price specified. The prices are computed in this way:
-
Sale price (
vehicle.sale_price(), the price a customer has to pay TO the dealership to buy a vehicle):base_price * S. In this caseSis a multiplier that will vary depending on the type of vehicle. (see section below). -
Purchase price (
vehicle.purchase_price(), the price the dealership will pay a customer to buy her/his vehicle):sale_price() - (P * miles). Similar toS,Pwill be a multiplier depending on the type of vehicle.
Sale multipliers
Car: 1.2Motorcycle: 1.1Truck: 1.6
Purchase multipliers
Car: 0.004Motorcycle: 0.009Truck: 0.02
A customer can either buy or lease a car. There are two types of contracts to support such operations: BuyContract and LeaseContract. A contract will have two important methods:
total_value()is the total value of the contract to pay by a customer.monthly_value()is the amount of money that a customer is supposed to pay monthly.
Each contract will have a different way to compute its value. But for both of them the value will depend of the price of the vehicle (discussed above) and the type of Customer. If the customer is an Employee, the contract will have a final discount of 10%. If the customer is a regular Customer, there's no discount applied.
A BuyContract is created by passing the following attributes:
customer: Either a regularCustomeror anEmployeevehicle: The vehicle involved in the transaction.monthly_payments: How many months the customer is going to take to pay for the whole contract. Example, ifmonthly_paymentsis 2, the customer will take two months to pay for it.
The total_value() of a BuyContract will be computed in this way: vehicle.sale_price() + (I * monthly_payments * sale_price() / 100) - (discount if employee). In this case I is the interest rate applied and it will vary depending of the type of vehicle:
Car: 7% monthly (1.07)Motorcycle: 3% monthly (1.03)Truck: 11% monthly (1.11)
The monthly_value of the contract will be just the total value divided by the amount of months: total_value() / monthly_payments.
A LeaseContract is created by passing the following attributes:
customer: Either a regularCustomeror anEmployeevehicle: The vehicle involved in the transaction.length_in_months: How many months the customer is going to lease the vehicle.
The total_value() of a LeaseContract will be computed in this way: vehicle.sale_price() + (lease_multiplier) - (discount if employee) * . In this case lease_multiplier depends on the vehicle and is computed in the following way:
Car:sale_price() + (sale_price() * 1.2 / length_in_months).Motorcycle:sale_price() + (sale_price() * 1 / length_in_months)Truck:sale_price() + (sale_price() * 1.7 / length_in_months)
The monthly_value of the contract will be just the total value divided by the amount of months: total_value() / length_in_months.
Given a car with base_price $8,000 and 21,000 miles:
sale_price()is:$8,000 x 1.2=$9,600purchase_price()is:sale_price() - [0.004 * 21,000 (miles)]=9600 - (0.004 * 21000)=$9,516
Given a motorcycle with base_price $11,500 and 5000 miles:
sale_price()is:$11,500 x 1.1=$12,650purchase_price()is:sale_price() - [0.009 * 5,000 (miles)]=12650 - (0.009 * 5000)=$12,605
Given a truck with base_price $21,900 and 50,000 miles
sale_price()is:$21,900 x 1.6=$35,040purchase_price()is:sale_price() - [0.02 * 50,000 (miles)]=35040 - (0.02 * 50000)=$34,040
Given a Car with sale_price() $15,000, a regular customer (not employee) and 12 monthly_payments:
total_value():sale_price() + (1.07 * monthly_payments * sale_price() / 100) - (discount if employee)=$15,000 + (1.07 * 12 * $15,000 / 100) - $0 (no employee)=15000 + (1.07 * 12 * 15000 / 100)=$16,926monthly_value():total_value() / monthly_payments=$16,926 / 12=$1,410.50
Given a Truck with sale_price() $35,000, a regular customer (not employee) and 36 length_in_months:
total_value():sale_price() + (sale_price() * 1.7 / length_in_months)=$35,000 + (35,000 * 1.7 / 36)=$36,652.77monthly_value():total_value() / monthly_payments=$36,652.78 / 36=$1,018.13