Advertisement
Guest User

playersInfo

a guest
Apr 10th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.28 KB | None | 0 0
  1. import Foundation
  2.  
  3. import Foundation
  4.  
  5.  
  6. class Players{
  7.     var fullName:String?        //optionals
  8.     var runs:Int?
  9.     var matchesPlayed:Int?
  10.     var wickets:Int?
  11.  
  12.     init() {                        //Designated initializer
  13.         fullName = ""
  14.         runs = 0
  15.         matchesPlayed = 0
  16.         wickets = 0
  17.     }
  18.    
  19.  
  20.     init(_ fullName:String, _ runs:Int, _ matchesPlayed:Int, _ wickets:Int) {
  21.         self.fullName = fullName
  22.         self.runs = runs
  23.         self.wickets = wickets
  24.         self.matchesPlayed = matchesPlayed
  25.     }
  26.    
  27.     func printInfo() {
  28.         print("Player name: \(fullName!)")
  29.         print("Scored \(runs!) and took  \(wickets!) in  \(matchesPlayed!) matches")
  30.     }
  31. }
  32.  
  33. let sachin = Players("Sachin Tendulkar", 10000, 100, 97)
  34. sachin.printInfo()
  35. print("\n")
  36.  
  37.  
  38. class Records{
  39.     var isCenturian:Bool?
  40.     var fiveWicketsHaul:Bool?
  41.  
  42.     var highestRuns = [Int]()           //arrays
  43.     var highestWickets = [Int]()
  44.  
  45.    var manOfTheMatch = [String : String]()     //Dictionaries
  46.  
  47.     init(_ isCenturian:Bool, _ fiveWicketsHaul:Bool) {
  48.         self.isCenturian = isCenturian
  49.         self.fiveWicketsHaul = fiveWicketsHaul
  50.     }
  51.  
  52.     func getHighestRuns(_ a:Int, _ b:Int, _ c:Int) {
  53.         highestRuns.append(a)   //inserting into array
  54.         highestRuns += [b, c]
  55.     }
  56.  
  57.  
  58.     func printHighestRuns(){
  59.         print("Three best innings: ")
  60.         for i in 0...2{
  61.             print(highestRuns[i])
  62.         }
  63.     }
  64.  
  65.     func getManOfTheMatch(_ date1:String , _ match1:String, _ date2:String, _ match2:String){  
  66.          manOfTheMatch[date1] = match1
  67.          manOfTheMatch[date2] = match2
  68.     }
  69.  
  70.     func printManOfTheMatch(){
  71.         print("Recent man of the match of the player: ")
  72.         for (date, match) in manOfTheMatch{
  73.             print("In \(date) , got Man of the match in \(match) match")
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79.  
  80. //Inheritance
  81. class BangladeshPlayers : Players{
  82.      
  83.     var runsAtLeague:Int?
  84.     var wicketsAtLeague:Int?
  85.     var matchesAtLeague:Int?
  86.    
  87.     var totalRuns:Int?
  88.     var totalWickets:Int?
  89.     var recordsAchieved:Records?       //empty class
  90.  
  91.  
  92.     //properties
  93.     var hasRecord:String{
  94.  
  95.         if let actualRecord = recordsAchieved{    
  96.             if recordsAchieved!.isCenturian != nil || recordsAchieved!.fiveWicketsHaul != nil{
  97.                 return "The player has an international record"
  98.             }
  99.             else {
  100.                 return "The player doesn't have any international record"
  101.             }
  102.         }
  103.         else{
  104.             return "The player doesn't have any international record yet"
  105.         }
  106.     }
  107.  
  108.  
  109.     convenience init(customName:String, customRuns:Int, customMatchesPlayed:Int, customWickets:Int) {      //convernience initializer
  110.         self.init()
  111.         fullName = customName
  112.         runs = customRuns
  113.         matchesPlayed = customMatchesPlayed
  114.         wickets = customWickets
  115.     }
  116.  
  117.     func leagueScore(_ runs:Int, _ match:Int, _ wicket:Int) {
  118.         runsAtLeague = runs
  119.         matchesAtLeague = match
  120.         wicketsAtLeague = wickets
  121.     }
  122.  
  123.  
  124.        
  125.     override func printInfo(){              //override is used to modify anything from the superclass
  126.         print(" International Records: ")
  127.         super.printInfo()           //used to access anything from superclass
  128.  
  129.         if let actual = matchesAtLeague{        //checking optionals
  130.             print("\r League Scores")
  131.             print("Scored \(runsAtLeague!) and took  \(wicketsAtLeague!) in  \(matchesAtLeague!) matches")
  132.         }
  133.         else{
  134.              print("\(fullName!) didn't play any league matches for yet")
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140.  
  141. let tamimRecords = Records(true, false)
  142. tamimRecords.getHighestRuns(128, 154, 160)
  143. tamimRecords.getManOfTheMatch("01-01-2020", "Bd vs ZIM", "05-08-2019", "BD vs IND")
  144.  
  145.  
  146. //player1
  147. let tamim = BangladeshPlayers(customName:"Tamim Iqbal" , customRuns:7500, customMatchesPlayed:125, customWickets:0)
  148. tamim.leagueScore(2000, 100, 1)
  149. tamim.recordsAchieved = tamimRecords
  150. tamim.printInfo()
  151. print(tamim.hasRecord)
  152. tamimRecords.printHighestRuns()
  153. tamimRecords.printManOfTheMatch()
  154. print("\n")
  155.  
  156.  
  157. //player2
  158. let miraz = BangladeshPlayers("Mehedi hasan Miraz", 1000, 40, 45)
  159. miraz.printInfo()
  160. print(miraz.hasRecord)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement