Created
September 2, 2023 16:54
-
-
Save zippi-MD/c6df65d706ffb4b64216de387ed96b6e to your computer and use it in GitHub Desktop.
Protocol example base code
This file contains 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
import Foundation | |
// Protocolos | |
protocol TabularDataSource { | |
var numberOfRows: Int { get } | |
var numberOfColumns: Int { get } | |
func label(forColumn column: Int) -> String | |
func itemFor(row: Int, column: Int) -> String | |
} | |
func printTable(_ dataSource: TabularDataSource) { | |
var headerRow = "|" | |
var columnWidths = [Int]() | |
for index in 0..<dataSource.numberOfColumns { | |
let columnLabel = dataSource.label(forColumn: index) | |
let columnHeader = "\(columnLabel) |" | |
headerRow += columnHeader | |
columnWidths.append(columnLabel.count) | |
} | |
print(headerRow) | |
for index in 0..<dataSource.numberOfRows { | |
// Start output string | |
var output = "|" | |
// row = ["name", "age", "age of experience"] | |
for columnIndex in 0..<dataSource.numberOfColumns { | |
let item = dataSource.itemFor(row: index, column: columnIndex) | |
let paddingNeeded = columnWidths[columnIndex] - item.count | |
let padding = repeatElement(" ", count: paddingNeeded).joined(separator: "") | |
output += "\(padding)\(item) |" | |
} | |
print(output) | |
} | |
} | |
struct Person { | |
let name: String | |
let age: Int | |
let yearsOfExperience: Int | |
} | |
struct Department: TabularDataSource { | |
let name: String | |
var people = [Person]() | |
init(name: String) { | |
self.name = name | |
} | |
mutating func add(_ person: Person) { | |
people.append(person) | |
} | |
var numberOfRows: Int { people.count } | |
var numberOfColumns: Int { 3 } | |
func label(forColumn column: Int) -> String { | |
let columnHeader: String | |
switch column { | |
case 0: | |
columnHeader = "Employee name" | |
case 1: | |
columnHeader = "Age" | |
case 2: | |
columnHeader = "Years of experience" | |
default: fatalError("Invalid column header index!") | |
} | |
return columnHeader | |
} | |
func itemFor(row: Int, column: Int) -> String { | |
let person = people[row] | |
switch column { | |
case 0: return person.name | |
case 1: return String(person.age) | |
case 2: return String(person.yearsOfExperience) | |
default: fatalError("Invalid column for person!") | |
} | |
} | |
} | |
var department = Department(name: "Engineering") | |
department.add(Person(name: "Eva", age: 30, yearsOfExperience: 6)) | |
department.add(Person(name: "Saleh", age: 40, yearsOfExperience: 18)) | |
department.add(Person(name: "Amit", age: 50, yearsOfExperience: 20)) | |
printTable(department) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment