SimpleGeometryCalculator

GeometryCalculator

GeometryCalculator

 

class ViewController: UIViewController, UITextFieldDelegate {

var selectedIndex : Int = 1

@IBOutlet weak var labal6 : UILabel! // geometry type
@IBOutlet weak var labal1 : UILabel! // input1 label
@IBOutlet weak var labal2 : UILabel! // input2 label
@IBOutlet weak var labal3 : UILabel! // input3 label

@IBOutlet weak var textField1 : UITextField! // input1
@IBOutlet weak var textField2 : UITextField! // input2
@IBOutlet weak var textField3 : UITextField! // input3
@IBOutlet weak var textField4 : UITextField! // S.A.
@IBOutlet weak var textField5 : UITextField! // Volume

@IBOutlet weak var selector : UISegmentedControl!

func calculateGeometry() {
if let type = GeometryType.valueOf(selectedIndex) {
label6.text = type.description

// label & textfield
textField1.hidden = false
textField2.hidden = false
textField3.hidden = false
switch type {
case .Sphere:
label1.text = “Radius”
label2.text = “”
label3.text = “”
textField2.hidden = true
textField3.hidden = true
let calc = Sphere(radius: textField1.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
case .Cone:
label1.text = “Radius”
label2.text = “Height”
label3.text = “”
textField3.hidden = true
let calc = Cone(radius: textField1.text!.toDouble(), height: textField2.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
case .Cylinder:
label1.text = “Radius”
label2.text = “Height”
label3.text = “”
textField3.hidden = true
let calc = Cylinder(radius: textField1.text!.toDouble(), height: textField2.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
case .RectangularPrism:
label1.text = “Width”
label2.text = “Height”
label3.text = “Length”
let calc = RectangularPrism(width: textField1.text!.toDouble(), height: textField2.text!.toDouble(), length: textField3.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
case .SquarePyramid:
label1.text = “Base”
label2.text = “Height”
label3.text = “”
textField3.hidden = true
let calc = SquarePyramid(base: textField1.text!.toDouble(), height: textField2.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
case .IsoscelesTriangularPrism:
label1.text = “Base”
label2.text = “Height”
label3.text = “Length”
let calc = IsoscelesTriangularPrism(base: textField1.text!.toDouble(), height: textField2.text!.toDouble(), length: textField3.text!.toDouble())
textField4.text = “\(calc.SurfaceArea)”
textField5.text = “\(calc.Volume)”
}
}
}

@IBAction func selectGeometry(sender: AnyObject) {
selectedIndex = selector.selectedSegmentIndex + 1
calculateGeometry()
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
calculateGeometry()
return true
}

override func viewDidLoad() {
super.viewDidLoad()
// do something
calculateGeometry()
textField1.delegate = self
textField2.delegate = self
textField3.delegate = self
}

}