I'm working on a calculator for balancing investments to match a model portfolio.
The classes that I developed look like this:
export class Portfolio {
public name: string;
public risks: Risk[];
}
export class Risk {
name: string;
funds: Fund[];
}
export class Fund {
shares: number;
value?: number;
percent: number;
}
This structure works, but the Fund[]
is really a property of the portfolio and the property on Fund
called percent is the only thing that changes between the Risks. In the application the user can toggle between risks and see what they need to buy and sell, but when they do, all of the inputs clear because they are bound to a different instance.
If funds
is moved from Risk to Portfolio how can the risk class store a value for each fund?
export class Portfolio {
name: string;
risks: Risk[];
funds: Fund[];
}
export class Risk {
constructor (public name: string, public percents: number[]) {}
}
export class Fund {
shares: number;
value?: number;
percent: number;
}
export class PortfoliosService {
setPercents(riskName) {
this.currentRisk = this.currentPortfolio.risks.filter(x => x.name === riskName)[0];
for (const i in this.currentRisk.percents) {
this.currentPortfolio.funds[i].percent = this.currentRisk.percents[i];
}
}
}
This works as well, the problem is that it relies on two independent arrays being the same size and in the same order. Is there any way to associate the percent value with an instance of fund so that if they don't match it is caught by the compiler?
Aucun commentaire:
Enregistrer un commentaire