Home > other >  Shake Gesture generate random discount 0 % - 20% then deducted price
Shake Gesture generate random discount 0 % - 20% then deducted price

Time:12-01

Hi, I'm trying to find out the discount number when the device is shake and then it will also change the pricing of grand total from the discount that the device is shaked

//
//  CheckOutViewController.swift
//  MSD_Project_NicholasGoh
//
//  Created by StudentQ on 24/11/21.
//

import UIKit

    class CheckOutViewController: UIViewController {
    
    
        @IBOutlet var totalCostLbl: UILabel!
    
        @IBOutlet var discountLbl: UILabel!
    
    
        var grandTotal:Double = total
        var discount:Double = 0.0
    
    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if motion == .motionShake{
            print("Shaked")
            
            let randomDouble = Double.random(in: 0..<20)
            
            
            discountLbl.text = "Discount: \(randomDouble)%"
        }
    }
    
    
    
    

        override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        totalCostLbl.text = "Total Cost: $\(grandTotal)"
        discountLbl.text = "Discount: \(discount)%"
        }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        }
    */
    
    
        @IBAction func payBtn(_ sender: UIButton) {
        total = 0.0
        navigationController?.popViewController(animated: true)
        }
    
    }

the discount and grand total are both Double but I have stuck to understand that do I have to make them to string ??? or display them both as Double Values.

CodePudding user response:

Answer Resolve: // // CheckOutViewController.swift // MSD_Project_NicholasGoh // // Created by StudentQ on 24/11/21. //

import UIKit

class CheckOutViewController: UIViewController {
    
    
    @IBOutlet var totalCostLbl: UILabel!
    
    @IBOutlet var discountLbl: UILabel!
    
    
    var grandTotal:Double = total
    var discount:Double = 0.0
    
    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if motion == .motionShake{
            let alertController = UIAlertController(title: nil, message: "Device has been Shaked for Discount", preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
            
            self.present(alertController, animated: true, completion: nil)
            
            let randomDouble = Double.random(in: 0..<20)
            
            totalCostLbl.text = "Total Cost: $\(round(grandTotal / randomDouble))"
              
            discountLbl.text = "Discount: \(round(randomDouble))%"
        }
    }
    
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        totalCostLbl.text = "Total Cost: $\(grandTotal)"
        discountLbl.text = "Discount: \(discount)%"
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    @IBAction func payBtn(_ sender: UIButton) {
        total = 0.0
        navigationController?.popViewController(animated: true)
    }
    
}
  • Related