Kod bu
element koyunca type hatası alıyorum.
/// Third item free when you buy any two deodorants (product codes 65 & 66)
class BuyTwoGetThirdFreeOffer :Offer {
init(){
super.init(name: "3 for 2 on Deodorants")
applicableProductIds = [65,66];
}
override func isApplicableTo(list: [ShoppingItem]) -> Bool {
return list.reduce(false) { $0 || applicableProductIds.contains($1.productId) }
}
override func valueOfDiscountFrom(list: [ShoppingItem]) -> Int {
//order the list by price before discounting every third item
var a = applicableProducts(list: list)
a = a.sorted { $0.priceInPence > $1.priceInPence }
var price: Int = 0
// for product in a {
a.enumerated().forEach { (index,element) in
if index % 3 == 2{
price += product.priceInPence
//// }
}
}
return price
}
}Testi de bu hocam
price değişkenin 219 438 538 olması gerekiyor ürün dizideki ürün sayısına göre
func testBuyTwoGetThirdFree5Items(){
addMultiple(prodId: 14, quantity: 2) //Lynx Africa 319p
addMultiple(prodId: 15, quantity: 3) //Dove deodorant 219p
XCTAssertEqual(receipt.discount(),219,"Buy two get third free correct with 5 items");
}
func testBuyTwoGetThirdFree6ItemsCheapestFree(){
addMultiple(prodId: 15, quantity: 4) //Dove deodorant 219p
addMultiple(prodId: 14, quantity: 2) //Lynx Africa 319p
XCTAssertEqual(receipt.discount(),438,"Buy two get third free correct with 6 items (4/2)");
}
func testBuyTwoGetThirdFree6ItemsOneOfEachPriceFree(){
addMultiple(prodId: 15, quantity: 3) //Dove deodorant 219p
addMultiple(prodId: 14, quantity: 3) //Lynx Africa 319p
XCTAssertEqual(receipt.discount(),538,"Buy two get third free correct with 6 items (3/3)");
Kendi projenize has protocoller ve classlarınız olduğundan kodun okunması ve anlaşılması zor oluyor hocam. Ama benim tahminim arraydeki elemanların Integer olmadığı yönünde, ya da for product in a diye loop yapıp içine foreach i koymanızdan ötürü gerçekleşiyor olabilir.