aboutsummaryrefslogtreecommitdiff
path: root/Space Calculator/ViewController.swift
blob: b7734e6bd39d09294f7534efadc01d02d6b0b5d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
//  ViewController.swift
//  Space Calculator
//
//  Created by Lucifer Conrad Reeves on 07/07/18.
//  Copyright © 2018 Lucifer Reeves. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    @IBOutlet weak var outputLbl: UILabel!
    var btnSound: AVAudioPlayer!
    enum Operation:String {
        case Divide = "/"
        case Add = "+"
        case Subtract = "-"
        case Multiply = "*"
        case Empty = "Empty"
    }
    var runningNumber = ""
    var left = ""
    var right = ""
    var currentOperation = Operation.Empty;
    var result = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let path = Bundle.main.path(forResource: "btn", ofType: "wav")
        let soundURL = URL(fileURLWithPath: path!)
        do {
            try btnSound = AVAudioPlayer(contentsOf: soundURL)
            btnSound.prepareToPlay()
        } catch let err as NSError {
            print(err.debugDescription)
        }
        outputLbl.text = "0"
    }
    @IBAction func numberPressed(sender: UIButton) {
        playSound()
        runningNumber += "\(sender.tag)"
        outputLbl.text = runningNumber
        
    }
    
    @IBAction func onDividePressed(sender: Any) {
        processOperation(operation: .Divide)
    }
    @IBAction func onMultiplyPressed(sender: Any) {
        processOperation(operation: .Multiply)
    }
    @IBAction func onAddPressed(sender: Any) {
        processOperation(operation: .Add)
    }
    @IBAction func onSubractPressed(sender: Any) {
        processOperation(operation: .Subtract)
    }
    @IBAction func onEqualPressed(sender: Any){
        processOperation(operation: currentOperation)
    }
    @IBAction func onClearPressed(sender: Any) {
        playSound()
        result = ""
        left = ""
        right = ""
        runningNumber = ""
        currentOperation = Operation.Empty
        outputLbl.text = "0"
    }
    
    func playSound() {
        if btnSound.isPlaying {
            btnSound.stop()
        }
        btnSound.play()
    }
    
    func processOperation(operation:Operation) {
        playSound()
        if currentOperation != Operation.Empty {
            if runningNumber != "" {
                right = runningNumber
                runningNumber = ""
                if currentOperation == Operation.Divide {
                    result = "\(Double(left)! / Double(right)!)"
                } else if currentOperation == Operation.Subtract {
                    result = "\(Double(left)! - Double(right)!)"
                } else if currentOperation == Operation.Multiply {
                    result = "\(Double(left)! * Double(right)!)"
                } else if currentOperation == Operation.Add {
                    result = "\(Double(left)! + Double(right)!)"
                }
                left = result
                outputLbl.text = result
            }
            currentOperation = operation
        } else {
            left = runningNumber
            runningNumber = ""
            currentOperation = operation
        }
    }
}