Setting:
Ionic version: 6.20.1
Angular CLI version: 10.0.8
In the process of developing a mobile expense management application, I am working on implementing a feature that calculates the recommended spending for different categories (e.g., home expenses).
Issue:
I have checkboxes representing various spending categories (e.g., home ❏, education ❏, entertainment ❏) that users can select or deselect. These categories are interconnected in such a way that if one is deactivated, its percentage is added to another related category (e.g., deactivating "Home 15%" adds its percentage to "Shopping 6%"). This selection is saved in the database and retrieved to determine the recommended spend percentages.
Objective:
I would like to handle the following conditions when a category is disabled:
If a category is deactivated (false), its value shifts to its related category.
If two related categories are deactivated, their values are transferred to a third related category (e.g., deactivating "Home 15%", "Shopping 6%" transfers their values to "Food -> 49%").
If three linked categories are disabled, a portion of their values is moved to savings (worth 10% of the total) and the rest to another category.
Proposed Solution:
To address this issue, I have designed a set of conditions covering all categories so that if one is false, its value passes to the appropriate related category. Here is an example code snippet demonstrating this approach:
// Example implementation to handle shifting values based on category deactivation
// Check if 'Home' is deactivated and redistribute value accordingly
if (i.home == false && i.shopping == true && ... && i.entertainment == true) {
this.sumaCatPurchases = (this.sumaTotalIncomes * 0.21).toFixed(2);
// Assign similar calculations for other categories as needed
}
This method helps maintain the integrity of each category's default value while ensuring seamless value transfer upon deactivation of a specific category.
However, the drawback of this approach is the extensive manual coding required for handling multiple scenarios involving category deselection. If numerous categories need to be managed simultaneously, the complexity grows exponentially, making it impractical to cover all possible combinations individually.
If anyone has insights or suggestions on how to improve this process and make it more efficient, I would greatly appreciate your input.