UNPKG

@votemike/property

Version:

Property class and related classes

40 lines (39 loc) 1.6 kB
import Finance from './finance'; import Payment from './payment'; import Rental from './rental'; export default class Property { constructor(name, finances, payments, rentals) { this.name = name; this.finances = finances; this.payments = payments; this.rentals = rentals; } calculateMonthlyCost(useTeaserRate = false) { const financeCosts = this.finances.reduce((total, finance) => (useTeaserRate ? finance.monthlyTeaserCostOfFinance : finance.monthlyCostOfFinance) + total, 0); const paymentCosts = this.payments.reduce((total, payment) => payment.monthlyCost + total, 0); return financeCosts + paymentCosts; } calculateMonthlyIncome() { return this.rentals.reduce((total, rental) => rental.monthlyIncome + total, 0); } calculateMonthlyProfit(useTeaserRate = false) { return this.calculateMonthlyIncome() - this.calculateMonthlyCost(useTeaserRate); } static fromJson(json) { const property = Object.create(Property.prototype); return Object.assign(property, json, { finances: json.finances.map((financeObject) => { return Finance.fromJson(financeObject); }), payments: json.payments.map((paymentObject) => { return Payment.fromJson(paymentObject); }), rentals: json.rentals.map((rentalObject) => { return Rental.fromJson(rentalObject); }) }); } static reviver(key, value) { return key === "" ? Property.fromJson(value) : value; } }