Exemple #1
0
        private static void MenuSelection()
        {
            int menuSelection = -1;

            while (menuSelection != 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Welcome to TEnmo! Please make a selection: ");
                Console.WriteLine("1: View your current balance");
                Console.WriteLine("2: View your past transfers");
                Console.WriteLine("3: View your pending requests");
                Console.WriteLine("4: Send TE bucks");
                Console.WriteLine("5: Request TE bucks");
                Console.WriteLine("6: Log in as different user");
                Console.WriteLine("0: Exit");
                Console.WriteLine("---------");
                Console.Write("Please choose an option: ");

                if (!int.TryParse(Console.ReadLine(), out menuSelection))
                {
                    Console.WriteLine("Invalid input. Please enter only a number.");
                }
                else if (menuSelection == 1)
                {
                    decimal?balance = apiService.GetAccountBalance();

                    if (balance != null)
                    {
                        decimal balanceAmount = (decimal)balance;
                        Console.WriteLine($"Here's your account balance: {balanceAmount.ToString("C2")}");
                    }
                }
                else if (menuSelection == 2)
                {
                    List <API_Transfer> transfers = apiService.GetTransfers();
                    if (transfers == null)
                    {
                        continue;
                    }

                    consoleService.PrintAllTransfers(transfers);

                    int chosenID = consoleService.PromptForTransferID("get details");

                    Func <API_Transfer, bool> search = t => t.TransferID == chosenID;

                    if (transfers.Any(search))
                    {
                        API_Transfer transfer = transfers.Single(search);

                        consoleService.PrintTransferDetails(transfer);
                    }
                    else if (chosenID != 0)
                    {
                        Console.WriteLine("The transfer ID you requested does not exist.");
                    }
                }
                else if (menuSelection == 3)
                {
                    List <API_Transfer> transfers = apiService.GetTransfers();
                    if (transfers == null)
                    {
                        continue;
                    }

                    consoleService.PrintPendingTransfers(transfers);
                    int chosenID = consoleService.PromptForTransferID("approve or reject request");
                    int approvalOption;

                    List <API_Transfer>       pendingTransfers = transfers.Where(t => t.TransferStatus == TransferStatus.Pending).ToList();
                    Func <API_Transfer, bool> search           = t => t.TransferID == chosenID;

                    if (pendingTransfers.Any(search))
                    {
                        API_Transfer pendingTransfer = pendingTransfers.Single(search);
                        approvalOption = consoleService.PromptForApproval();
                        apiService.UpdateTransferStatus(pendingTransfer, approvalOption);
                    }
                    else if (chosenID != 0)
                    {
                        Console.WriteLine("The transfer ID you requested does not exist.");
                    }
                }
                else if (menuSelection == 4)
                {
                    List <API_User> users = apiService.GetOtherUsers();
                    if (users == null)
                    {
                        continue;
                    }

                    consoleService.PrintUsers(users);
                    TransferType transferType = TransferType.Send;
                    API_Transfer transfer     = consoleService.PromptForTransferRequest(transferType);

                    if (!users.Any(u => u.UserId == transfer.ToUserID))
                    {
                        Console.WriteLine("The user ID you requested does not exist.");
                        continue;
                    }

                    transfer.ToUserName = users.First(u => u.UserId == transfer.ToUserID)?.Username;
                    apiService.SubmitTransferRequest(transfer);
                }
                else if (menuSelection == 5)
                {
                    List <API_User> users = apiService.GetOtherUsers();
                    if (users == null)
                    {
                        continue;
                    }

                    consoleService.PrintUsers(users);
                    TransferType transferType = TransferType.Request;
                    API_Transfer transfer     = consoleService.PromptForTransferRequest(transferType);

                    if (!users.Any(u => u.UserId == transfer.FromUserID))
                    {
                        Console.WriteLine("The user ID you requested does not exist.");
                        continue;
                    }

                    transfer.FromUserName = users.First(u => u.UserId == transfer.FromUserID).Username;
                    apiService.SubmitTransferRequest(transfer);
                }
                else if (menuSelection == 6)
                {
                    Console.WriteLine("");
                    UserService.SetLogin(new API_User()); //wipe out previous login info
                    Run();                                //return to entry point
                }
                else
                {
                    Console.WriteLine("Goodbye!");
                    Environment.Exit(0);
                }
            }
        }