/// <summary>
        /// It Added All the Share Purchased in parts.
        /// </summary>
        /// <param name="customerPurchaseds"></param>
        /// <returns></returns>
        public static List <CustomerPurchased> AllCustomerPurchasedShare(List <CustomerPurchased> customerPurchaseds)
        {
            try
            {
                List <CustomerPurchased> customerPurchaseds1 = new List <CustomerPurchased>();
                List <CustomerPurchased> customerPurchaseds2;
                CustomerPurchased        customerPurchased1;
                int    n = StockAccount.ListOfCompanyShares().Count;
                string shareName = "";
                double specificShare = 0.0, amount = 0.0;

                if (customerPurchaseds.Count == 0)
                {
                    return(null);
                }
                else
                {
                    for (int i = 0; i < n; i++)
                    {
                        shareName           = StockAccount.ListOfCompanyShares()[i].ShareName;
                        customerPurchaseds2 = customerPurchaseds.FindAll(x => x.ShareName.Equals(shareName));
                        if (customerPurchaseds2.Count != 0)
                        {
                            foreach (CustomerPurchased customerPurchased in customerPurchaseds2)
                            {
                                specificShare += Convert.ToDouble(customerPurchased.NoOfShare);
                                amount        += Convert.ToDouble(customerPurchased.Amount);
                            }
                            customerPurchased1 = new CustomerPurchased
                            {
                                ShareName = shareName,
                                NoOfShare = specificShare.ToString(),
                                Amount    = amount.ToString()
                            };
                            customerPurchaseds1.Add(customerPurchased1);
                            specificShare = 0.0;
                            amount        = 0.0;
                        }
                    }
                    return(customerPurchaseds1);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}", e.Message);
                return(null);
            }
        }
Пример #2
0
        public void Buy(int amount, string symbol)
        {
            try
            {
                float  customerPurchasedShare;
                string TransactionId, userName;

                Stock stockPortfolio = Utility.SingleStockData(symbol);
                float sharePrice     = (float)Convert.ToDouble(stockPortfolio.SharePrice);
                float noOfShare      = (float)Convert.ToDouble(stockPortfolio.NoOfShares);

                if (stockPortfolio == null)
                {
                    Console.WriteLine("This Share is not present.");
                    return;
                }

                customerPurchasedShare = (float)amount / sharePrice;

                if (noOfShare < customerPurchasedShare)
                {
                    Console.WriteLine("We Do not Posses this much Share.");
                    return;
                }

                Console.WriteLine();
                Console.Write("Are You Sure, Do you want to Buy {0} Share for Rs.{1} [y/n]: ", symbol, amount);

                if (Console.ReadLine().ToLower()[0] == 'n')
                {
                    return;
                }
                else
                {
                    TransactionId = Guid.NewGuid().ToString();
                    userName      = Utility.UserName;

                    stockPortfolio.NoOfShares = (noOfShare - customerPurchasedShare).ToString();

                    List <Stock> stockPortfolios1 = Utility.ReadStockData(stockPortfolio);

                    StockList stock = new StockList()
                    {
                        Stocks = stockPortfolios1
                    };

                    Utility.UpdateStockDataToJson(stock);

                    List <CustomerPurchased> customerPurchasedLists = Utility.ReadCustomerPurchasedLists();

                    CustomerPurchased customerPurchased = new CustomerPurchased
                    {
                        Transaction_Id = TransactionId,
                        UserName       = userName,
                        ShareName      = symbol,
                        NoOfShare      = customerPurchasedShare.ToString(),
                        Amount         = amount.ToString(),
                        DateAndTime    = DateTime.Now.ToString()
                    };

                    customerPurchasedLists.Add(customerPurchased);

                    CustomerPurchasedList customerPurchasedList = new CustomerPurchasedList
                    {
                        CustomerPurchasedlists = customerPurchasedLists
                    };

                    Utility.AddCustomerPurchasedShareToJson(customerPurchasedList);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}", e.Message);
            }
        }