예제 #1
0
        public SqlitefUI(String productTableName, string addProductToMainTransactionTableName, string addProductToTransactionTableName, string databaseLocation)
        {
            sqlite = new SQLiteConnection(databaseLocation);
            DataTable TableWithProducts = InteractWithDatabase.SelectQuery($"Select * From {productTableName}", sqlite);

            this.productTableName = productTableName;
            this.addProductToTransactionTableName = addProductToTransactionTableName;
            this.addProductToTransactionTableName = mainBuyTransactionTableName;
            productList     = new ListOfProducts(InteractWithDatabase.ProductTableToListOfProducts(TableWithProducts), this);
            transactionList = new ListOfTransactions(null);
        }
예제 #2
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");
            }
        }
예제 #3
0
        /// <summary>
        /// Takes the file with transactions and reads them in to a list of transactions to use at runtime
        /// </summary>
        /// <returns></returns>
        public static List <AddProductToTransaction> ReadFromTransactionFileToListOfTransactions(string path, ListOfProducts listOfProducts, string DiscardedFile)
        {
            //The list of transactions to be send as an argument in the constructor of the returned ListOfTransactions
            List <AddProductToTransaction> listOfTransactions = new List <AddProductToTransaction>();

            //The number of informationslots in te list

            //Read the text from the files into an array of strings, so every transaction has a entrance
            string[] lines = System.IO.File.ReadAllLines(path);
            //Makes sure the first describing line is not translated.
            bool firstIsDone = false;

            foreach (string line in lines)
            {
                //TODO better security
                if (firstIsDone)
                {
                    try
                    {
                        string[] transactionDetails = line.Split(';');
                        if (transactionDetails.Length == numberOfInformationslots)
                        {
                            if (transactionDetails[transactionTypePlacementProductInTransaction] == "Buy")
                            {
                                MakeBuyTransaction(listOfTransactions, listOfProducts, transactionDetails);
                            }
                        }
                        else
                        {
                            //Makes sure the line is added to a list of broken transactions
                            throw new FormatException($"The number of segments in the line is wrong. It should be {numberOfInformationslots} but it is {transactionDetails.Length} \n The line is {line}");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new FormatException($"The string {line} is not a valid transaction. The (first) thing wrong is {ex}");
                    }
                }
                else
                {
                    //Shows that the first is done, so the code will go into the if statement.
                    firstIsDone = true;
                }
            }

            return(listOfTransactions);
        }