/// <summary> /// 蘋果買2顆 , 香蕉一串 ,芭樂3顆 共多少錢 /// </summary> /// <returns></returns> public decimal CulcFruitsPrice(Dictionary <string, int> buyFruits) { var fruits = _prdouctRepository.SelectFruits(); var totalPrice = 0m; foreach (var KeyValue in buyFruits) { var fruit = fruits.FirstOrDefault(p => p.Name == KeyValue.Key); if (fruit != null) { totalPrice += fruit.Price * KeyValue.Value; } } //foreach (var fruit in fruits) //{ // if (fruit.Name == "蘋果") // totalPrice += fruit.Price * 2; // else if (fruit.Name == "香蕉") // totalPrice += fruit.Price * 1; // else if (fruit.Name == "芭樂") // totalPrice += fruit.Price * 3; //} return(totalPrice); }
public void CulcFruitsPriceTest_Return135() { var service2 = new ProductService(_mock); _mock.SelectFruits().Returns(p => new List <Product> { new Product { Name = "蘋果", Price = 30 }, new Product { Name = "香蕉", Price = 15 }, new Product { Name = "芭樂", Price = 20 }, }); var service = new ProductService(new ProductRepositoryStub()); var expect = 130; var result = service2.CulcFruitsPrice(GetFruit()); Assert.AreEqual(expect, result); }