Skip to content

Commit

Permalink
feat: Setup constructor param types
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Aug 2, 2024
1 parent 8bede03 commit 5386b54
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
29 changes: 14 additions & 15 deletions app/models/Mortgage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 } =
Expand Down
52 changes: 21 additions & 31 deletions app/models/tenure/FairholdLandPurchase.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 5386b54

Please sign in to comment.