HERE'S MY CODE import Foundation class Item{ var name: String var price: Double init(_ name:String,_ price:Double){ self.name = name; self.price = price; } } class ItemFruit: Item{ var taste: String init(_ name:String,_ price:Double,_ taste: String){ self.taste = taste super.init(name,price); } } print("Enter 5 records:") var x = 0; var pricesTaste:[String: Double] = ["sweet":0,"sour":0,"bitter":0]; while x < 5 { if let input = readLine() { let fullNameArr = input.components(separatedBy: ", ") if let price: Double = Double(fullNameArr[0]){ let itemObj = ItemFruit(fullNameArr[1],price,fullNameArr[2]) if let prevPrice:Double = pricesTaste[itemObj.taste] { pricesTaste[itemObj.taste] = prevPrice + itemObj.price } } } x = x+1 } if let priceBitter:Double = pricesTaste["bitter"] { print("bitter fruit total price: \(priceBitter)") } if let priceSour:Double = pricesTaste["sour"] { print("sour fruit total price: \(priceSour)") } if let priceSweet:Double = pricesTaste["sweet"] { print("sweet fruit total price: \(priceSweet)") }
HERE'S MY CODE
import Foundation
class Item{
var name: String
var price: Double
init(_ name:String,_ price:Double){
self.name = name;
self.price = price;
}
}
class ItemFruit: Item{
var taste: String
init(_ name:String,_ price:Double,_ taste: String){
self.taste = taste
super.init(name,price);
}
}
print("Enter 5 records:")
var x = 0;
var pricesTaste:[String: Double] = ["sweet":0,"sour":0,"bitter":0];
while x < 5 {
if let input = readLine()
{
let fullNameArr = input.components(separatedBy: ", ")
if let price: Double = Double(fullNameArr[0]){
let itemObj = ItemFruit(fullNameArr[1],price,fullNameArr[2])
if let prevPrice:Double = pricesTaste[itemObj.taste] {
pricesTaste[itemObj.taste] = prevPrice + itemObj.price
}
}
}
x = x+1
}
if let priceBitter:Double = pricesTaste["bitter"] {
print("bitter fruit total price: \(priceBitter)")
}
if let priceSour:Double = pricesTaste["sour"] {
print("sour fruit total price: \(priceSour)")
}
if let priceSweet:Double = pricesTaste["sweet"] {
print("sweet fruit total price: \(priceSweet)")
}
HELP ME TO PRINT THE INVALID INPUT
Step by step
Solved in 2 steps