Exemplo n.º 1
0
        public void ChargeRate_Create_Success()
        {
            var chargeRate = new ChargeRate(10.04m, 5m);

            Assert.AreEqual(10.04m, chargeRate.TaxRate);
            Assert.AreEqual(5m, chargeRate.ImportDutyRate);
        }
Exemplo n.º 2
0
        public void Scenario_Two_Success()
        {
            // Arrange
            var listOfProducts = new List <IProduct>
            {
                new ImportedProduct("box of chocolates", 10.00m, 1, ProductType.Food),
                new ImportedProduct("bottle of perfume", 47.50m, 1, ProductType.Perfume)
            };
            var chargeRate = new ChargeRate(10, 5);

            // Act
            listOfProducts = TaxCalculationLogic.CalculateTax(listOfProducts, chargeRate).ToList();

            // Assert
            Assert.AreEqual(10.50m, listOfProducts[0].TotalPriceIncTax);
            Assert.AreEqual(54.63m, listOfProducts[1].TotalPriceIncTax);
            Assert.AreEqual(7.63m, listOfProducts.Sum(product => product.ProductTax));
            Assert.AreEqual(65.13m, listOfProducts.Sum(product => product.TotalPriceIncTax));
        }
Exemplo n.º 3
0
        public void Receipt_GetSubtotalIncTax_Success()
        {
            // Arrange
            var listOfProduct = new List <IProduct>
            {
                new Product("vitamin C 30 Tablets", 13.82m, 1, ProductType.MedicalProduct),
                new Product("D&G Blue", 42.99m, 1, ProductType.Perfume),
                new ImportedProduct("Box of apples", 3.56m, 3, ProductType.Food),
                new ImportedProduct("3D Puzzle", 10m, 2, ProductType.None)
            };
            var chargeRate = new ChargeRate(10m, 5m);

            // Act
            listOfProduct = TaxCalculationLogic.CalculateTax(listOfProduct, chargeRate).ToList();
            var receipt        = new Receipt(listOfProduct);
            var subtotalIncTax = receipt.SubtotalIncTax;

            // Assert
            Assert.AreEqual(95.33m, subtotalIncTax);
        }
Exemplo n.º 4
0
        public void Scenario_One_Success()
        {
            // Arrange
            var listOfProducts = new List <IProduct>
            {
                new Product("book", 12.49m, 1, ProductType.Book),
                new Product("music CD", 14.99m, 1, ProductType.None),
                new Product("chocolate bar", 0.85m, 1, ProductType.Food)
            };
            var chargeRate = new ChargeRate(10, 5);

            // Act
            listOfProducts = TaxCalculationLogic.CalculateTax(listOfProducts, chargeRate).ToList();

            // Assert
            Assert.AreEqual(12.49m, listOfProducts[0].TotalPriceIncTax);
            Assert.AreEqual(16.49m, listOfProducts[1].TotalPriceIncTax);
            Assert.AreEqual(0.85m, listOfProducts[2].TotalPriceIncTax);
            Assert.AreEqual(1.50m, listOfProducts.Sum(product => product.ProductTax));
            Assert.AreEqual(29.83m, listOfProducts.Sum(product => product.TotalPriceIncTax));
        }
Exemplo n.º 5
0
        private static ChargeRate CreatechargeRate()
        {
            bool       chargeRateCreated = false;
            ChargeRate chargeRate        = null;

            while (chargeRateCreated == false)
            {
                try
                {
                    Console.Write("Enter your basic tax rate (%): ");

                    int basicchargeRate = int.Parse(Console.ReadLine());


                    Console.Write("Please enter your imported tax rate (%): ");

                    int importedchargeRate = int.Parse(Console.ReadLine());


                    chargeRate = new ChargeRate(basicchargeRate, importedchargeRate);

                    chargeRateCreated = true;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (FormatException)
                {
                    Console.WriteLine("--- WARNING --- Invalid value format for the tax rate!\n");
                }
                catch (Exception)
                {
                    Console.WriteLine("--- WARNING --- Something else went wrong!!!\n");
                }
            }

            return(chargeRate);
        }
Exemplo n.º 6
0
        public void Scenario_Three_Success()
        {
            // Arrange
            var listOfProducts = new List <IProduct>
            {
                new ImportedProduct("bottle of perfume", 27.99m, 1, ProductType.Perfume),
                new Product("bottle of perfume", 18.99m, 1, ProductType.Perfume),
                new Product("packet of headache pills", 9.75m, 1, ProductType.MedicalProduct),
                new ImportedProduct("box of chocolates", 11.25m, 1, ProductType.Food)
            };
            var chargeRate = new ChargeRate(10, 5);

            // Act
            TaxCalculationLogic.CalculateTax(listOfProducts, chargeRate);

            // Assert
            Assert.AreEqual(32.19m, listOfProducts[0].TotalPriceIncTax);
            Assert.AreEqual(20.89m, listOfProducts[1].TotalPriceIncTax);
            Assert.AreEqual(9.75m, listOfProducts[2].TotalPriceIncTax);
            Assert.AreEqual(11.81m, listOfProducts[3].TotalPriceIncTax);
            Assert.AreEqual(6.66m, listOfProducts.Sum(product => product.ProductTax));
            Assert.AreEqual(74.64m, listOfProducts.Sum(product => product.TotalPriceIncTax));
        }
        public void TaxCalculation_RecalculateForAllProduct_Realculated()
        {
            // Arrange
            var listOfProduct = new List <IProduct>
            {
                new Product("vitamin C 30 Tablets", 13.82m, 1, ProductType.MedicalProduct),
                new Product("D&G Blue", 42.99m, 1, ProductType.Perfume),
                new ImportedProduct("Box of apples", 3.56m, 3, ProductType.Food),
                new ImportedProduct("3D Puzzle", 10m, 2, ProductType.None)
            };
            var chargeRate = new ChargeRate(12.06m, 5.2m);

            // Act
            listOfProduct[1].ProductTax = 3.5m;
            listOfProduct[2].ProductTax = 5.5m;
            var listOfProductWithTax = TaxCalculationLogic.CalculateTax(listOfProduct, chargeRate, recalculateAll: true);

            // Assert
            Assert.AreEqual(4, listOfProductWithTax.Count);
            Assert.AreEqual(0, listOfProductWithTax[0].ProductTax);
            Assert.AreEqual(5.18m, listOfProductWithTax[1].ProductTax);
            Assert.AreEqual(0.19m, listOfProductWithTax[2].ProductTax);
            Assert.AreEqual(1.73m, listOfProductWithTax[3].ProductTax);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //Create tax rate based on the user input
            ChargeRate chargeRate = CreatechargeRate();

            if (chargeRate != null)
            {
                try
                {
                    IList <IProduct> listOfProducts = AddProductsBasedOnUserInput(chargeRate);

                    PrintReceipt.PrintReceiptWithCalculatedTax(listOfProducts, chargeRate);
                }

                catch (Exception)
                {
                    Console.WriteLine("--- WARNING --- Something went wrong!!!\n");

                    //TODO: Error handling for: product line input.
                }
            }

            Console.ReadKey();
        }
Exemplo n.º 9
0
        private static List <IProduct> AddProductsBasedOnUserInput(ChargeRate chargeRate)
        {
            List <IProduct> listOfProducts = new List <IProduct>();

            bool          importedProduct    = false;
            decimal       productPrice       = 0;
            int           productQuantity    = 0;
            List <string> productDescription = new List <string>();

            bool productsAdded = false;

            while (!productsAdded)
            {
                Console.Write("Enter product line or (-1 to print): ");
                string productLine = Console.ReadLine();
                if (productLine.Equals(TriggerReceiptCreation))
                {
                    productsAdded = true;
                }
                else
                {
                    if (string.IsNullOrEmpty(productLine))
                    {
                        throw new ArgumentNullException();
                    }
                    else
                    {
                        string[] words = productLine.Split(' ');

                        //set the quantity
                        if (int.TryParse(words[0], out productQuantity))
                        {
                            //if quantity set, loop from second word
                            for (int i = 1; i < words.Length; i++)
                            {
                                //determine if the product line includes "imported"
                                if (words[i].Equals(ImportedProductIdentifier))
                                {
                                    importedProduct = true;
                                }
                                else
                                {
                                    //identify product price
                                    if (words[i].Equals(PriceIdentifier))
                                    {
                                        decimal.TryParse(words[i + 1], out productPrice);
                                        break;
                                    }
                                    else
                                    {
                                        //add description
                                        productDescription.Add(words[i]);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //if quantity not specified set as default to 1
                            productQuantity = 1;
                            for (int i = 0; i < words.Length; i++)
                            {
                                //determine if the product line includes "imported"
                                if (words[i].Equals(ImportedProductIdentifier))
                                {
                                    importedProduct = true;
                                }
                                else
                                {
                                    //determine if the product line string include "imported"
                                    if (words[i].Equals(PriceIdentifier))
                                    {
                                        decimal.TryParse(words[i + 1], out productPrice);
                                        break;
                                    }
                                    else
                                    {
                                        //add description
                                        productDescription.Add(words[i]);
                                    }
                                }
                            }
                        }
                    }

                    //ask user for the product type
                    int productType = AskUserToSetProductType();

                    if (productType >= 0)
                    {
                        IProduct product;

                        if (importedProduct)
                        {
                            product = new ImportedProduct(string.Join(" ", productDescription), productPrice, productQuantity, (ProductType)productType);
                        }
                        else
                        {
                            product = new Product(string.Join(" ", productDescription), productPrice, productQuantity, (ProductType)productType);
                        }
                        listOfProducts.Add(product);

                        //reset product fiels
                        importedProduct = false;
                        productPrice    = 0;
                        productQuantity = 0;
                        productDescription.Clear();
                    }
                }
            }

            return(listOfProducts);
        }
Exemplo n.º 10
0
 public void ChargeRate_SetNegativeImportDutyRate_Fail()
 {
     var chargeRate = new ChargeRate(14.2m, -34.7m);
 }
Exemplo n.º 11
0
 public void ChargeRate_SetImportDutyRateToZero_Fail()
 {
     var chargeRate = new ChargeRate(12m, 0);
 }
Exemplo n.º 12
0
 public void ChargeRate_SetNegativeTaxRate_Fail()
 {
     var chargeRate = new ChargeRate(-3.2m, 5m);
 }
Exemplo n.º 13
0
 public void ChargeRate_SetTaxRateToZero_Fail()
 {
     var chargeRate = new ChargeRate(0, 5m);
 }