Example #1
0
        public static ApplicationResult SwitchAccount(string targetAccountType)
        {
            if (CurrentAccount != null && targetAccountType == CurrentAccount.Type)
            {
                return(new ApplicationResult {
                    Success = false,
                    Message = $"{targetAccountType} is already chosen"
                });
            }
            var result = AccountManager.Data.FirstOrDefault(acc => acc.Type == targetAccountType);

            if (result == null)
            {
                return(new ApplicationResult {
                    Success = false,
                    Message = $"Type of {targetAccountType} does not exist"
                });
            }
            AccountActionManager?.Dispose();
            CurrentAccount       = result;
            AccountActionManager = new AccountActionManager(CurrentAccount.Id);
            AccountActionManager.LoadData();
            return(new ApplicationResult {
                Success = true,
                Message = "Successfully switched"
            });
        }
Example #2
0
 public static ApplicationResult ModifyAccount(string newAccountCurrencyType = null, string newAccountType = null)
 {
     if (CurrentAccount == null)
     {
         return(new ApplicationResult {
             Success = false,
             Message = "No active accounts"
         });
     }
     if (string.IsNullOrEmpty(newAccountCurrencyType) && string.IsNullOrEmpty(newAccountType))
     {
         return(new ApplicationResult {
             Success = false,
             Message = "At least one argument must be non-null"
         });
     }
     if (newAccountCurrencyType != null && newAccountCurrencyType != CurrentAccount.Currency.Type)
     {
         if (!AccountCurrency.Exists(newAccountCurrencyType))
         {
             return(new ApplicationResult {
                 Success = false,
                 Message = $"Currency {newAccountCurrencyType} is not supported"
             });
         }
         CurrentAccount.Currency.Type = newAccountCurrencyType;
         foreach (var item in AccountActionManager.Data)
         {
             item.Currency.Type = newAccountCurrencyType;
         }
         AccountActionManager.UpdateData();
     }
     if (newAccountType != null && newAccountType != CurrentAccount.Type)
     {
         if (AccountManager.Data.Exists(acc => acc.Type == newAccountType))
         {
             return(new ApplicationResult {
                 Success = false,
                 Message = $"Account {newAccountType} already exists"
             });
         }
         CurrentAccount.Type = newAccountType;
     }
     AccountManager.UpdateData();
     return(new ApplicationResult {
         Success = true,
         Message = "Successfully modified the account"
     });
 }
Example #3
0
 static Application()
 {
     AccountManager = new AccountManager("AccountsData.json");
     AccountManager.LoadData();
     AccountActionManager = null;
     if (AccountManager.Data.Count == 0)
     {
         CurrentAccount       = null;
         AccountActionManager = null;
     }
     else
     {
         CurrentAccount       = AccountManager.Data[0];
         AccountActionManager = new AccountActionManager(CurrentAccount.Id);
         AccountActionManager.LoadData();
     }
 }
Example #4
0
        public static ApplicationResult AddIncomeOrExpense(AccountActionType actionType, string currencyType, decimal amount, string category, string note)
        {
            if (amount <= 0)
            {
                return(new ApplicationResult {
                    Success = false,
                    Message = "Amount cannot be zero or less than zero"
                });
            }
            if (currencyType.ToLower() == "auto")
            {
                currencyType = CurrentAccount.Currency.Type;
            }
            var currentAccountCurrency = CurrentAccount.Currency;

            if (currencyType != CurrentAccount.Currency.Type)
            {
                try {
                    var result = AccountCurrency.Convert(currencyType, CurrentAccount.Currency.Type, amount);
                    DoIncomeOrExpenseOperation(actionType, currentAccountCurrency, result);
                    AccountActionManager.AddAccountAction(new AccountAction(actionType, new AccountCurrency(CurrentAccount.Currency.Type, result), category, note));
                    AccountActionManager.UpdateData();
                    AccountManager.UpdateData();
                } catch (BadConversionException ex) {
                    return(new ApplicationResult {
                        Success = false,
                        Message = ex.Message
                    });
                }
                return(new ApplicationResult {
                    Success = true,
                    Message = "Successfully converted and added"
                });
            }
            DoIncomeOrExpenseOperation(actionType, currentAccountCurrency, amount);
            AccountActionManager.AddAccountAction(new AccountAction(actionType, new AccountCurrency(currencyType, amount), category, note));
            AccountActionManager.UpdateData();
            AccountManager.UpdateData();
            return(new ApplicationResult {
                Success = true,
                Message = "Successfully added"
            });
        }
Example #5
0
        public static ApplicationResult RemoveAccount(string type)
        {
            var result = AccountManager.Data.FirstOrDefault(x => x.Type == type);

            if (result == null)
            {
                return(new ApplicationResult {
                    Success = false,
                    Message = $"{type} does not exist"
                });
            }
            if (type == CurrentAccount.Type)
            {
                AccountManager.DeleteAccount(acc => acc == CurrentAccount);
                AccountManager.UpdateData();
                AccountActionManager.Destroy();
                AccountActionManager = null;
                if (AccountManager.Data.Count != 0)
                {
                    CurrentAccount = AccountManager.Data.First();
                }
                else
                {
                    CurrentAccount = null;
                }
            }
            else
            {
                new AccountActionManager(result.Id).Destroy();
                AccountManager.DeleteAccount(acc => acc.Type == type);
                AccountManager.UpdateData();
            }
            return(new ApplicationResult {
                Success = true,
                Message = "Successfully deleted"
            });
        }