コード例 #1
0
        static void Main(string[] args)
        {
            PointOfSaleTerminal   terminal = new PointOfSaleTerminal();
            IEnumerable <Command> commands = new List <Command>
            {
                new _1_SetPricing(terminal),
                new _2_AddCustomProduct(terminal),
                new _3_AddCustomDiscount(terminal),
                new _4_ScanProduct(terminal),
                new _5_ShowTotalPrice(terminal),
                new _6_ClearCart(terminal)
            };

            Console.WriteLine("Welcome to Point-Of-Sale Terminal!");

            string commandCode = "";

            while (commandCode != "q")
            {
                Console.WriteLine("\nEnter one of the following commands or \"q\" to quit:");

                foreach (var command in commands)
                {
                    Console.WriteLine(command.Description);
                }

                commandCode = Console.ReadLine();

                commands.FirstOrDefault(c => c.CommandCode == commandCode)?.CommandAction();
            }
        }
コード例 #2
0
        public void Setup()
        {
            var productA = (new ArticleBuilder())
                           .SetProductCode("A")
                           .SetUnitPrice(1.25M)
                           .SetBulkPriceCondition(quantity: 3, price: 3.00M)
                           .BuildArticle();

            var productB = (new ArticleBuilder())
                           .SetProductCode("B")
                           .SetUnitPrice(4.25M)
                           .BuildArticle();

            var productC = (new ArticleBuilder())
                           .SetProductCode("C")
                           .SetUnitPrice(1M)
                           .SetBulkPriceCondition(quantity: 6, price: 5.00M)
                           .BuildArticle();

            var productD = (new ArticleBuilder())
                           .SetProductCode("D")
                           .SetUnitPrice(0.75M)
                           .BuildArticle();

            var dataSource = new SimpleArticleDataSource();

            dataSource.articles.Add(productA);
            dataSource.articles.Add(productB);
            dataSource.articles.Add(productC);
            dataSource.articles.Add(productD);

            terminal = new PointOfSaleTerminal(dataSource: dataSource);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Pdubnz/POSTSLibraryDemo
        static void Main()
        {
            // ABCDABA Verify that the total price is $13.25
            // CCCCCCC Verify that the total price is $6.00
            // ABCD Verify that the total price is $7.25

            // Product Code Unit Price Bulk Price
            // A $1.25  3 for $3.00
            // B $4.25
            // C $1.00  $5 for a six-pack
            // D $0.75

            List <string> orders = new List <string>()
            {
                "ABCDABA", "CCCCCCC", "ABCD"
            };
            var pos = new PointOfSaleTerminal();

            orders.ForEach(order => {
                foreach (char productId in order)
                {
                    pos.ScanItem(productId);
                }
                ;
                double TotalPrice = pos.CalculateCartTotal();
                Console.WriteLine($"Order => {order} has a total cost of ${TotalPrice}");
                pos.EmptyCart();
            });

            Console.ReadLine();
        }
コード例 #4
0
        public void Test8_GetProductList_SetProductListSetList_GetProductListReturnTheList()
        {
            // Arrange
            var             terminal          = new PointOfSaleTerminal();
            List <IProduct> productListActual = new List <IProduct>
            {
                new Product("A"),
                new Product("B")
            };

            terminal.SetProductList(productListActual);
            bool correct = false;

            // Act
            List <IProduct> actual = productListActual;

            // Assert
            List <IProduct> expected = terminal.GetProductList(); //expected return list of terminal.GetProductList()

            // ---- compare the lists before and after deep copy above
            var firstNotSecond = actual.Except(expected).ToList();
            var secondNotFirst = expected.Except(actual).ToList();

            if (!firstNotSecond.Any() && !secondNotFirst.Any())
            {
                correct = true;
            }
            // ^^^^ compare the lists before and after deep copy above

            Assert.IsTrue(correct, "GetProductList() returns a wrong list");
        }
コード例 #5
0
        public void Test12_ScanProduct_ScanProductProductWithCodeA_B_C_ScanProductReturn1_1_0()
        {
            // Arrange
            var             terminal          = new PointOfSaleTerminal();
            List <IProduct> productListActual = new List <IProduct>
            {
                new Product("A"),
                new Product("B")
            };

            terminal.SetProductList(productListActual);
            int testingValueForCodeA = terminal.SetPricing("A", 1.25f, 3, 3.0f);
            int testingValueForCodeB = terminal.SetPricing("B", 4.25f);

            // Act
            int actualProductAReturn = terminal.ScanProduct("A");
            int actualProductBReturn = terminal.ScanProduct("B");
            int actualProductCReturn = terminal.ScanProduct("C");// there is no C scanned

            // Assert
            int expectedProductAReturn = 1;
            int expectedProductBReturn = 1;
            int expectedProductCReturn = 0;// This is zero because there was no C scanned.

            Assert.AreEqual(expectedProductAReturn, actualProductAReturn, "Return value from ScanProduct('A') is wrong");
            Assert.AreEqual(expectedProductBReturn, actualProductBReturn, "Return value from ScanProduct('B') is wrong");
            Assert.AreEqual(expectedProductCReturn, actualProductCReturn, "Return value from ScanProduct('C') is wrong");
        }
コード例 #6
0
        public void Test10_SetPricing_SetPricingOfGivenProducts_SetPricingReturn1()
        {
            // Arrange
            var             terminal          = new PointOfSaleTerminal();
            List <IProduct> productListActual = new List <IProduct>
            {
                new Product("A"),
                new Product("B")
            };

            terminal.SetProductList(productListActual);

            // Act - for product with code A
            int actualReturnValueForCodeA = terminal.SetPricing("A", 1.25f, 3, 3.0f);

            // Act - for product with code B
            int actualReturnValueForCodeB = terminal.SetPricing("B", 4.25f);

            // Assert - for product with code A
            int expectedReturnValueForCodeA = 1;

            Assert.AreEqual(expectedReturnValueForCodeA, actualReturnValueForCodeA, "terminal.SetPricing('A',1.25f,3,3.0f) should return 1");

            // Assert - for product with code B
            int expectedReturnValueForCodeB = 1;

            Assert.AreEqual(expectedReturnValueForCodeB, actualReturnValueForCodeB, "terminal.SetPricing('B', 4.25f) should return 1");
        }
コード例 #7
0
        public void CalculateTotal_Should_Return_Correct_Sum(string productCodes, double totalPrice)
        {
            // arrange
            IPointOfSaleTerminal terminal = new PointOfSaleTerminal();
            IReadOnlyDictionary <string, ProductPricingSettings> settings = new Dictionary <string, ProductPricingSettings>
            {
                { "A", new ProductPricingSettings(1.25, 3, 3) },
                { "B", new ProductPricingSettings(4.25) },
                { "C", new ProductPricingSettings(1, 5, 6) },
                { "D", new ProductPricingSettings(0.75) },
                { "E", new ProductPricingSettings(1, 0.75, 1) }
            };

            terminal.SetPricing(settings);

            foreach (char productCode in productCodes)
            {
                terminal.Scan(productCode.ToString());
            }

            // act
            double result = terminal.CalculateTotal();

            // assert
            Assert.Equal(totalPrice, result);
        }
コード例 #8
0
        public void Test16_CalculateTotal_ScannedProductABCD_CalculateTotalEqual7_25()
        {
            // Arrange
            var             terminal          = new PointOfSaleTerminal();
            List <IProduct> productListActual = new List <IProduct>
            {
                new Product("A"),
                new Product("B"),
                new Product("C"),
                new Product("D")
            };

            terminal.SetProductList(productListActual);
            int ReturnValueForCodeA_priceSet = terminal.SetPricing("A", 1.25f, 3, 3.0f);
            int ReturnValueForCodeB_priceSet = terminal.SetPricing("B", 4.25f);
            int ReturnValueForCodeC_priceSet = terminal.SetPricing("C", 1.0f, 6, 5.0f);
            int ReturnValueForCodeD_priceSet = terminal.SetPricing("D", 0.75f);

            terminal.ScanProduct("A");
            terminal.ScanProduct("B");
            terminal.ScanProduct("C");
            terminal.ScanProduct("D");

            // Act
            float actualTotal = terminal.CalculateTotal();

            // Assert
            float expectedTotal = 7.25f;

            Assert.AreEqual(expectedTotal, actualTotal, "The calculated total is wrong and should be 7.25");
        }
コード例 #9
0
        public void CalculateTotal()
        {
            var terminal = new PointOfSaleTerminal(this._mockCart.Object, this._mockProductRepository.Object);
            var result   = terminal.CalculateTotal();

            result.ShouldBe(100d);
        }
コード例 #10
0
        public void SetUp()
        {
            var productA = new Item("A", 1.25m);
            var productC = new Item("C", 1);

            var items = new List <Item>
            {
                productA,
                new Item("B", 4.25m),
                productC,
                new Item("D", 0.75m),
            };

            var specialPrices = new List <SpecialPrice>
            {
                new SpecialPrice(productA, 3, 3),
                new SpecialPrice(productC, 6, 5),
            };

            var itemManager          = new ItemManager(new ItemValidator());
            var specialPricesManager = new SpecialPricesManager(itemManager, new SpecialPriceValidator());
            var basket = new Basket(itemManager, specialPricesManager);

            _terminal = new PointOfSaleTerminal(itemManager, specialPricesManager, basket);
            _terminal.SetPricing(items, specialPrices);
        }
コード例 #11
0
 private void ScanStringAsChars(PointOfSaleTerminal terminal, string productCodes)
 {
     foreach (char ch in productCodes)
     {
         terminal.Scan(ch.ToString());
     }
 }
コード例 #12
0
        public void SetPricing_Success()
        {
            // arrange
            var posTerminal = new PointOfSaleTerminal();

            // act
            posTerminal.SetPricing(new Product('A', 1));
        }
コード例 #13
0
        public void CalculateTotal_Should_Throw_ArgumentException_If_ProductPricingSettings_Is_Null_or_Empty()
        {
            // arrange
            IPointOfSaleTerminal terminal = new PointOfSaleTerminal();

            // assert
            Assert.Throws <ArgumentException>(() => terminal.CalculateTotal());
        }
コード例 #14
0
        public void Scan_Should_Throw_ArgumentNullException_If_ProductCode_Is_Null_or_Empty(string productCode)
        {
            // arrange
            IPointOfSaleTerminal terminal = new PointOfSaleTerminal();

            // assert
            Assert.Throws <ArgumentNullException>(() => terminal.Scan(productCode));
        }
コード例 #15
0
        public void Reset()
        {
            var terminal = new PointOfSaleTerminal(this._mockCart.Object, this._mockProductRepository.Object);

            terminal.Reset();

            this._mockCart.Verify(c => c.Clear(), Times.Once);
        }
コード例 #16
0
        public void ShouldRaiseExceptionIfScanUnknownProduct()
        {
            var sut = new PointOfSaleTerminal();

            sut.SetPricing(new ProductCatalog());

            Assert.Throws <ProductNotFoundException>(() => sut.Scan("A"));
        }
コード例 #17
0
 public void Setup()
 {
     terminal = new PointOfSaleTerminal();
     terminal.SetPricing('A', 1.25, Tuple.Create(3, 3.0));
     terminal.SetPricing('B', 4.25);
     terminal.SetPricing('C', 1.00, Tuple.Create(6, 5.0));
     terminal.SetPricing('D', 0.75);
 }
コード例 #18
0
        public void Scan()
        {
            var terminal = new PointOfSaleTerminal(new Cart(), this._mockProductRepository.Object);

            var ex = Should.Throw <ProductNotFoundException>(() => terminal.Scan("B"));

            ex.ProductName.ShouldBe("B");
        }
コード例 #19
0
        public void SetPricing_Should_Throw_ArgumentException_If_ProductPricingSettings_Is_Null_or_Empty()
        {
            // arrange
            IPointOfSaleTerminal terminal = new PointOfSaleTerminal();

            // assert
            Assert.Throws <ArgumentException>(() => terminal.SetPricing(null));
            Assert.Throws <ArgumentException>(() => terminal.SetPricing(new Dictionary <string, ProductPricingSettings>()));
        }
コード例 #20
0
        public IActionResult CreateScannerSession()
        {
            var terminalId          = Interlocked.Increment(ref _cartIdGenerator);
            var pointOfSaleTerminal = new PointOfSaleTerminal();

            pointOfSaleTerminal.SetPricing(_productCatalog);
            ScannerSessionsMap[terminalId] = pointOfSaleTerminal;
            return(CreatedAtRoute("GetScannerSession", new { id = terminalId }, terminalId));
        }
コード例 #21
0
        public void SetUp()
        {
            ShoppingItem a = new ShoppingItem("A", 1.25M, 3.00M, 3);
            ShoppingItem b = new ShoppingItem("B", 4.25M);
            ShoppingItem c = new ShoppingItem("C", 1.00M, 5.00M, 6);
            ShoppingItem d = new ShoppingItem("D", 0.75M);

            _terminal = new PointOfSaleTerminal(new ShoppingItem[] { a, b, c, d });
        }
コード例 #22
0
        public PointOfSaleTerminalTests()
        {
            mockCartService = new Mock <ICartService>();

            mockProductRangeService = new Mock <IProductRangeService>();

            terminal = new PointOfSaleTerminal(
                mockCartService.Object, mockProductRangeService.Object);
        }
コード例 #23
0
        public void Scan_ProductNotFoundException()
        {
            // arrange
            var posTerminal = new PointOfSaleTerminal();

            // act
            // assert
            posTerminal.Scan("A");
        }
コード例 #24
0
        static async Task Main(string[] args)
        {
            var products = new ProductModel[]
            {
                new ProductModel
                {
                    ProductCode     = "A",
                    PricePerItem    = 1.25m,
                    PriceAtVolume   = 3.00m,
                    VolumeThreshold = 3,
                    SaleType        = ProductSaleType.FlatRateAndVolumePricing
                },
                new ProductModel
                {
                    ProductCode  = "B",
                    PricePerItem = 4.25m,
                    SaleType     = ProductSaleType.FlatRate
                },
                new ProductModel
                {
                    ProductCode     = "C",
                    PricePerItem    = 1.00m,
                    PriceAtVolume   = 5.00m,
                    VolumeThreshold = 6,
                    SaleType        = ProductSaleType.FlatRateAndVolumePricing
                },
                new ProductModel
                {
                    ProductCode  = "D",
                    PricePerItem = 0.75m,
                    SaleType     = ProductSaleType.FlatRate
                },
            };

            var terminal = new PointOfSaleTerminal();

            terminal.SetPricing(products);
            await terminal.ScanProductAsync("A");

            await terminal.ScanProductAsync("B");

            await terminal.ScanProductAsync("C");

            await terminal.ScanProductAsync("D");

            await terminal.ScanProductAsync("A");

            await terminal.ScanProductAsync("B");

            await terminal.ScanProductAsync("A");

            var total = await terminal.CalculateTotalAsync();

            Console.WriteLine(total);
            Console.ReadLine();
        }
コード例 #25
0
        static void Main(string[] args)
        {
            var products = new List <ProductDTO>
            {
                new ProductDTO {
                    Code = "A", RetailPrice = 1.25m, VolumePrice = 3m, VolumeQuantity = 3
                },
                new ProductDTO {
                    Code = "B", RetailPrice = 4.25m
                },
                new ProductDTO {
                    Code = "C", RetailPrice = 1m, VolumePrice = 5m, VolumeQuantity = 6
                },
                new ProductDTO {
                    Code = "D", RetailPrice = 0.75m
                }
            };

            PointOfSaleTerminal terminal1 = new PointOfSaleTerminal();

            terminal1.SetPricing(products);
            terminal1.Scan("A");
            terminal1.Scan("B");
            terminal1.Scan("C");
            terminal1.Scan("D");
            terminal1.Scan("A");
            terminal1.Scan("B");
            terminal1.Scan("A");

            Console.WriteLine($"ABCDABA - 13.25???{terminal1.CalculateTotal()}");

            PointOfSaleTerminal terminal2 = new PointOfSaleTerminal();

            terminal2.SetPricing(products);
            terminal2.Scan("C");
            terminal2.Scan("C");
            terminal2.Scan("C");
            terminal2.Scan("C");
            terminal2.Scan("C");
            terminal2.Scan("C");
            terminal2.Scan("C");

            Console.WriteLine($"CCCCCCC - 6???{terminal2.CalculateTotal()}");

            PointOfSaleTerminal terminal3 = new PointOfSaleTerminal();

            terminal3.SetPricing(products);
            terminal3.Scan("A");
            terminal3.Scan("B");
            terminal3.Scan("C");
            terminal3.Scan("D");

            Console.WriteLine($"ABCD - 7.25???{terminal3.CalculateTotal()}");

            Console.ReadLine();
        }
コード例 #26
0
        public void Scan_WhenNothingScanned_ThenPriceIs0WithNoErrors()
        {
            PointOfSaleTerminal terminal = new PointOfSaleTerminal();

            terminal.SetPricing();

            decimal result = terminal.GetSaleTotal();

            Assert.Equal(0, result);
        }
コード例 #27
0
        public void SetPricing_SinglePrice()
        {
            var productPrice = 10m;
            var terminal     = new PointOfSaleTerminal(new Cart(), this._mockProductRepository.Object);

            terminal.SetPricing(PRODUCT_NAME, productPrice);

            this._mockProductRepository.Verify(p => p.Create(It.IsAny <string>()), Times.Never);
            this._mockProduct.Verify(p => p.SetPricing(It.Is <decimal>(price => price == productPrice)), Times.Once);
        }
コード例 #28
0
        public void AllUnknown()
        {
            decimal expected = 0.00M;
            var     terminal = new PointOfSaleTerminal();

            terminal.ScanProduct("F");
            decimal result = terminal.CalculateTotal();

            Assert.AreEqual(expected, result);
        }
コード例 #29
0
        public void SingleD()
        {
            decimal expected = 0.75M;
            var     terminal = new PointOfSaleTerminal();

            terminal.ScanProduct("D");
            decimal result = terminal.CalculateTotal();

            Assert.AreEqual(expected, result);
        }
コード例 #30
0
        public void TwoValidCodesInOneString()
        {
            decimal expected = 0.00M;
            var     terminal = new PointOfSaleTerminal();

            terminal.ScanProduct("AA");
            decimal result = terminal.CalculateTotal();

            Assert.AreEqual(expected, result);
        }
コード例 #31
0
ファイル: TestMain.cs プロジェクト: some-other-guy/Sandbox
 static void Main( string[] args )
 {
     if (1 != args.Length)
     {
         System.Console.WriteLine("Usage: PointOfSaleTernimal <command file>");
     }
     else
     {
         PointOfSaleTerminal terminal = new PointOfSaleTerminal();
         System.IO.StreamReader priceFile = new System.IO.StreamReader(args[0]);
         string line = priceFile.ReadLine();
         if (null != line)
         {
             terminal.SetPricing(line);
             while ((line = priceFile.ReadLine()) != null)
             {
                 terminal.Scan(line);
             }
         }
         System.Console.WriteLine("Total sale: $" + terminal.CalculateTotal());
     }
 }