Advertisement
Guest User

cricketInfo

a guest
Apr 9th, 2020
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.33 KB | None | 0 0
  1. import Foundation
  2.  
  3.  
  4. class Players{
  5.     var fullName:String?        //optionals
  6.     var runs:Int?
  7.     var matchesPlayed:Int?
  8.     var wickets:Int?
  9.  
  10.     init() {                        //Designated initializer
  11.         fullName = ""
  12.         runs = 0
  13.         matchesPlayed = 0
  14.         wickets = 0
  15.     }
  16.    
  17.  
  18.     init(_ fullName:String, _ runs:Int, _ matchesPlayed:Int, _ wickets:Int) {
  19.         self.fullName = fullName
  20.         self.runs = runs
  21.         self.wickets = wickets
  22.         self.matchesPlayed = matchesPlayed
  23.     }
  24.    
  25.     func printInfo() {
  26.         print("Player name: \(fullName!)")
  27.         print("Scored \(runs!) and took  \(wickets!) in  \(matchesPlayed!) matches")
  28.     }
  29. }
  30.  
  31. let sachin = Players("Sachin Tendulkar", 10000, 100, 97)
  32. sachin.printInfo()
  33. print("\n")
  34.  
  35.  
  36. class Records{
  37.     var isCenturian:Bool?
  38.     var fiveWicketsHaul:Bool?
  39.  
  40.    // var highestRuns = [Int]()           //arrays
  41.     //var highestWickets = [Int]()
  42.  
  43.    // var manOfTheMatch = [String : String]()     //Dictionaries
  44.  
  45.     init(_ isCenturian:Bool, _ fiveWicketsHaul:Bool) {
  46.         self.isCenturian = isCenturian
  47.         self.fiveWicketsHaul = fiveWicketsHaul
  48.     }
  49.  
  50.    
  51. }
  52.  
  53.  
  54.  
  55. //Inheritance
  56. class BangladeshPlayers : Players{
  57.      
  58.     var runsAtLeague:Int?
  59.     var wicketsAtLeague:Int?
  60.     var matchesAtLeague:Int?
  61.    
  62.     var totalRuns:Int?
  63.     var totalWickets:Int?
  64.     var recordsAchieved:Records?       //empty class
  65.  
  66.  
  67.     //properties
  68.     var hasRecord:String{
  69.  
  70.         if let actualRecord = recordsAchieved{    
  71.             if recordsAchieved!.isCenturian != nil || recordsAchieved!.fiveWicketsHaul != nil{
  72.                 return "The player has an international record"
  73.             }
  74.             else {
  75.                 return "The player doesn't have any international record"
  76.             }
  77.         }
  78.         else{
  79.             return "The player doesn't have any international record yet"
  80.         }
  81.     }
  82.  
  83.  
  84.     convenience init(customName:String, customRuns:Int, customMatchesPlayed:Int, customWickets:Int) {      //convernience initializer
  85.         self.init()
  86.         fullName = customName
  87.         runs = customRuns
  88.         matchesPlayed = customMatchesPlayed
  89.         wickets = customWickets
  90.     }
  91.  
  92.     func leagueScore(_ runs:Int, _ match:Int, _ wicket:Int) {
  93.         runsAtLeague = runs
  94.         matchesAtLeague = match
  95.         wicketsAtLeague = wickets
  96.     }
  97.  
  98.  
  99.        
  100.     override func printInfo(){              //override is used to modify anything from the superclass
  101.         print(" International Records: ")
  102.         super.printInfo()           //used to access anything from superclass
  103.  
  104.         if let actual = matchesAtLeague{        //checking optionals
  105.             print("\r League Scores")
  106.             print("Scored \(runsAtLeague!) and took  \(wicketsAtLeague!) in  \(matchesAtLeague!) matches")
  107.         }
  108.         else{
  109.              print("No league matches yet")
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115.  
  116. let records = Records(true, false)
  117.  
  118.  
  119. //player1
  120. let tamim = BangladeshPlayers(customName:"Tamim Iqbal" , customRuns:7500, customMatchesPlayed:125, customWickets:0)
  121. tamim.leagueScore(2000, 100, 1)
  122. tamim.recordsAchieved = records
  123. tamim.printInfo()
  124. print(tamim.hasRecord, "\n")
  125.  
  126. //player2
  127. let miraz = BangladeshPlayers("Mehedi hasan Miraz", 1000, 40, 45)
  128. miraz.printInfo()
  129. print(miraz.hasRecord)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement