示例#1
0
 /// <summary>
 /// Adds the stock.
 /// </summary>
 public void AddStock()
 {
     try
     {
         IList <StockDataModel> stock = new List <StockDataModel>();
         Console.WriteLine("enter id of stock");
         int id = Convert.ToInt32(Console.ReadLine());
         Console.WriteLine("enter name of stock");
         string name = Console.ReadLine();
         Console.WriteLine("enter the number of shares");
         int numberOfStocks = Convert.ToInt32(Console.ReadLine());
         Console.WriteLine("enter price per share");
         int pricePerShare = Convert.ToInt32(Console.ReadLine());
         ////creating the object of StockDataModel class
         StockDataModel stockDataModel = new StockDataModel();
         {
             stockDataModel.Id             = id;
             stockDataModel.Name           = name;
             stockDataModel.NumberOfShares = numberOfStocks;
             stockDataModel.PricePerShare  = pricePerShare;
             ////this is used for reading the file
             using (StreamReader stream = new StreamReader(this.constants.StockFile))
             {
                 string json = stream.ReadToEnd();
                 stream.Close();
                 stock = JsonConvert.DeserializeObject <List <StockDataModel> >(json);
                 stock.Add(stockDataModel);
                 ////searializeing the object
                 var convertedJson = JsonConvert.SerializeObject(stock);
                 File.WriteAllText(this.constants.StockFile, convertedJson);
                 Console.WriteLine("new stock added");
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
        /// <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");
        }
        /// <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");
        }