示例#1
0
        static void Main(string[] args)
        {
            IDataAccessLayer dal = new JsonFileAccessLayer("C:\\testingAccount.bank");
            //CreateTestAccounts(dal);
            AccountHolder ach = DemandAuthentication(dal);

            Command userCommand = null;
            while (userCommand == null || userCommand.Inquiry != TransactionTypes.Quit)
            {
                string amount = "";
                string direction = "";
                userCommand = PromptUser("Please select one of the following commands:", ach.AvailableCommands);
                if (userCommand != null && userCommand.Inquiry != TransactionTypes.Quit)
                {
                    Account chosen = null;

                    if (RequireCountAndDirection.Contains(userCommand.Inquiry))
                    {
                        Command takeOneAcount = new Command(string.Format("return only 1 account."), TransactionTypes.ListAccounts);
                        Command takeFiveAccounts = new Command(string.Format("return 5 accounts."), TransactionTypes.ListAccounts);
                        List<Command> howManyAccounts = new List<Command>()
                        {
                            takeOneAcount,
                            takeFiveAccounts
                        };
                        Command oneOrFive = PromptUser("How many accounts would you like to list?", howManyAccounts);
                        amount = oneOrFive == takeOneAcount ? "1" : "5";

                        Command takeTopAccounts =  new Command(string.Format("view the top {0} account(s).", amount), TransactionTypes.ListAccounts);
                        Command takeBottomAccounts = new Command(string.Format("view the bottom {0} account(s).", amount), TransactionTypes.ListAccounts);
                        List<Command> possibleInquiries = new List<Command>()
                        {
                            takeTopAccounts,
                            takeBottomAccounts
                        };

                        Command topOrBottom = PromptUser("Which accounts would you like?", possibleInquiries);
                        userCommand = topOrBottom;
                        direction = userCommand == takeTopAccounts ? "desc" : "asc";

                    }

                    if (RequireAccount.Contains(userCommand.Inquiry))
                    {
                        chosen = ChooseTargetAccount(dal.LoadAccountsForUser(ach.Id));
                    }

                    if (userCommand.Inquiry == TransactionTypes.GetAccountAtDate)
                    {
                        chosen = ChooseTargetAccount(dal.GetAllAcccounts());
                    }

                    if (userCommand.Inquiry == TransactionTypes.ChangeCurrency)
                    {
                        Command useDollars = new Command(string.Format("view currency in {0}.", AvailableCurrencies.CurrencyList[0].Name), TransactionTypes.ListAccounts);
                        Command useEuros = new Command(string.Format("view currency in {0}", AvailableCurrencies.CurrencyList[1].Name), TransactionTypes.ListAccounts);
                        List<Command> possibleCurrencies = new List<Command>()
                        {
                            useDollars,
                            useEuros
                        };

                        Command dOrE = PromptUser("Please choose a currency: ", possibleCurrencies);

                        if (dOrE == useEuros)
                            amount = AvailableCurrencies.CurrencyList[1].Name;
                        else
                            amount = AvailableCurrencies.CurrencyList[0].Name;
                    }

                    if (RequireMoney.Contains(userCommand.Inquiry))
                    {
                        Console.WriteLine("Please enter the amount.");
                        amount = Console.ReadLine();
                    }

                    if (userCommand.Inquiry == TransactionTypes.GetAccountAtDate)
                    {
                        Console.WriteLine("Please enter a date to view account balance for.");
                        amount = Console.ReadLine();
                    }

                    InquiryResult result = ach.PerformInquiry(chosen, userCommand, dal, amount, direction);
                    foreach (string s in result.Response)
                        Console.WriteLine(s);

                    if (chosen != null)
                        dal.SaveAccount(chosen);
                }
            }

            // Say goodbye to the user.
            Console.WriteLine("Goodbye {0}!, press enter to conclude your session.", ach.DisplayName);
            Console.ReadLine();
        }
        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);
        }
        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.BalanceInquiry:
                    responses.Add(string.Format("Current Balance is:  {0}", selectedAccount.Balance));
                    break;
                case TransactionTypes.Deposit:
                    double depositing = 0d;
                    if (parameters != null && parameters.Count() > 0 && double.TryParse(parameters[0], out depositing))
                    {
                        responses.Add(string.Format("Deposited:  {0}", depositing));
                        selectedAccount.PerformTransaction(new Transaction("Deposited Funds", new Money(depositing, selectedAccount.LocalCurrency), toPerform.Inquiry));
                    }
                    break;
                case TransactionTypes.Withdrawal:
                    double withdrawing = 0d;
                    if (parameters != null && parameters.Count() > 0 && double.TryParse(parameters[0], out withdrawing))
                    {
                        responses.Add(string.Format("Withdrew:  {0}", withdrawing));
                        selectedAccount.PerformTransaction(new Transaction("Withdrew Funds", -new Money(withdrawing, selectedAccount.LocalCurrency), toPerform.Inquiry));
                    }
                    break;
                case TransactionTypes.ChangeCurrency:
                    Currency toSwitch = selectedAccount.LocalCurrency;
                    if (parameters != null && parameters.Count() > 0)
                    {
                        foreach (Currency c in AvailableCurrencies.CurrencyList)
                        {
                            if (c.Name == parameters[0])
                            {
                                toSwitch = c;
                                responses.Add(string.Format("Switching to currency:  {0}", c.Name));
                            }
                        }
                    }
                    selectedAccount.SwitchAccountCurrency(toSwitch);
                    responses.Add(string.Format("Completed currency exchange."));
                    goto case TransactionTypes.BalanceInquiry;
                default:
                    break;
            }

            return new InquiryResult(responses, followUps);
        }
 public InquiryResult PerformInquiry(Account selectedAccount, Command toPerform, IDataAccessLayer dal, params string[] parameters)
 {
     return injectedHolder.PerformInquiry(selectedAccount, toPerform, dal, parameters);
 }