Ejemplo n.º 1
0
        /// <summary>
        /// Make the ReadFromTransactionfileToListOfTransactios more readable
        /// </summary>
        public static void MakeBuyTransaction(List <AddProductToTransaction> listOfTransactions, ListOfProducts listOfProducts, String[] transactionDetails)
        {
            //I declare the variables here, to make sure they do not contain information from last line
            //The product in transaction ID
            int productInTransactionID;
            //The product
            Product product;
            //The date of the transaction
            DateTime transactiondate;
            //The price of the transaction
            decimal price;
            //The discount given to the product
            decimal discountAmount;
            //The product description
            string comment;
            //The amount of products bought
            int amountOfProducts;
            //The name of the product in case something should go wrong with the system
            string nameOfProduct;
            //The amount of product
            decimal amountOFUnitInProduct;
            //The transaction ID
            int transactionID;

            try
            {
                //The transaction ID, is used to identify the transaction later on
                productInTransactionID = int.Parse(transactionDetails[productInTransactionIDPlacementProductInTransaction]);
            }
            catch (FormatException)
            {
                throw new FormatException($"The format of the product in transaction ID {transactionDetails[productInTransactionIDPlacementProductInTransaction]} is invalid. ");
            }
            try
            {
                //The product ID, is used to find the relevant product
                uint productID = UInt32.Parse(transactionDetails[productIDPlacementProductInTransaction]);
                product = listOfProducts.FindProductByID(productID);
                if (product == null)
                {
                    throw new ElementDoesNotExistException($"Product with ID {productID} does not excist");
                }
            }
            catch (FormatException)
            {
                throw new FormatException($"The format of the prodcut ID {transactionDetails[productIDPlacementProductInTransaction]} is invalid");
            }
            catch (ElementDoesNotExistException)
            {
                throw;
            }
            try
            {
                //The date of the transaction
                transactiondate = ParseStringToType.StringToDateTime(transactionDetails[datePlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The transactiondate {transactionDetails[datePlacementProductInTransaction]} is not valid");
            }
            try
            {
                //The fourth is the price
                price = decimal.Parse(transactionDetails[amountOfMoneyPlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The string {transactionDetails[amountOfMoneyPlacementProductInTransaction]} is not a valid decimal number, and can therefore not represent a price");
            }
            try
            {
                //The fifth is the discount amount
                discountAmount = decimal.Parse(transactionDetails[discountAmountPlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The string {transactionDetails[discountAmountPlacementProductInTransaction]} is not a valid decimal, and can not represent the discount amount");
            }
            try
            {
                //The sixth one is the amount of products bought in this transaction
                amountOfProducts = int.Parse(transactionDetails[amountOfProductsBoughtPlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The string {transactionDetails[amountOfProductsBoughtPlacementProductInTransaction]} is not a valid int, and can not be an amount of products");
            }
            try
            {
                //The name of the product in case something should go wrong with the ID system
                nameOfProduct = transactionDetails[productNamePlacementProductInTransaction];
                //The optional comment when purchace is made
                comment = transactionDetails[commentPlacementProductInTransaction];
            }
            catch
            {
                throw new FormatException("This would make no scense, but it is with the comment it is wrong");
            }
            try
            {
                transactionID = int.Parse(transactionDetails[transactionIDPlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The string {transactionDetails[transactionIDPlacementProductInTransaction]} is not a valid uint and can therefore not be a transaction ID.");
            }
            try
            {
                amountOFUnitInProduct = decimal.Parse(transactionDetails[amountOfUnitPlacementProductInTransaction]);
            }
            catch
            {
                throw new FormatException($"The string {transactionDetails[amountOfUnitPlacementProductInTransaction]} is not a valid decimal, and can therefore not be a unit amount");
            }
            try
            {
                //Here I add the transaction to the list
                //   listOfTransactions.Add(new AddProductToBuyTransaction(price, product, discountAmount, productInTransactionID, transactiondate, amountOfProducts, comment, nameOfProduct, amountOFUnitInProduct, transactionID));
            }
            catch
            {
                throw new FormatException("The product was not possible to make");
            }
        }