Demo entry 2298299

Person

   

Submitted by anonymous on Jul 21, 2015 at 15:51
Language: Swift. Code size: 1.8 kB.

import Foundation

enum GenderType {
    case Male, Female
}

func stringForGender(gender: GenderType) -> String {
    switch gender {
    case GenderType.Male:
        return "Male"
    case GenderType.Female:
        return "Female"
    }
}

class Person : NSObject {
    
    var firstName, lastName, state : String
    var birthday : NSDate
    var socialSecurityNumber : NSInteger
    var gender : GenderType
    
    var fullName : String {
        get {
            return "\(firstName) \(lastName)"
        }
    }
    
    override var description : String {
        get {
            let genderString = stringForGender(self.gender)
            return "Name: \(self.fullName), state: \(self.state), birthday: \(self.birthday), gender: \(genderString)"
        }
    }
    
    init(dbLine: String) {
        var wordArray : [String] = dbLine.componentsSeparatedByString(" ")
        
        if wordArray.count != 5 {
            print("Failed to read in person from line: \(dbLine)")
        }
        
        let dateString = wordArray[0]
        
        self.birthday = Person.birthDateFromDateString(dateString)
        self.socialSecurityNumber = Int(wordArray[1])!
        self.firstName = wordArray[2]
        self.lastName = wordArray[3]
        self.state = wordArray[4]
        
        self.gender = GenderType.Male
    }
    
    static func birthDateFromDateString(dateString: String) -> NSDate {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "ddMMyyyy"
        
        return dateFormatter.dateFromString(dateString)!
    }
    
    func compareAge(anotherPerson : Person) -> NSComparisonResult {
        return self.birthday.compare(anotherPerson.birthday)
    }
    
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).