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)") }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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 

Test Cases
case 1
Enter 5 records:
20.50, apple, sweet
25.75, orange, sour
25.25, mango yellow, sweet
15.50, mango green, sour
7.75, banana green, bitter
bitter fruit total price: 7.75
sour fruit total price: 41.25
Sweet fruit total price: 45.75
case 2
Enter 5 records:
26, rambutan, Sweet
180, grapes, sweet
67, avocado, bitter
35, strawberry, sweet
50, pineapple, sour
bitter fruit total price: 67.00
sour fruit total price: 50.00
Sweet fruit total price: 241
case 3
Enter 5 records:
23.25, guava, sour
1 2 3, ponkan, sweet
Invalid Input
case 4
Enter 5 records:
twelve, tomato, sweet
Invalid Input
Transcribed Image Text:Test Cases case 1 Enter 5 records: 20.50, apple, sweet 25.75, orange, sour 25.25, mango yellow, sweet 15.50, mango green, sour 7.75, banana green, bitter bitter fruit total price: 7.75 sour fruit total price: 41.25 Sweet fruit total price: 45.75 case 2 Enter 5 records: 26, rambutan, Sweet 180, grapes, sweet 67, avocado, bitter 35, strawberry, sweet 50, pineapple, sour bitter fruit total price: 67.00 sour fruit total price: 50.00 Sweet fruit total price: 241 case 3 Enter 5 records: 23.25, guava, sour 1 2 3, ponkan, sweet Invalid Input case 4 Enter 5 records: twelve, tomato, sweet Invalid Input
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY