-
Notifications
You must be signed in to change notification settings - Fork 0
/
A+B - 7.swift
74 lines (63 loc) · 2.08 KB
/
A+B - 7.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Foundation
enum InputError: Error {
case onePositiveIntegerOnly
case twoPositiveIntegersSeparatedBySpace
case invalidNumberRange
}
extension InputError: LocalizedError {
var errorMessages: String {
switch self {
case .onePositiveIntegerOnly:
return "양의 정수 1개만 입력해주세요."
case .twoPositiveIntegersSeparatedBySpace:
return "공백으로 구분된 양의 정수 2개를 입력해주세요"
case .invalidNumberRange:
return "첫 번째 숫자는 0보다 커야 하며 두 번째 숫자는 10보다 작아야 합니다."
}
}
}
class InputManager {
func getNumberOfTestCases() throws -> Int {
guard let readLine = readLine(),
let numberOfTestCases = Int(readLine), numberOfTestCases > 0
else {
throw InputError.onePositiveIntegerOnly
}
return numberOfTestCases
}
func readTwoNumbers() throws -> (Int, Int) {
guard let readLine = readLine()?.split(separator: " ").compactMap({ Int($0) }),
readLine.count == 2 else {
throw InputError.twoPositiveIntegersSeparatedBySpace
}
guard readLine[0] > 0 && readLine[1] < 10 else {
throw InputError.invalidNumberRange
}
return (readLine[0], readLine[1])
}
}
class Calculator {
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
}
class Program {
private let inputManager = InputManager()
private let calculator = Calculator()
func run() {
do {
let testCase = try inputManager.getNumberOfTestCases()
for i in 1...testCase {
let (a, b) = try inputManager.readTwoNumbers()
let result = calculator.add(a, b)
print("Case #\(i): \(result)")
}
} catch let error as InputError {
print(error.errorMessages)
} catch {
print("알 수 없는 오류가 발생했습니다.")
}
}
}
let program = Program()
program.run()