コード例 #1
0
        public InquiryResult PerformInquiry(Account selectedAccount, Command toPerform, IDataAccessLayer dal, params string[] parameters)
        {
            List<string> responses = new List<string>();
            List<string> followUps = new List<string>();

            switch (toPerform.Inquiry)
            {
                case TransactionTypes.ListAccounts:

                    List<Account> foundAccounts = new List<Account>();
                    int numberToLoad = 5;
                    int.TryParse(parameters[0], out numberToLoad);

                    bool descOrder = true;
                    if (parameters != null && parameters.Count() > 1 && parameters[1] == "asc")
                        descOrder = false;

                    if (descOrder)
                        foundAccounts.AddRange(dal.HighestAccounts(numberToLoad));
                    else
                        foundAccounts.AddRange(dal.LowestAccounts(numberToLoad));

                    foreach (Account act in foundAccounts)
                    {
                        responses.Add(string.Format("Account {0} has {1} in it and is owned by {2}", act.Id, act.Balance.ToString(), act.Owner.DisplayName));
                    }

                    break;
                case TransactionTypes.GetAccountAtDate:
                    if (parameters != null && parameters.Count() > 0)
                    {
                        DateTime targetDate = DateTime.MinValue;
                        if(DateTime.TryParse(parameters[0], out targetDate))
                        {
                            if (selectedAccount.TransactionHistory.Count() > 0)
                            {
                                List<Transaction> allTransInOrder = selectedAccount.TransactionHistory.OrderBy(x => x.Time).ToList();
                                Transaction lastTransaction = allTransInOrder[0];
                                foreach(var t in allTransInOrder)
                                {
                                    if (t.Time > lastTransaction.Time && t.Time < targetDate)
                                    {
                                        lastTransaction = t;
                                    }
                                }

                                responses.Add(string.Format("Account {0} had {1} on {2}", selectedAccount.Id, lastTransaction.BalanceAfter, targetDate));
                            }
                        }

                    }
                    break;
                default:
                    break;
            }

            return new InquiryResult(responses, followUps);
        }