using System; using System.Collections.Generic; using System.Linq; namespace Game { public static class Controller { /// /// Manages the initial interaction with the user. /// public static void StartGame() { View.ShowBanner(); var showInstructions = GetYesOrNo(View.PromptShowInstructions); View.ShowSeparator(); if (showInstructions) View.ShowInstructions(); View.ShowSeparator(); } /// /// Gets a yes or no answer from the user. /// /// /// Displays the prompt. /// /// /// True if the user answered yes and false if he or she answered no. /// public static bool GetYesOrNo(Action prompt) { prompt(); var response = default(char); do { response = Console.ReadKey(intercept: true).KeyChar; } while (response != '0' && response != '1'); View.ShowChar(response); return response == '1'; } /// /// Gets a transaction amount for each company in the given collection /// of companies and returns the updated assets. /// /// /// The assets to update. /// /// /// The collection of companies. /// /// /// The updated assets. /// public static Assets UpdateAssets(Assets assets, IEnumerable companies) { while (true) { View.PromptEnterTransactions(); var result = Broker.Apply ( assets, companies.Select(GetTransactionAmount).ToList(), companies); switch (result) { case (Assets newAssets, TransactionResult.Ok): return newAssets; case (_, TransactionResult.Oversold): View.ShowOversold(); break; case (Assets newAssets, TransactionResult.Overspent): View.ShowOverspent(-newAssets.Cash); break; } } } /// /// Gets a transaction amount for the given company. /// /// /// The company to buy or sell. /// /// /// The number of shares to buy or sell. /// public static int GetTransactionAmount(Company company) { while (true) { View.PromptBuySellCompany(company); var input = Console.ReadLine(); if (input is null) Environment.Exit(0); else if (!Int32.TryParse(input, out var amount)) View.PromptValidInteger(); else return amount; } } } }