|
| 1 | +# Swift Fundamentals III Module Project |
| 2 | + |
| 3 | +The goal of today's module project is to create a Currency Conversion playground that can convert from US dollars (USD) to Canadian dollars (CAD) and Mexican Pesos (MXN). |
| 4 | + |
| 5 | +Get the currency conversion value from Google: |
| 6 | + |
| 7 | +On August 7th, 2019 they were: |
| 8 | + |
| 9 | + $1 USD to $1.33 CAD |
| 10 | + $1 USD to $19.70 MXN |
| 11 | + |
| 12 | +## Add the Conversion Logic |
| 13 | + |
| 14 | +1. Create a currency type enum above the class and below the `import` statements. |
| 15 | + |
| 16 | + ```swift |
| 17 | + enum Currency { |
| 18 | + case cad |
| 19 | + case mxn |
| 20 | + } |
| 21 | + ``` |
| 22 | + |
| 23 | +2. Create a property named `currency` of type `Currency`. This will store the current currency type we'll be converting to. Set an initial value of `.cad`. |
| 24 | +3. Create a helper method to calculate the currency based on the Currency using the method signature: |
| 25 | + ```swift |
| 26 | + func convert(_ dollars: Double) -> Double { |
| 27 | + |
| 28 | + } |
| 29 | + ``` |
| 30 | +4. In the above method: |
| 31 | + * Check the value of `currency` to see whether you should convert to CAD or MXN |
| 32 | + * Perform the conversion with the dollars passed into this method |
| 33 | + * Return the converted value |
| 34 | +5. Create a function called `convert(amountString: String)` In it, do the following: |
| 35 | + * create a constant called `amount`. Its value should be the `amountString` that is initialized into a `Double` |
| 36 | + * use a `guard let` to unwrap the new `amount` constant. |
| 37 | + * convert the dollar amount to the expected currency (hint, you'll want to call the `convert` method you created in step 3) |
| 38 | + * Update the `toCurrencyTextField.text` property with the converted currency value |
| 39 | + |
| 40 | +## Go Further (Optional) |
| 41 | + |
| 42 | +1. Customize the output using a `NumberFormatter` |
| 43 | + |
| 44 | + ```swift |
| 45 | + var currencyFormatter: NumberFormatter = { |
| 46 | + let formatter = NumberFormatter() |
| 47 | + formatter.numberStyle = .currency |
| 48 | + return formatter |
| 49 | + }() |
| 50 | + ``` |
| 51 | + |
| 52 | +2. Use the `string(from:)` method to convert from a number to a String for display |
| 53 | + |
| 54 | + |
0 commit comments