-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.swift
41 lines (33 loc) · 1.48 KB
/
App.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Foundation
class App {
private let coffeeShopService: CoffeeShopService
private let csvManager: CSVManager
private let coordinateValidator: CoordinateValidatorImpl
init(coffeeShopService: CoffeeShopService, csvManager: CSVManager, coordinateValidator: CoordinateValidatorImpl) {
self.coffeeShopService = coffeeShopService
self.csvManager = csvManager
self.coordinateValidator = coordinateValidator
}
func run(arguments: [String]) {
guard arguments.count == 4 else {
print("Please provide user coordinates (X Y) and a valid file path/url.")
exit(1)
}
guard let userXCoordinate = Double(arguments[1]),
let userYCoordinate = Double(arguments[2]),
coordinateValidator.isValid(x: userXCoordinate, y: userYCoordinate) else {
print("Invalid coordinates. Please provide valid numbers for the user coordinates (X Y) within the range (-180 to 180 for X & Y.")
exit(1)
}
let user = EUserFactory.createUser(xCoordinate: userXCoordinate, yCoordinate: userYCoordinate)
let coffeeShops = csvManager.loadCoffeeShops()
let closestCoffeeShops = coffeeShopService.findClosestCoffeeShop(user: user, coffeeShops: coffeeShops)
PrintHelper.printClosestCoffeeShops(closestCoffeeShops: closestCoffeeShops)
}
}
extension Double {
func rounded(toPlaces places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}