/// <summary>
        /// Views the customer.
        /// </summary>
        public static void ViewCustomer()
        {
            CustomerData          customer2 = new CustomerData();
            IList <CustomerModel> list      = customer2.GetAllCustomers();

            foreach (var items in list)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation);
            }
        }
        /// <summary>
        /// Customers the data.
        /// </summary>
        public void CustomerData()
        {
            ////customer data in file
            CustomerData          customerData1 = new CustomerData();
            IList <CustomerModel> values        = customerData1.GetAllCustomer();

            foreach (var items in values)
            {
                Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation);
            }
        }
        /// <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");
        }
Esempio n. 5
0
        /// <summary>
        /// Data processing.
        /// </summary>
        /// <exception cref="Exception">system exceptions </exception>
        public void DataProcesssing()
        {
            try
            {
                int caseCondition;
                ////this variable is used for checking the condition of do
                string         doCondition    = null;
                DataProcessing dataProcessing = new DataProcessing();
                do
                {
                    Console.WriteLine("enter 1 for adding customer");
                    Console.WriteLine("enter 2 for adding stock");
                    Console.WriteLine("enter 3 for buying stock");
                    Console.WriteLine("enter 4 for selling stock");
                    Console.WriteLine("enter 5 for view customers");
                    Console.WriteLine("enter 6 for view stock");
                    Console.WriteLine("enter 7 for view transaction");
                    Console.WriteLine("enter 8 for removing the stock ");
                    caseCondition = Convert.ToInt32(Console.ReadLine());
                    switch (caseCondition)
                    {
                    case 1:
                        ////this case is used for adding customer
                        CustomerData customerData = new CustomerData();
                        customerData.AddCustomer();
                        break;

                    case 2:
                        ////this case is used for adding the stock
                        StockData stockData = new StockData();
                        stockData.AddStock();
                        break;

                    case 3:
                        ////this case is used for buying the stock
                        Transaction transaction = new Transaction();
                        transaction.BuyStock();
                        break;

                    case 4:
                        ////this case is used for selling the stock
                        Transaction transaction1 = new Transaction();
                        transaction1.SellStock();
                        break;

                    case 5:
                        ////this case is used for getting the all customer data
                        dataProcessing.CustomerData();

                        break;

                    case 6:
                        ////this case is used for getting all the records
                        dataProcessing.StockData();

                        break;

                    case 7:
                        ////this case is used for getting all the Transactions
                        dataProcessing.TransactionData();

                        break;

                    case 8:
                        ////this case is used for removing the stock
                        RemovingStock removingStock = new RemovingStock();
                        removingStock.RemoveStock();
                        break;
                    }

                    Console.WriteLine("enter y to continue");
                    doCondition = Console.ReadLine();
                }while (doCondition.Equals("y"));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// Addings the customer.
        /// </summary>
        public static void AddingCustomer()
        {
            CustomerData customer1 = new CustomerData();

            customer1.AddCustomer();
        }
        /// <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);
            }
        }