Created
November 15, 2015 02:35
-
-
Save takaheraw/4339f39f69ee7a4fdc11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Donor { | |
let title: String | |
let firstName: String | |
let familyName: String | |
let lastDonation: Float | |
init(_ title:String, _ first:String, _ family:String, _ last:Float) { | |
self.title = title | |
self.firstName = first | |
self.familyName = family | |
self.lastDonation = last | |
} | |
} | |
class DonorDatabase { | |
private var donors: [Donor] | |
init() { | |
donors = [ | |
Donor("Ms", "Anne", "Jones", 0), | |
Donor("Mr", "Bob", "Smith", 100), | |
Donor("Dr", "Alice", "Doe", 200), | |
Donor("Prof", "Joe", "Davis", 320) | |
] | |
} | |
func generateGalaInvitations(maxNumber: Int) -> [String] { | |
var targetDonors: [Donor] = donors.filter({ $0.lastDonation > 0 }) | |
targetDonors.sort({ $0.lastDonation > $1.lastDonation }) | |
if (targetDonors.count > maxNumber) { | |
targetDonors = Array(targetDonors[0..<maxNumber]) | |
} | |
return targetDonors.map({ donor in | |
return "Dear \(donor.title). \(donor.familyName)" | |
}) | |
} | |
} | |
let donorDb = DonorDatabase() | |
let galaInvitations = donorDb.generateGalaInvitations(2) | |
for invite in galaInvitations { | |
print(invite) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment