/// <summary>
        /// Sells the stock.
        /// </summary>
        /// <exception cref="Exception">
        /// Invalid stock id
        /// No. of shares you mentioned are not available in stock or invalid input
        /// </exception>
        public void SellStock()
        {
            ////creating the object of customer class
            CustomerData          customer       = new CustomerData();
            IList <CustomerModel> customerModels = customer.GetAllCustomer();

            Console.WriteLine("id \t name \t valuation");
            ////this is used for reading the data in customerModels
            foreach (var items in customerModels)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation);
            }

            Console.WriteLine("Please enter the selling customer id");
            int customerId = Convert.ToInt32(Console.ReadLine());
            ////creating the object of stock data
            StockData stockData = new StockData();
            IList <StockDataModel> stockModels = stockData.GetStock();

            Console.WriteLine("id \tname \tnumberofshares \tpricepershare");
            foreach (var items in stockModels)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShares + "\t" + items.PricePerShare);
            }

            Console.WriteLine("Please enter the Stock Id to which stock you want to sell");
            int            stockId        = Convert.ToInt32(Console.ReadLine());
            StockDataModel stockDataModel = null;
            bool           stockFl        = true;

            ////this loop is used for printing the data in stock model
            foreach (var items in stockModels)
            {
                if (items.Id == stockId)
                {
                    stockDataModel = items;
                    Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShares + "\t" + items.PricePerShare);
                    stockFl = false;
                }
            }

            if (stockFl == true)
            {
                throw new Exception("Invalid stock id");
            }

            Console.WriteLine("Enter the number of shares need to sell");
            ////taking the input from the user
            int    numberOfShares  = Convert.ToInt32(Console.ReadLine());
            string customerName    = string.Empty;
            string stockName       = string.Empty;
            int    amountValuation = 0;

            if (numberOfShares > 0)
            {
                ////int priceOfShare = 0;
                bool stockFlag = true;
                ////this loop is used for searching the entered id
                foreach (var items in stockModels)
                {
                    ////this condition is used for checkint the data present in the stock
                    if (items.Id == stockId)
                    {
                        items.NumberOfShares = items.NumberOfShares + numberOfShares;
                        this.priceOfShare    = items.PricePerShare * numberOfShares;
                        stockName            = items.Name;
                        stockFlag            = false;
                    }
                }

                if (stockFlag == true)
                {
                    throw new Exception("Invalid stock id");
                }

                bool customerFlag = true;
                foreach (var item in customerModels)
                {
                    ////this condition is used for
                    if (item.Id == customerId)
                    {
                        item.Valuation  = item.Valuation + this.priceOfShare;
                        customerName    = item.Name;
                        amountValuation = item.Valuation;
                        customerFlag    = false;
                    }
                }

                if (customerFlag == true)
                {
                    throw new Exception("Invalid customer id");
                }
            }
            else
            {
                throw new Exception("No. of shares you mentioned are not available in stock or invalid input");
            }

            ////creating the object of transactionModel
            TransactionModel transactionModel = new TransactionModel()
            {
                CustomerName    = customerName,
                StockName       = stockName,
                NoOfShares      = numberOfShares,
                Amount          = this.priceOfShare * numberOfShares,
                Time            = DateTime.Now.ToString(),
                TransactionType = TransactionType.Sell
            };

            IList <TransactionModel> transactionModels = Transaction.GetAllTransactions();

            transactionModels.Add(transactionModel);
            Constants constants = new Constants();

            ////writing the stock in to the file
            Transaction.WriteFile(constants.StockFile, stockModels);
            ////writing the customer in to the file
            Transaction.WriteFile(constants.CustomerDetails, customerModels);
            ////writing the transaction in to file
            Transaction.WriteFile(constants.TransactionFile, transactionModels);
            Console.WriteLine("selling is successfull");
        }
        /// <summary>
        /// Buys the stock.
        /// </summary>
        /// <exception cref="Exception">
        /// Invalid stock id
        /// No. of shares you mentioned are not available in stock or invalid input
        /// </exception>
        public void BuyStock()
        {
            int priceOfShare = 0;
            ////creating the object of customer class
            CustomerData customer = new CustomerData();
            ////creating new list
            IList <CustomerModel> customerModels = customer.GetAllCustomer();

            Console.WriteLine("id \t name \t valuation");
            ////this loop is used for printing the elements in a list
            foreach (var items in customerModels)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation);
            }

            Console.WriteLine("Please enter the customer id");
            int customerId = Convert.ToInt32(Console.ReadLine());
            ////creating the object of  StockData class
            StockData stockData = new StockData();
            IList <StockDataModel> stockModels = stockData.GetStock();

            Console.WriteLine("id \tname \tnumberofshares \tpricepershare");
            ////this loop is used for printing the data in  StockData
            foreach (var items in stockModels)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShares + "\t" + items.PricePerShare);
            }

            Console.WriteLine("Please enter the  Stock Id to select stock for the customer");
            int            stockId        = Convert.ToInt32(Console.ReadLine());
            StockDataModel stockDataModel = null;
            bool           stockFl        = true;

            ////this loop is used for printing the data in  stockModels
            foreach (var items in stockModels)
            {
                if (items.Id == stockId)
                {
                    stockDataModel = items;
                    Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShares + "\t" + items.PricePerShare);
                    stockFl = false;
                }
            }

            ////this condition is used for checking the whether the stock present or not
            if (stockFl == true)
            {
                throw new Exception("Invalid stock id");
            }

            Console.WriteLine("Enter the number of shares need to purchase");
            int    numberOfShares  = Convert.ToInt32(Console.ReadLine());
            string customerName    = string.Empty;
            string stockName       = string.Empty;
            int    amountValuation = 0;

            ////this condition is used for checking whether user entered the valid number of shares
            if (numberOfShares < stockDataModel.NumberOfShares || numberOfShares <= 0)
            {
                ////int priceOfShare = 0;
                bool stockFlag = true;
                ////this loop is used for searching for the given stock id
                foreach (var items in stockModels)
                {
                    if (items.Id == stockId)
                    {
                        items.NumberOfShares = items.NumberOfShares - numberOfShares;
                        priceOfShare         = items.PricePerShare;
                        stockName            = items.Name;
                        stockFlag            = false;
                    }
                }

                ////this condition is used for checking the id exists
                if (stockFlag == true)
                {
                    throw new Exception("Invalid stock id");
                }

                bool customerFlag = true;
                ////this loop is used for searching for the given customer id
                foreach (var item in customerModels)
                {
                    if (item.Id == customerId)
                    {
                        item.Valuation  = item.Valuation - (priceOfShare * numberOfShares);
                        customerName    = item.Name;
                        amountValuation = item.Valuation;
                        customerFlag    = false;
                    }
                }

                ////this condition is used for checking the id exists
                if (customerFlag == true)
                {
                    throw new Exception("Invalid customer id");
                }
            }
            else
            {
                throw new Exception("No. of shares you mentioned are not available in stock or invalid input");
            }

            ////creating the object of transactionModel class
            TransactionModel transactionModel = new TransactionModel()
            {
                CustomerName    = customerName,
                StockName       = stockName,
                NoOfShares      = numberOfShares,
                Amount          = priceOfShare * numberOfShares,
                Time            = DateTime.Now.ToString(),
                TransactionType = TransactionType.Buy
            };

            IList <TransactionModel> transactionModels = Transaction.GetAllTransactions();

            transactionModels.Add(transactionModel);
            Constants constants = new Constants();

            Transaction.WriteFile(constants.StockFile, stockModels);
            Transaction.WriteFile(constants.CustomerDetails, customerModels);
            Transaction.WriteFile(constants.TransactionFile, transactionModels);
            Console.WriteLine("purchase sucessfull");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Buys the stock.
        /// </summary>
        /// <exception cref="Exception">
        /// Invalid stock id
        /// or
        /// Invalid stock id
        /// or
        /// Invalid customer id
        /// or
        /// No. of shares you mentioned are not available in stock or invalid input
        /// </exception>
        public void BuyStock()
        {
            int priceOfShare = 0;

            try
            {
                CustomerData          customer       = new CustomerData();
                IList <CustomerModel> customerModels = customer.GetAllCustomers();
                Console.WriteLine("id \t name \t valuation");
                foreach (var items in customerModels)
                {
                    Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation);
                }

                Console.WriteLine("Please enter the customer id");
                int    customerId = Convert.ToInt32(Console.ReadLine());
                Stock5 stockData  = new Stock5();
                IList <StockModel5> stockModels = stockData.GetStock();
                Console.WriteLine("id \tname \tnumberofshares \tpricepershare");
                foreach (var items in stockModels)
                {
                    Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShare + "\t" + items.PricePerShare);
                }

                Console.WriteLine("Please enter the  Stock Id to select stock for the customer");
                int         stockId        = Convert.ToInt32(Console.ReadLine());
                StockModel5 stockDataModel = null;
                bool        stockFl        = true;
                foreach (var items in stockModels)
                {
                    if (items.Id == stockId)
                    {
                        stockDataModel = items;
                        Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShare + "\t" + items.PricePerShare);
                        stockFl = false;
                    }
                }

                if (stockFl == true)
                {
                    throw new Exception("Invalid stock id");
                }

                Console.WriteLine("Enter the number of shares need to purchase");
                int    numberOfShares  = Convert.ToInt32(Console.ReadLine());
                string customerName    = string.Empty;
                string stockName       = string.Empty;
                int    amountValuation = 0;
                if (numberOfShares < stockDataModel.NumberOfShare || numberOfShares <= 0)
                {
                    priceOfShare = 0;
                    bool stockFlag = true;
                    foreach (var items in stockModels)
                    {
                        if (items.Id == stockId)
                        {
                            items.NumberOfShare = items.NumberOfShare - numberOfShares;
                            priceOfShare        = items.PricePerShare;
                            stockName           = items.Name;
                            stockFlag           = false;
                        }
                    }

                    if (stockFlag == true)
                    {
                        throw new Exception("Invalid stock id");
                    }

                    bool customerFlag = true;
                    foreach (var item in customerModels)
                    {
                        if (item.Id == customerId)
                        {
                            item.Valuation = item.Valuation - (priceOfShare * numberOfShares);
                            Console.WriteLine("Your cost for buying item is {0} ", priceOfShare * numberOfShares);
                            customerName    = item.Name;
                            amountValuation = item.Valuation;
                            customerFlag    = false;
                        }
                    }

                    if (customerFlag == true)
                    {
                        throw new Exception("Invalid customer id");
                    }
                }
                else
                {
                    throw new Exception("No. of shares you mentioned are not available in stock or invalid input");
                }

                TransactionModel transactionModel = new TransactionModel()
                {
                    CustomerName    = customerName,
                    StockName       = stockName,
                    NoOfShare       = numberOfShares,
                    Amount          = priceOfShare * numberOfShares,
                    Time            = DateTime.Now.ToString(),
                    TransactionType = TransactionType.Buy
                };
                IList <TransactionModel> transactionModels = Transaction.GetAllTransactions();
                transactionModels.Add(transactionModel);
                Constants constants = new Constants();
                Transaction.WriteFile(constants.StockFile, stockModels);
                Transaction.WriteFile(constants.CustomerData, customerModels);
                Transaction.WriteFile(constants.TransactionFile, transactionModels);
                Console.WriteLine("purchase sucessfull");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }