var bankAccountBalance = 3000000.00
var macbookpro = 191000.00
var iphone6sPlus = 59000.00
var newSweetHome = 3500000.00
var amountRequired = 00.00
func purchaseItem1(currentBalance : Double, itemPrice : Double) ->Double {
if(itemPrice<=currentBalance)
{
print("Purchased item for :\(itemPrice)")
print("Your current outstanding Account balance is \(currentBalance)")
return currentBalance-itemPrice
}
else {
amountRequired = itemPrice - currentBalance
print("You dont have enough account balance to purchase this item :( ")
print("You need \(amountRequired)Rs in your bank")
return amountRequired
}
}
purchaseItem1(currentBalance: bankAccountBalance, itemPrice: macbookpro)
purchaseItem1(currentBalance: bankAccountBalance, itemPrice: iphone6sPlus)
purchaseItem1(currentBalance: bankAccountBalance, itemPrice: newSweetHome)
答案 0 :(得分:0)
你不能。您是否考虑过使用字典,例如
let items = ["MacBook Pro": 191000.0, "iPhone 6S Plus": 59000.0"]
或者你应该真的在某种数据结构中编码这些信息,例如
struct Item {
var name: String
var price: Double
}
let items = [
Item(name: "MacBook Pro", price: 191000),
Item(name: "iPhone 6S Plus", price: 59000)
]