Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Swift Function Exercise
- Sample Example:
- Here's how to write a basic function in Swift that adds two numbers:
- ```
- func add(_ x: Int, _ y: Int) -> Int {
- return x + y
- }
- // Using the function:
- let result = add(5, 3)
- print(result) // Prints: 8
- ```
- Your Task:
- Write a function called processOrder that:
- Takes a string in the format "itemName:quantity@price"
- Returns a tuple of (itemName: String, total: Double, isValid: Bool)
- Requirements:
- Extract the item name, quantity, and price from the string
- Quantity must be converted from string to Int
- Price must be converted from string to Double
- Return ("", 0.0, false) if the string format is invalid
- Total should be quantity * price
- Test Cases:
- processOrder("iPhone:[email protected]") // Returns ("iPhone", 1999.98, true)
- processOrder("Coffee:[email protected]") // Returns ("Coffee", 13.50, true)
- processOrder("Laptop:[email protected]") // Returns ("", 0.0, false)
- processOrder("Invalid String") // Returns ("", 0.0, false)
Advertisement
Add Comment
Please, Sign In to add comment