Ejemplo n.º 1
0
        //Method for calculating the updated balance record value of the month which previously contained the modified saving/saving account expense(applies when "moving" a record from one month to the other)
        private int calculateOldBalanceRecordValue(SavingAccountBalanceManager oldMonthRecordManager, int oldRecordValue, BudgetItemType selectedItemType)
        {
            if (oldMonthRecordManager == null || selectedItemType == BudgetItemType.UNDEFINED)
            {
                return(-1);
            }

            int oldBalanceRecordValue     = -1;
            int currentBalanceRecordValue = oldMonthRecordManager.getRecordValue();//Retrieving the current value of the record that will be updated

            if (currentBalanceRecordValue == -1)
            {
                return(-1);
            }

            if (selectedItemType == BudgetItemType.SAVING_ACCOUNT_EXPENSE)
            {
                //currentBalanceRecordValue = oldMonthRecordManager.getRecordValue();//Retrieving the current value of the record that will be updated
                oldBalanceRecordValue = currentBalanceRecordValue + oldRecordValue;//The balance of the month from where the expense was "moved" will increase
            }
            else if (selectedItemType == BudgetItemType.SAVING)
            {
                //currentBalanceRecordValue = oldMonthRecordManager.getRecordValue();//Retrieving the current value of the record that will be updated
                oldBalanceRecordValue = currentBalanceRecordValue - oldRecordValue;//The balance of the month from where the saving was "moved" will decrease
            }

            return(oldBalanceRecordValue);
        }
Ejemplo n.º 2
0
        //Method for calculating the new balance record value after changing the current value of the saving/saving account expense(applies when the record isn't "moved" from one month to the other)
        private int calculateNewBalanceRecordValue(SavingAccountBalanceManager newMonthRecordManager, int oldRecordValue, int newRecordValue, int comparisonResult, BudgetItemType selectedItemType, bool performDeletion)
        {
            if (newMonthRecordManager == null || selectedItemType == BudgetItemType.UNDEFINED)
            {
                return(-1);
            }

            int newBalanceRecordValue     = -1;
            int currentBalanceRecordValue = newMonthRecordManager.getRecordValue();

            if (currentBalanceRecordValue == -1)
            {
                //return -1;
                currentBalanceRecordValue = 0;//CHANGE!!!
            }

            if (!performDeletion)
            {
                //Applies when the user has changed the record date or both the record date and record value
                if (hasChangedRecordDate || (hasChangedRecordValue && hasChangedRecordDate))
                {
                    if (selectedItemType == BudgetItemType.SAVING_ACCOUNT_EXPENSE)
                    {
                        newBalanceRecordValue = currentBalanceRecordValue - newRecordValue;//The new balance record value for the corresponding month to which the date was modified is obtained by subtracting the new expense value from its current value
                    }
                    else if (selectedItemType == BudgetItemType.SAVING)
                    {
                        newBalanceRecordValue = currentBalanceRecordValue + newRecordValue;//The new balance record value for the corresponding month to which the date was modified is obtained by adding the new saving value to its current value
                    }
                }
                else if (hasChangedRecordValue)
                {
                    int recordDifferenceValue = 0;
                    if (comparisonResult == -1)
                    {
                        //The new record value is lower than the old record value
                        recordDifferenceValue = oldRecordValue - newRecordValue;
                        if (selectedItemType == BudgetItemType.SAVING_ACCOUNT_EXPENSE)
                        {
                            newBalanceRecordValue = currentBalanceRecordValue + recordDifferenceValue;//The new expense is lower than the old expense so the balance record value increases
                        }
                        else if (selectedItemType == BudgetItemType.SAVING)
                        {
                            newBalanceRecordValue = currentBalanceRecordValue - recordDifferenceValue;//The new saving is lower than the old saving so the balance decreases
                        }
                    }
                    else if (comparisonResult == 1)
                    {
                        //The new record value is higher than the old record value
                        recordDifferenceValue = newRecordValue - oldRecordValue;
                        if (selectedItemType == BudgetItemType.SAVING_ACCOUNT_EXPENSE)
                        {
                            newBalanceRecordValue = currentBalanceRecordValue - recordDifferenceValue;//The new expense is higher than the old expense so the balance record value decreases
                        }
                        else if (selectedItemType == BudgetItemType.SAVING)
                        {
                            newBalanceRecordValue = currentBalanceRecordValue + recordDifferenceValue;//The new saving is higher than the old saving so the balance record value increases
                        }
                    }
                }
            }
            else
            {
                if (selectedItemType == BudgetItemType.SAVING_ACCOUNT_EXPENSE)
                {
                    newBalanceRecordValue = currentBalanceRecordValue + deletedRecordValue;//The expense is deleted so the balance record value increases by the deleted value(the deleted record value is used since the record was not modified)
                }
                else if (selectedItemType == BudgetItemType.SAVING)
                {
                    newBalanceRecordValue = currentBalanceRecordValue - deletedRecordValue;//The saving is deleted so the balance record value decreases by the deleted value(the deleted record value is used since the record was not modified)
                }
            }


            return(newBalanceRecordValue);
        }
Ejemplo n.º 3
0
        //Method for updating the records in the saving account balance table and creating records in the saving account expense table when needed(secondary task)
        private int updateSavingAccountBalanceTable(int userID, int month, int year, DateTime date, BudgetItemType selectedItemType, bool performDeletion)
        {
            int executionResult = -1;

            //Object that manages the update of the tables that are part of the saving account system(initial object which represents the balance record of the unmodified date)
            SavingAccountBalanceManager balanceManager = new SavingAccountBalanceManager(userID, month, year, date);

            int recordComparisonResult = balanceManager.compareRecordValues(newRecordValue, oldRecordValue);

            //Balance record table update when only the values of the saving/saving account expense records are modified
            if (!performDeletion)
            {
                if (balanceManager.hasBalanceRecord())
                {
                    int oldBalanceRecordValue = balanceManager.getRecordValue();
                    int newBalanceRecordValue = 0;
                    //int recordDifferenceValue = 0;

                    //Applies when the user has changed the record date or both the record date and record value
                    if (hasChangedRecordDate || (hasChangedRecordValue && hasChangedRecordDate))
                    {
                        int newMonth = newRecordDate.Month;
                        int newYear  = newRecordDate.Year;


                        //A new SavingAccountBalanceManager is created which represents the balance record of the newly modified date
                        SavingAccountBalanceManager newBalanceManager = new SavingAccountBalanceManager(userID, newMonth, newYear, newRecordDate);

                        if (!hasChangedRecordValue)
                        {
                            oldRecordValue = newRecordValue;//Current value of the record(unmodified)
                        }

                        if (newBalanceManager.hasBalanceRecord())
                        {
                            //If a balance record already exists it will be updated(the record to which the modified value is "moved")
                            //newBalanceRecordValue = newBalanceManager.getRecordValue() - newRecordValue;//The new balance record value for the corresponding month to which the date was modified is obtained by subtracting the new expense value from its current value
                            newBalanceRecordValue = calculateNewBalanceRecordValue(newBalanceManager, oldRecordValue, newRecordValue, recordComparisonResult, getSelectedBudgetItemType(), false);//CHANGE
                            executionResult       = newBalanceRecordValue != -1 ? newBalanceManager.updateBalanceRecord(newBalanceRecordValue) : -1;

                            //After that the balance record representing the old date is also updated(the record from which the modified value is "moved")
                            int previousMonthBalanceUpdatedRecordValue = calculateOldBalanceRecordValue(balanceManager, oldRecordValue, getSelectedBudgetItemType());//CHANGE
                            executionResult = previousMonthBalanceUpdatedRecordValue != -1 ? balanceManager.updateBalanceRecord(previousMonthBalanceUpdatedRecordValue) : -1;
                        }
                        else
                        {
                            //Calculates the value for a balance record entry that does not exist yet but will be created(user "moves" a saving account expense/saving to a month having no balance record yet)
                            newBalanceRecordValue = calculateNewBalanceRecordValue(newBalanceManager, oldRecordValue, newRecordValue, recordComparisonResult, getSelectedBudgetItemType(), false);
                            //If not a new record with the newly modified value will be created
                            executionResult = newBalanceManager.createBalanceRecord(newBalanceRecordValue);

                            //Updating the old balance record(from where the saving account expense/saving is "moved")
                            int previousMonthBalanceUpdatedRecordValue = calculateOldBalanceRecordValue(balanceManager, oldRecordValue, getSelectedBudgetItemType());
                            executionResult = previousMonthBalanceUpdatedRecordValue != -1 ? balanceManager.updateBalanceRecord(previousMonthBalanceUpdatedRecordValue) : -1;
                        }
                    }
                    else if (hasChangedRecordValue)
                    {
                        newBalanceRecordValue = calculateNewBalanceRecordValue(balanceManager, oldRecordValue, newRecordValue, recordComparisonResult, getSelectedBudgetItemType(), false);
                        executionResult       = newBalanceRecordValue != -1 ? balanceManager.updateBalanceRecord(newBalanceRecordValue) : -1;//The balance record is updated only if the newly calculated value is greater than -1(-1 means that something went wrong during the calculation process)
                    }
                }
            }
            else
            {
                //Balance record table update when a saving/saving account expense record is deleted
                SavingAccountBalanceManager deletionBalanceManager = new SavingAccountBalanceManager(userID, month, year, date);
                int newBalanceRecordValue = calculateNewBalanceRecordValue(deletionBalanceManager, oldRecordValue, newRecordValue, recordComparisonResult, getSelectedBudgetItemType(), true); //CHANGE
                executionResult = newBalanceRecordValue != -1 ? deletionBalanceManager.updateBalanceRecord(newBalanceRecordValue) : -1;
            }

            return(executionResult);
        }