Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- 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
- }
- func getHighestRuns(_ a:Int, _ b:Int, _ c:Int) {
- highestRuns.append(a) //inserting into array
- highestRuns += [b, c]
- }
- func printHighestRuns(){
- print("Three best innings: ")
- for i in 0...2{
- print(highestRuns[i])
- }
- }
- func getManOfTheMatch(_ date1:String , _ match1:String, _ date2:String, _ match2:String){
- manOfTheMatch[date1] = match1
- manOfTheMatch[date2] = match2
- }
- func printManOfTheMatch(){
- print("Recent man of the match of the player: ")
- for (date, match) in manOfTheMatch{
- print("In \(date) , got Man of the match in \(match) match")
- }
- }
- }
- //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("\(fullName!) didn't play any league matches for yet")
- }
- }
- }
- let tamimRecords = Records(true, false)
- tamimRecords.getHighestRuns(128, 154, 160)
- tamimRecords.getManOfTheMatch("01-01-2020", "Bd vs ZIM", "05-08-2019", "BD vs IND")
- //player1
- let tamim = BangladeshPlayers(customName:"Tamim Iqbal" , customRuns:7500, customMatchesPlayed:125, customWickets:0)
- tamim.leagueScore(2000, 100, 1)
- tamim.recordsAchieved = tamimRecords
- tamim.printInfo()
- print(tamim.hasRecord)
- tamimRecords.printHighestRuns()
- tamimRecords.printManOfTheMatch()
- print("\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