diff --git a/app/models/Mortgage.ts b/app/models/Mortgage.ts index f844c6a..67761ef 100644 --- a/app/models/Mortgage.ts +++ b/app/models/Mortgage.ts @@ -4,6 +4,13 @@ const DEFAULT_INTEREST_RATE = 0.06; const DEFAULT_MORTGAGE_TERM = 30; const DEFAULT_INITIAL_DEPOSIT = 0.15; +interface MortgageParams { + propertyValue: number; + interestRate?: number; + mortgageTerm?: number; + initialDeposit?: number; +} + type MortgageBreakdown = { yearlyPayment: number; cumulativePaid: number; @@ -29,21 +36,13 @@ export class Mortgage { totalMortgageCost: number; yearlyPaymentBreakdown: MortgageBreakdown; - constructor({ - propertyValue, - interestRate = DEFAULT_INTEREST_RATE, - mortgageTerm = DEFAULT_MORTGAGE_TERM, - initialDeposit = DEFAULT_INITIAL_DEPOSIT, - }: { - propertyValue: number; - interestRate?: number; - mortgageTerm?: number; - initialDeposit?: number; - }) { - this.propertyValue = propertyValue; - this.initialDeposit = initialDeposit; - this.interestRate = interestRate; - this.termYears = mortgageTerm; + constructor(params: MortgageParams) { + this.propertyValue = params.propertyValue; + this.initialDeposit = params.initialDeposit || DEFAULT_INITIAL_DEPOSIT; + this.interestRate = params.interestRate || DEFAULT_INTEREST_RATE; + this.termYears = params.mortgageTerm || DEFAULT_MORTGAGE_TERM; + + // Computed properties, order is significant this.principal = this.calculateMortgagePrinciple(); const { monthlyPayment, totalMortgageCost } = diff --git a/app/models/tenure/FairholdLandPurchase.ts b/app/models/tenure/FairholdLandPurchase.ts index 2e8ee50..3d384ff 100644 --- a/app/models/tenure/FairholdLandPurchase.ts +++ b/app/models/tenure/FairholdLandPurchase.ts @@ -1,6 +1,17 @@ import { Fairhold } from "../Fairhold"; import { Mortgage } from "../Mortgage"; +interface FairholdLandPurchaseParams { + newBuildPrice: number; + depreciatedBuildPrice: number; + constructionPriceGrowthPerYear: number; + yearsForecast: number; + maintenanceCostPercentage: number; + incomeGrowthPerYear: number; + affordability: number; + fairhold: Fairhold; +}; + type Lifetime = { maintenanceCost: number; landMortgagePaymentYearly: number; @@ -13,47 +24,26 @@ export class FairholdLandPurchase { depreciatedHouseMortgage: Mortgage; lifetime: Lifetime; - constructor({ - newBuildPrice, - depreciatedBuildPrice, - constructionPriceGrowthPerYear, - yearsForecast, - maintenanceCostPercentage, - fairhold, - }: { - newBuildPrice: number; - depreciatedBuildPrice: number; - constructionPriceGrowthPerYear: number; - yearsForecast: number; - maintenanceCostPercentage: number; - incomeGrowthPerYear: number; - affordability: number; - fairhold: Fairhold; - }) { - this.discountedLandPrice = fairhold.calculateDiscountedPriceOrRent(); + constructor(params: FairholdLandPurchaseParams) { + this.discountedLandPrice = params.fairhold.calculateDiscountedPriceOrRent(); this.discountedLandMortgage = new Mortgage({ propertyValue: this.discountedLandPrice, }); this.depreciatedHouseMortgage = new Mortgage({ - propertyValue: depreciatedBuildPrice, + propertyValue: params.depreciatedBuildPrice, }); - this.lifetime = this.calculateLifetime( - newBuildPrice, - maintenanceCostPercentage, - yearsForecast, - constructionPriceGrowthPerYear - ); + this.lifetime = this.calculateLifetime(params); } - private calculateLifetime( - newBuildPrice: number, - maintenanceCostPercentage: number, - yearsForecast: number, - constructionPriceGrowthPerYear: number - ) { + private calculateLifetime({ + newBuildPrice, + maintenanceCostPercentage, + yearsForecast, + constructionPriceGrowthPerYear, + }: FairholdLandPurchaseParams) { let newBuildPriceIterative = newBuildPrice; let maintenanceCostIterative = maintenanceCostPercentage * newBuildPrice;