Skip to content

Commit be3f76c

Browse files
committed
Add AppKotlin.kt
1 parent e8da450 commit be3f76c

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'groovy'
33
id 'java'
4+
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
45
}
56

67
group 'lv.id.jc'
@@ -12,11 +13,22 @@ repositories {
1213

1314
dependencies {
1415
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
16+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
1517

1618
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
17-
testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
19+
testImplementation 'org.codehaus.groovy:groovy-all:3.0.9'
1820
}
1921

2022
test {
2123
useJUnitPlatform()
24+
}
25+
compileKotlin {
26+
kotlinOptions {
27+
jvmTarget = "1.8"
28+
}
29+
}
30+
compileTestKotlin {
31+
kotlinOptions {
32+
jvmTarget = "1.8"
33+
}
2234
}

src/main/groovy/lv/id/jc/App.groovy renamed to src/main/groovy/lv/id/jc/AppGroovy.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import algorithm.DijkstrasAlgorithm
55
import algorithm.Graph
66
import algorithm.SearchAlgorithm
77

8-
class App {
9-
private static final Graph<String> complex = new Graph<>(Map.of(
8+
class AppGroovy {
9+
private static final Graph<String> COMPLEX_GRAPH = new Graph<>(Map.of(
1010
"A", Map.of("B", 5, "H", 2),
1111
"B", Map.of("A", 5, "C", 7),
1212
"C", Map.of("B", 7, "D", 3, "G", 4),
@@ -20,11 +20,11 @@ class App {
2020
private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>()
2121

2222
static void main(String[] args) {
23-
System.out.println(complex)
23+
System.out.println(COMPLEX_GRAPH)
2424

25-
printRoute(complex, "D", "C")
26-
printRoute(complex, "A", "G")
27-
printRoute(complex, "D", "H")
25+
printRoute(COMPLEX_GRAPH, "D", "C")
26+
printRoute(COMPLEX_GRAPH, "A", "G")
27+
printRoute(COMPLEX_GRAPH, "D", "H")
2828
}
2929

3030
private static void printRoute(final Graph<String> graph, final String source, final String target) {

src/main/java/Main.java renamed to src/main/java/lv/id/jc/AppJava.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
package lv.id.jc;
2+
13
import algorithm.BreadthFirstSearch;
24
import algorithm.DijkstrasAlgorithm;
35
import algorithm.Graph;
46
import algorithm.SearchAlgorithm;
57

68
import java.util.Map;
79

8-
public class Main {
9-
private static final Graph<String> complex = new Graph<>(Map.of(
10+
public class AppJava {
11+
private static final Graph<String> COMPLEX_GRAPH = new Graph<>(Map.of(
1012
"A", Map.of("B", 5, "H", 2),
1113
"B", Map.of("A", 5, "C", 7),
1214
"C", Map.of("B", 7, "D", 3, "G", 4),
@@ -20,13 +22,11 @@ public class Main {
2022
private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>();
2123

2224
public static void main(String[] args) {
23-
System.out.println(complex);
24-
25-
printRoute(complex, "D", "C");
26-
27-
printRoute(complex, "A", "G");
25+
System.out.println(COMPLEX_GRAPH);
2826

29-
printRoute(complex, "D", "H");
27+
printRoute(COMPLEX_GRAPH, "D", "C");
28+
printRoute(COMPLEX_GRAPH, "A", "G");
29+
printRoute(COMPLEX_GRAPH, "D", "H");
3030
}
3131

3232
private static void printRoute(final Graph<String> graph, final String source, final String target) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package lv.id.jc
2+
3+
import algorithm.BreadthFirstSearch
4+
import algorithm.DijkstrasAlgorithm
5+
import algorithm.Graph
6+
import algorithm.SearchAlgorithm
7+
8+
class AppKotlin {
9+
10+
companion object {
11+
@JvmStatic
12+
private val complexGraph = Graph(
13+
mapOf(
14+
"A" to mapOf("B" to 5, "H" to 2),
15+
"B" to mapOf("A" to 5, "C" to 7),
16+
"C" to mapOf("B" to 7, "D" to 3, "G" to 4),
17+
"D" to mapOf("C" to 20, "E" to 4),
18+
"E" to mapOf("F" to 5),
19+
"F" to mapOf("G" to 6),
20+
"G" to mapOf("C" to 4),
21+
"H" to mapOf("G" to 3)
22+
)
23+
)
24+
25+
@JvmStatic
26+
private val fastest: SearchAlgorithm<String> = DijkstrasAlgorithm()
27+
28+
@JvmStatic
29+
private val shortest: SearchAlgorithm<String> = BreadthFirstSearch()
30+
31+
@JvmStatic
32+
fun main(args: Array<String>) {
33+
println(complexGraph)
34+
printRoute(complexGraph, "D", "C")
35+
printRoute(complexGraph, "A", "G")
36+
printRoute(complexGraph, "D", "H")
37+
}
38+
39+
@JvmStatic
40+
private fun printRoute(graph: Graph<String>, source: String, target: String) {
41+
val routeOne = shortest.findPath(graph, source, target)
42+
val routeTwo = fastest.findPath(graph, source, target)
43+
44+
val message: String = String.format(
45+
"%nFind the path from %s to %s%n" +
46+
" - the shortest take %.0f min and the path is %s%n" +
47+
" - the fastest take %.0f min and the path is %s",
48+
source, target,
49+
graph.getDistance(routeOne), routeOne,
50+
graph.getDistance(routeTwo), routeTwo
51+
)
52+
println(message)
53+
}
54+
}
55+
56+
57+
}

0 commit comments

Comments
 (0)