Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions CurrencyConverter.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import UIKit

/*
On May 13, 2020
$1 USD = $1.41 CAD
$1 USD = $24.21 MXN
*/

enum Currency: String {
case cad = "CAD"
case mxn = "MXN"
}

let usToCad = 1.41
let usToMxn = 24.21

var currency = Currency.cad

func convert(_ dollars: Double) -> Double {
switch currency {
case .cad:
return dollars * usToCad
case .mxn:
return dollars * usToMxn
}
}

func convert(amountString: String) -> String? {
let amount = Double(amountString)
guard let unwrappedAmount = amount else {
return nil
}
let convertedAmount = convert(unwrappedAmount)
let formatter = NumberFormatter()
formatter.numberStyle = .currency
Comment on lines +34 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job getting the currency formatter working.

if let formattedAmount = formatter.string(for: convertedAmount) {
print("\(formattedAmount) \(currency.rawValue)")
return "\(formattedAmount) \(currency.rawValue)"
} else {
return nil
}
}

convert(amountString: "15.00")
convert(amountString: "25.00")

currency = Currency.mxn

convert(amountString: "15.00")
convert(amountString: "25.00")
Comment on lines +44 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the test cases here.

4 changes: 4 additions & 0 deletions CurrencyConverter.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>