-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.kt
More file actions
133 lines (118 loc) · 3.44 KB
/
Assignment.kt
File metadata and controls
133 lines (118 loc) · 3.44 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.acme.snapgreen
import org.junit.Test
import org.junit.Assert.*
/**
* Destin Estrela
*
* Unit testing framework: Junit is also compatible with kotlin and runs on dev machine.
*/
class TestDrivenDevelopmentAssignment {
@Test
fun test_greet_1() {
assertEquals("Hello, Bob.", greet("Bob"))
}
@Test
fun test_greet_2() {
assertEquals("Hello, my friend.", greet(null))
}
@Test
fun test_greet_3() {
assertEquals("HELLO JERRY!", greet("JERRY"))
}
@Test
fun test_greet_4() {
assertEquals("Hello, Jill and Jane.", greet("Jill", "Jane"))
}
@Test
fun test_greet_5() {
assertEquals(
"Hello, Amy, Brian, and Charlotte.",
greet("Amy", "Brian", "Charlotte")
)
}
@Test
fun test_greet_6() {
assertEquals(
"Hello, Amy and Charlotte. AND HELLO BRIAN!",
greet("Amy", "BRIAN", "Charlotte")
)
}
@Test
fun test_greet_7() {
assertEquals(
"Hello, Bob, Charlie, and Dianne.",
greet("Bob", "Charlie, Dianne")
)
}
@Test
fun test_greet_8() {
assertEquals(
"Hello, Bob and Charlie, Dianne.",
greet("Bob", "\"Charlie, Dianne\"")
)
}
private fun isAllUpper(string: String?): Boolean {
var isUpper = true
string?.toCharArray()?.forEach { c: Char ->
if (!c.isUpperCase()) isUpper = false
}
return isUpper
}
private fun greet(vararg strings: String?): String {
var ret = ""
var ret2 = ""
val lowerList = ArrayList<String?>()
var upperString = ""
for (string in strings) {
if (string != null) {
when {
string.contains("\"") -> {
lowerList.add(string.substring(1 until string.length - 1))
}
string.contains(',') -> {
var split = string.split(", ")
for (splitString in split) {
lowerList.add(splitString)
}
}
isAllUpper(string) -> {
upperString = string
}
else -> {
lowerList.add(string)
}
}
}
}
when {
strings[0].isNullOrEmpty() -> {
ret = "Hello, my friend."
}
strings.size == 1 -> {
ret = if (isAllUpper(strings[0])) {
"HELLO " + strings[0] + "!"
} else {
"Hello, " + strings[0] + "."
}
}
else -> {
if (lowerList.size == 2) {
ret = "Hello, " + lowerList[0] + " and " + lowerList[1] + "."
} else {
ret = "Hello, "
for (i in lowerList.indices) {
ret += if (i != lowerList.size - 1) {
lowerList[i] + ", "
} else {
"and " + lowerList[i] + "."
}
}
}
if (upperString.isNotEmpty()) {
ret2 = " AND HELLO $upperString!"
}
}
}
return "$ret$ret2"
}
}