Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // TipPro
  4. //
  5. // Created by Jenstine on 3/25/17.
  6. // Copyright © 2017 Jenstine. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12. @IBOutlet weak var billAmountField: UITextField!
  13. @IBOutlet weak var tipSelector: UISegmentedControl!
  14. @IBOutlet weak var tipAmountField: UITextField!
  15. @IBOutlet weak var totalAmountField: UITextField!
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view, typically from a nib.
  20. }
  21.  
  22. override func didReceiveMemoryWarning() {
  23. super.didReceiveMemoryWarning()
  24. // Dispose of any resources that can be recreated.
  25. }
  26.  
  27.  
  28. @IBAction func calculateTip(sender: AnyObject) {
  29. guard let billAmount = Double(billAmountField.text!) else {
  30. //show error
  31. billAmountField.text = ""
  32. tipAmountField.text = ""
  33. totalAmountField.text = ""
  34. return
  35. }
  36.  
  37. var tipPercentage = 0.0
  38.  
  39. switch tipSelector.selectedSegmentIndex {
  40. case 0:
  41. tipPercentage = 0.15
  42. case 1:
  43. tipPercentage = 0.18
  44. case 2:
  45. tipPercentage = 0.20
  46. default:
  47. break
  48. }
  49.  
  50. let roundedBillAmount = round(100*billAmount)/100
  51. let tipAmount = roundedBillAmount * tipPercentage
  52. let roundedTipAmount = round(100*tipAmount)/100
  53. let totalAmount = roundedBillAmount + roundedTipAmount
  54.  
  55. if (!billAmountField.isEditing) {
  56. billAmountField.text = String(format: "%.2f", roundedBillAmount)
  57. }
  58. tipAmountField.text = String(format: "%.2f", roundedTipAmount)
  59. totalAmountField.text = String(format: "%.2f", totalAmount)
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement