Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- class Players{
- var fullName:String? //optionals
- var runs:Int?
- var matchesPlayed:Int?
- var wickets:Int?
- init() { //Designated initializer
- fullName = ""
- runs = 0
- matchesPlayed = 0
- wickets = 0
- }
- init(_ fullName:String, _ runs:Int, _ matchesPlayed:Int, _ wickets:Int) {
- self.fullName = fullName
- self.runs = runs
- self.wickets = wickets
- self.matchesPlayed = matchesPlayed
- }
- func printInfo() {
- print("Player name: \(fullName!)")
- print("Scored \(runs!) and took \(wickets!) in \(matchesPlayed!) matches")
- }
- }
- let sachin = Players("Sachin Tendulkar", 10000, 100, 97)
- sachin.printInfo()
- print("\n")
- class Records{
- var isCenturian:Bool?
- var fiveWicketsHaul:Bool?
- // var highestRuns = [Int]() //arrays
- //var highestWickets = [Int]()
- // var manOfTheMatch = [String : String]() //Dictionaries
- init(_ isCenturian:Bool, _ fiveWicketsHaul:Bool) {
- self.isCenturian = isCenturian
- self.fiveWicketsHaul = fiveWicketsHaul
- }
- }
- //Inheritance
- class BangladeshPlayers : Players{
- var runsAtLeague:Int?
- var wicketsAtLeague:Int?
- var matchesAtLeague:Int?
- var totalRuns:Int?
- var totalWickets:Int?
- var recordsAchieved:Records? //empty class
- //properties
- var hasRecord:String{
- if let actualRecord = recordsAchieved{
- if recordsAchieved!.isCenturian != nil || recordsAchieved!.fiveWicketsHaul != nil{
- return "The player has an international record"
- }
- else {
- return "The player doesn't have any international record"
- }
- }
- else{
- return "The player doesn't have any international record yet"
- }
- }
- convenience init(customName:String, customRuns:Int, customMatchesPlayed:Int, customWickets:Int) { //convernience initializer
- self.init()
- fullName = customName
- runs = customRuns
- matchesPlayed = customMatchesPlayed
- wickets = customWickets
- }
- func leagueScore(_ runs:Int, _ match:Int, _ wicket:Int) {
- runsAtLeague = runs
- matchesAtLeague = match
- wicketsAtLeague = wickets
- }
- override func printInfo(){ //override is used to modify anything from the superclass
- print(" International Records: ")
- super.printInfo() //used to access anything from superclass
- if let actual = matchesAtLeague{ //checking optionals
- print("\r League Scores")
- print("Scored \(runsAtLeague!) and took \(wicketsAtLeague!) in \(matchesAtLeague!) matches")
- }
- else{
- print("No league matches yet")
- }
- }
- }
- let records = Records(true, false)
- //player1
- let tamim = BangladeshPlayers(customName:"Tamim Iqbal" , customRuns:7500, customMatchesPlayed:125, customWickets:0)
- tamim.leagueScore(2000, 100, 1)
- tamim.recordsAchieved = records
- tamim.printInfo()
- print(tamim.hasRecord, "\n")
- //player2
- let miraz = BangladeshPlayers("Mehedi hasan Miraz", 1000, 40, 45)
- miraz.printInfo()
- print(miraz.hasRecord)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement