Пример #1
0
            /// <summary>
            /// removes all <see cref="PortfolioEntry"/>s from portfolio.
            /// returns whether command was executed successfully.
            /// </summary>
            /// <param name="commandArguments"></param>
            /// <returns>
            /// true if command executed successfully,
            /// else false
            /// </returns>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                try
                {
                    // get all coinIds in portfolio
                    long[] coinIdsToBeRemoved = PortfolioManager.Instance.CoinIds;

                    // remove all PortfolioEntry s from portfolio
                    PortfolioManager.Instance.RemoveCoins(coinIdsToBeRemoved);

                    // log successful removal notice to console
                    ConsoleIOManager.Instance.LogNotice(
                        "All entries were successfully removed from portfolio.",
                        ConsoleIOManager.eOutputReportType.CommandExecution);

                    commandExecutedSuccessfuly = true;

                    this.removedCoinIds = coinIdsToBeRemoved;
                }
                catch (DatabaseCommunicationException databaseCommunicationException)
                {
                    PortfolioCommand.HandleDatabaseCommunicationException(databaseCommunicationException);
                    commandExecutedSuccessfuly = false;
                }

                return(commandExecutedSuccessfuly);
            }
            /// <summary>
            /// removes <see cref="PortfolioEntry"/> corresponding to coin id
            /// specified in <paramref name="commandArguments"/>[0] from portfolio.
            /// returns whether command executed successfully.
            /// </summary>
            /// <seealso cref="CoinListingManager.GetCoinIdByNameOrSymbol(string)"/>
            /// <seealso cref="PortfolioManager.RemoveCoin(int)"/>
            /// <param name="commandArguments"></param>
            /// <returns>
            /// true if command executed successfully,
            /// else false
            /// </returns>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                // command arguments should be coin names or symbols
                string[] coinNamesOrSymbols = commandArguments;

                try
                {
                    // get coin ids by name or symbol
                    long[] coinIds =
                        CoinListingManager.Instance.GetCoinIdsByNamesOrSymbols(coinNamesOrSymbols);

                    // remove coins from portfolio
                    PortfolioManager.Instance.RemoveCoins(coinIds);

                    // log success notice
                    string portfolioCoinRemoveSuccessNotice = buildPortfolioCoinRemoveSuccessNotice(coinIds);
                    ConsoleIOManager.Instance.LogNotice(
                        portfolioCoinRemoveSuccessNotice,
                        ConsoleIOManager.eOutputReportType.CommandExecution);

                    commandExecutedSuccessfuly = true;
                }
                catch (CoinListingManager.CoinNameOrSymbolNotFoundException coinNameOrSymbolNotFoundException)
                {
                    // coin with specified name / symbol not found in listing repository
                    ConsoleIOManager.Instance.LogError(
                        coinNameOrSymbolNotFoundException.Message,
                        ConsoleIOManager.eOutputReportType.CommandExecution);

                    commandExecutedSuccessfuly = false;
                }
                catch (CoinNotInPortfolioException coinNotInPortfolioException)
                {
                    // coin id corresponding to given name / symbol does not exist in portfolio manager
                    long   coinId   = coinNotInPortfolioException.CoinId;
                    string coinName = CoinListingManager.Instance.GetCoinNameById(coinId);

                    ConsoleIOManager.Instance.LogErrorFormat(
                        false,
                        ConsoleIOManager.eOutputReportType.CommandExecution,
                        "There's no entry in portfolio manager for '{0}'.",
                        coinName);

                    commandExecutedSuccessfuly = false;
                }
                catch (DatabaseCommunicationException databaseCommunicationException)
                {
                    PortfolioCommand.HandleDatabaseCommunicationException(databaseCommunicationException);

                    commandExecutedSuccessfuly = false;
                }

                return(commandExecutedSuccessfuly);
            }
Пример #3
0
            /// <summary>
            /// prints portfolio data corresponding to coin name / symbols
            /// contained in <paramref name="commandArguments"/> (or all coins in portfolio if
            /// <paramref name="commandArguments"/>.Length == 0) in tabular format.
            /// returns whether command was executed successfully.
            /// </summary>
            /// <seealso cref="CoinListingManager.FetchCoinIds(string[])"/>
            /// <seealso cref="PortfolioManager.GetPortfolioEntryDisplayTableString(int[])"/>
            /// <param name="commandArguments"></param>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                try
                {
                    // only coin ids which have a corresponding portfolio entry are displayed
                    List <long>   coinIdsWithPortfolioEntry      = new List <long>();
                    List <string> coinNamesWithoutPortfolioEntry = new List <string>();

                    if (commandArguments.Length == 0)
                    {
                        // if no command args are provided, display all entries in portfolio
                        long[] allCoinIdsInPortfolio = PortfolioManager.Instance.CoinIds;
                        coinIdsWithPortfolioEntry.AddRange(allCoinIdsInPortfolio);
                    }
                    else // single / multiple PortfolioEntry s
                    {
                        // fetch coin ids corresponding to coin names / symbols
                        long[] coinIds = CoinListingManager.Instance.FetchCoinIds(commandArguments);

                        // get coin ids with initialized ticker data
                        foreach (int coinId in coinIds)
                        {
                            if (PortfolioManager.Instance.IsInPortfolio(coinId))
                            {
                                coinIdsWithPortfolioEntry.Add(coinId);
                            }
                            else
                            {
                                string coinName = CoinListingManager.Instance.GetCoinNameById(coinId);
                                coinNamesWithoutPortfolioEntry.Add(coinName);
                            }
                        }
                    }
                    if (coinIdsWithPortfolioEntry.Count == 0) // no PortfolioEntries to display
                    {
                        string noticeMessage = "No portfolio entries to display.";
                        ConsoleIOManager.Instance.LogNotice(
                            noticeMessage,
                            ConsoleIOManager.eOutputReportType.CommandExecution);
                    }
                    else if (coinIdsWithPortfolioEntry.Count == 1) // a single PortfolioEntry
                    {
                        // print PortfolioEntry's detailed data string
                        long           portfolioEntryCoinId = coinIdsWithPortfolioEntry[0];
                        PortfolioEntry portfolioEntry       =
                            PortfolioManager.Instance.GetPortfolioEntry(portfolioEntryCoinId);
                        ConsoleIOManager.Instance.PrintData(
                            portfolioEntry.GetDetailedString(),
                            ConsoleIOManager.eOutputReportType.CommandExecution);
                    }

                    if (coinIdsWithPortfolioEntry.Count > 1) // // multiple PortfolioEntries requested
                    {
                        // print coin PortfolioEntry display table containing portfolio entries corresponding
                        // to fetched coin ids
                        string portfolioEntryDisplayTableString =
                            PortfolioManager.Instance.GetPortfolioEntryDisplayTableString(
                                coinIdsWithPortfolioEntry.ToArray());
                        ConsoleIOManager.Instance.PrintData(
                            portfolioEntryDisplayTableString,
                            ConsoleIOManager.eOutputReportType.CommandExecution);
                    }

                    // if data for coin ids which don't have corresponding porfolio entries was requested,
                    // display an appropriate message to user
                    if (coinNamesWithoutPortfolioEntry.Count > 0)
                    {
                        string noticeMessage = StringUtils.Append(
                            "Following coin(s) were not in portfolio: ",
                            ", ",
                            coinNamesWithoutPortfolioEntry.ToArray())
                                               + ".";
                        ConsoleIOManager.Instance.LogNotice(
                            noticeMessage,
                            ConsoleIOManager.eOutputReportType.CommandExecution);
                    }

                    commandExecutedSuccessfuly = true;
                }
                // user specified coin names / symbols which don't exist in CoinListingManager
                catch (CoinListingManager.CoinNameOrSymbolNotFoundException coinNameOrSymbolNotFoundException)
                {
                    ConsoleIOManager.Instance.LogError(
                        coinNameOrSymbolNotFoundException.Message,
                        ConsoleIOManager.eOutputReportType.CommandExecution);
                    commandExecutedSuccessfuly = false;
                }
                catch (DatabaseCommunicationException databaseCommunicationException)
                {
                    PortfolioCommand.HandleDatabaseCommunicationException(databaseCommunicationException);
                    commandExecutedSuccessfuly = false;
                }

                return(commandExecutedSuccessfuly);
            }
Пример #4
0
            /// <summary>
            /// sells coin corresponding to name / symbol specified in <paramref name="commandArguments"/>[0],
            /// where sell amount is specified in <paramref name="commandArguments"/>[1]
            /// and sell price per coin is specified in <paramref name="commandArguments"/>[2].
            /// returns whether command was executed successfully.
            /// </summary>
            /// <seealso cref="CoinListingManager.GetCoinIdByNameOrSymbol(string)"/>
            /// <seealso cref="PortfolioManager.GetCoinHoldings(int)"/>
            /// <seealso cref="PortfolioManager.SellCoin(int, double, double, long)"/>
            /// <param name="commandArguments"></param>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                try
                {
                    // price coin name or symbol from command argument 0
                    string coinNameOrSymbol = commandArguments[0];

                    // get coin id by name or symbol
                    long coinId = CoinListingManager.Instance.GetCoinIdByNameOrSymbol(coinNameOrSymbol);

                    // get coin name & symbol
                    string coinName   = CoinListingManager.Instance.GetCoinNameById(coinId);
                    string coinSymbol = CoinListingManager.Instance.GetCoinSymbolById(coinId);

                    // current timestamp
                    long unixTimestamp = DateTimeUtils.GetUnixTimestamp();

                    // parse buy transactions
                    SellTransaction[] sellTransactions = tryParseTransactionArray(
                        commandArguments,
                        coinId,
                        unixTimestamp,
                        out bool sellTransactionArrayParseSuccess);

                    if (sellTransactionArrayParseSuccess)
                    {
                        // check if there are enough funds for sell transactions
                        PortfolioEntry portfolioEntry = PortfolioManager.Instance.GetPortfolioEntry(coinId);
                        bool           sufficientFundsForSellTransactions = sufficientFundsForTransactions(
                            portfolioEntry,
                            sellTransactions,
                            out string lackingExchangeName);

                        if (!sufficientFundsForSellTransactions) // not enough funds to perform sales
                        {
                            double lackingExchangeCoinHoldings = portfolioEntry.GetCoinHoldings(
                                lackingExchangeName);

                            throw new InsufficientFundsForSellTransactionsException(
                                      coinId,
                                      lackingExchangeName,
                                      lackingExchangeCoinHoldings);
                        }

                        // execute sell transactions
                        PortfolioManager.Instance.SellCoin(sellTransactions);

                        // sale(s) performed successfully
                        string successfulSaleNoticeMessage = sellTransactions.Length == 1
                            ? string.Format(
                            "Successfully sold {0} '{1}' from exchange '{2}' for {3}$ each.",
                            sellTransactions[0].Amount,
                            coinName,
                            sellTransactions[0].ExchangeName,
                            sellTransactions[0].PricePerCoin)
                            : string.Format(
                            "{0} Specified sales made successfully.",
                            sellTransactions.Length);

                        ConsoleIOManager.Instance.LogNotice(
                            successfulSaleNoticeMessage,
                            ConsoleIOManager.eOutputReportType.CommandExecution);

                        commandExecutedSuccessfuly = true;
                    }
                    else // !sellTransactionArrayParseSuccess
                    {
                        commandExecutedSuccessfuly = false;
                    }
                }
                catch (CoinNameOrSymbolNotFoundException coinNameOrSymbolNotFoundException)
                {
                    // coin with specified name / symbol not found in listing repository
                    ConsoleIOManager.Instance.LogError(
                        coinNameOrSymbolNotFoundException.Message,
                        ConsoleIOManager.eOutputReportType.CommandExecution);

                    commandExecutedSuccessfuly = false;
                }
                catch (CoinNotInPortfolioException coinNotInPortfolioException)
                {
                    // portfolio has no entry with specified id
                    string coinName = CoinListingManager.Instance.GetCoinNameById(
                        coinNotInPortfolioException.CoinId);

                    ConsoleIOManager.Instance.LogErrorFormat(
                        false,
                        ConsoleIOManager.eOutputReportType.CommandExecution,
                        "There's no entry in portfolio manager for '{0}'.",
                        coinName);

                    commandExecutedSuccessfuly = false;
                }
                catch (InsufficientFundsForSellTransactionsException
                       insufficientFundsForSellTransactionsException)
                {
                    string coinName = CoinListingManager.Instance.GetCoinNameById(
                        insufficientFundsForSellTransactionsException.CoinId);
                    string coinSymbol = CoinListingManager.Instance.GetCoinSymbolById(
                        insufficientFundsForSellTransactionsException.CoinId);

                    ConsoleIOManager.Instance.LogErrorFormat(
                        false,
                        ConsoleIOManager.eOutputReportType.CommandExecution,
                        "Not enough funds for requested sell operation(s)." +
                        " '{0}' holdings in exchange '{1}': {2} {3}.",
                        coinName,
                        insufficientFundsForSellTransactionsException.ExchangeName,
                        insufficientFundsForSellTransactionsException.ExchangeCoinHoldings,
                        coinSymbol);

                    commandExecutedSuccessfuly = false;
                }
                catch (DatabaseCommunicationException databaseCommunicationException)
                {
                    PortfolioCommand.HandleDatabaseCommunicationException(databaseCommunicationException);
                    commandExecutedSuccessfuly = false;
                }

                return(commandExecutedSuccessfuly);
            }
Пример #5
0
            /// <summary>
            /// buys coin corresponding to name / symbol specified in <paramref name="commandArguments"/>[0],
            /// where buy amount is specified in <paramref name="commandArguments"/>[1]
            /// and buy price per coin is specified in <paramref name="commandArguments"/>[2].
            /// returns whether command was executed successfully.
            /// </summary>
            /// <seealso cref="CoinListingManager.GetCoinIdByNameOrSymbol(string)"/>
            /// <seealso cref="ConsoleIOManager.ShowConfirmationDialog(string)"/>
            /// <seealso cref="PortfolioManager.BuyCoin(int, double, double, long)"/>
            /// <param name="commandArguments"></param>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                try
                {
                    // price coin name or symbol from command argument 0
                    string coinNameOrSymbol = commandArguments[0];

                    // get coin id by name or symbol
                    long coinId = CoinListingManager.Instance.GetCoinIdByNameOrSymbol(coinNameOrSymbol);

                    // get coin name
                    string coinName = CoinListingManager.Instance.GetCoinNameById(coinId);

                    // current timestamp
                    long unixTimestamp = DateTimeUtils.GetUnixTimestamp();

                    // parse buy transactions
                    BuyTransaction[] buyTransactions = tryParseTransactionArray(
                        commandArguments,
                        coinId,
                        unixTimestamp,
                        out bool buyTransactionsParseSuccess);

                    bool createNewPortfolioEntry = false;

                    if (buyTransactionsParseSuccess)
                    {
                        bool executeBuyTransactions = false;

                        // check if portfolio has an entry with specified id
                        if (!PortfolioManager.Instance.IsInPortfolio(coinId))
                        {
                            // portfolio has no entry with specified id
                            ConsoleIOManager.Instance.LogErrorFormat(
                                false,
                                ConsoleIOManager.eOutputReportType.CommandExecution,
                                "There's no entry in portfolio manager for '{0}'.",
                                coinName);

                            // ask user if they want to create a new portfolio entry
                            string promptMessage = "Create new entry?";
                            createNewPortfolioEntry =
                                ConsoleIOManager.Instance.ShowConfirmationDialog(
                                    promptMessage,
                                    ConsoleIOManager.eOutputReportType.CommandExecution);

                            if (createNewPortfolioEntry) // user chose to create a new portfolio entry
                            {
                                executeBuyTransactions = true;
                            }
                            else // user chose not to create a new portfolio entry
                            {
                                ConsoleIOManager.Instance.LogNotice(
                                    "Purchase cancelled.",
                                    ConsoleIOManager.eOutputReportType.CommandExecution);
                                executeBuyTransactions = false;
                            }
                        }
                        else // portfolio already has an entry with specified id
                        {
                            executeBuyTransactions = true;
                        }

                        if (executeBuyTransactions)
                        {
                            if (createNewPortfolioEntry) // perform coin (add + buy) action
                            {
                                PortfolioManager.Instance.AddAndBuyCoin(coinId, buyTransactions);

                                // log coin add notice
                                ConsoleIOManager.Instance.LogNoticeFormat(
                                    false,
                                    ConsoleIOManager.eOutputReportType.CommandExecution,
                                    "'{0}' successfully added to portfolio.",
                                    coinName);
                            }
                            else // perform coin buy action
                            {
                                PortfolioManager.Instance.BuyCoin(buyTransactions);
                            }

                            // purchase performed successfully
                            string successfulPurchaseNoticeMessage = buyTransactions.Length == 1
                                ? string.Format(
                                "Successfully purchased {0} {1} for {2}$ each, stored in exchange " +
                                "'{3}'.",
                                buyTransactions[0].Amount,
                                coinName,
                                buyTransactions[0].PricePerCoin,
                                buyTransactions[0].ExchangeName)
                                : string.Format(
                                "{0} Specified purchases made successfully.",
                                buyTransactions.Length);

                            ConsoleIOManager.Instance.LogNotice(
                                successfulPurchaseNoticeMessage,
                                ConsoleIOManager.eOutputReportType.CommandExecution);

                            commandExecutedSuccessfuly = true;
                        }
                        else // !executeBuyTransactions
                        {
                            commandExecutedSuccessfuly = false;
                        }
                    }
                    else // buy transaction parse not successful
                    {
                        commandExecutedSuccessfuly = false;
                    }
                }
                catch (CoinNameOrSymbolNotFoundException coinNameOrSymbolNotFoundException)
                {
                    // coin with specified name / symbol not found in listing repository
                    ConsoleIOManager.Instance.LogError(
                        coinNameOrSymbolNotFoundException.Message,
                        ConsoleIOManager.eOutputReportType.CommandExecution);

                    commandExecutedSuccessfuly = false;
                }
                catch (DatabaseCommunicationException databaseCommunicationException)
                {
                    PortfolioCommand.HandleDatabaseCommunicationException(databaseCommunicationException);
                    commandExecutedSuccessfuly = false;
                }

                return(commandExecutedSuccessfuly);
            }