Question: Could you please write this in swift code and update it in the swift class below please define a Discountable protocol, in which has a
Could you please write this in swift code and update it in the swift class below
please define a Discountable protocol, in which has a function func discount( _ itemName: String) -> Double. Update the Item class, which conforms to this protocol and implements the discount function. When the input item name is apple2, the discount amount is 0.3, for other item names, the discount is 0.0. The following is the example inputs and outputs:
public class Item {
private var name:String
private var price:Double
private var quantity:Int
public init(itemName:String,itemPrice:Double,numPurchased:Int){
self.name = itemName
self.price = itemPrice
self.quantity = numPurchased
}
public func toString()->String{
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .currency
let formatePrice = formatter.string(from: price as NSNumber)!
let formatePriceQuantity = formatter.string(from: price*Double(quantity) as NSNumber)!
var str = (name + "\t" + formatePrice + "\t" + String(quantity))
str += ("\t" + formatePriceQuantity)
return str
}
public func getPrice()->Double{
return price;
}
public func getName()->String{
return name;
}
public func getQuantity()->Int{
return quantity;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
