Skip to content

Commit 2371fba

Browse files
committed
TriangleClassification added
1 parent f6c5716 commit 2371fba

File tree

9 files changed

+225
-1
lines changed

9 files changed

+225
-1
lines changed
Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1-
TODO replace me
1+
<!-- TODO replace me -->
2+
3+
<?xml version="1.0" encoding="UTF-8"?>
4+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.4.3</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
<groupId>com.example</groupId>
14+
<artifactId>springmvc-docker</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<name>SpringMVCDocker</name>
17+
<description>SpringMVCDocker</description>
18+
<url/>
19+
<licenses>
20+
<license/>
21+
</licenses>
22+
<developers>
23+
<developer/>
24+
</developers>
25+
<scm>
26+
<connection/>
27+
<developerConnection/>
28+
<tag/>
29+
<url/>
30+
</scm>
31+
<properties>
32+
<java.version>21</java.version>
33+
</properties>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example.springmvcdocker;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
@Controller
11+
public class FormController {
12+
13+
@GetMapping("/index")
14+
public String showForm() {
15+
return "index";
16+
}
17+
18+
@PostMapping("/process")
19+
public String processForm(
20+
@RequestParam("a") int a,
21+
@RequestParam("b") int b,
22+
@RequestParam("c") int c)
23+
{
24+
int value = TriangleClassification.classify(a, b, c);
25+
String triangleType = getTriangleType(value);
26+
return "redirect:/" + triangleType.toLowerCase().replace(" ", "-");
27+
}
28+
29+
@RequestMapping("/not-a-triangle")
30+
public String notATriangle(Model model) {
31+
model.addAttribute("output", "The input does not form a triangle.");
32+
return "notTriangle";
33+
}
34+
35+
@RequestMapping("/scalene-triangle")
36+
public String scaleneTriangle(Model model) {
37+
model.addAttribute("output", "This is a Scalene triangle.");
38+
return "scalene";
39+
}
40+
41+
@RequestMapping("/isosceles-triangle")
42+
public String isoscelesTriangle(Model model) {
43+
model.addAttribute("output", "This is an Isosceles triangle.");
44+
return "isosceles";
45+
}
46+
47+
@RequestMapping("/equilateral-triangle")
48+
public String equilateralTriangle(Model model) {
49+
model.addAttribute("output", "This is an Equilateral triangle.");
50+
return "equilateral";
51+
}
52+
53+
public static String getTriangleType(int value) {
54+
switch (value) {
55+
case 0: return "Not a triangle";
56+
case 1: return "Scalene triangle";
57+
case 2: return "Isosceles triangle";
58+
case 3: return "Equilateral triangle";
59+
default: return "Invalid input";
60+
}
61+
}
62+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.springmvcdocker;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringMvcDockerApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(SpringMvcDockerApplication.class, args);
10+
}
11+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.springmvcdocker;
2+
3+
public class TriangleClassification {
4+
5+
public static int classify(int a, int b, int c) {
6+
7+
if (a <= 0 || b <= 0 || c <= 0) {
8+
return 0;
9+
}
10+
11+
if (a == b && b == c) {
12+
return 3;
13+
}
14+
15+
int max = Math.max(a, Math.max(b, c));
16+
17+
if ((max == a && max - b - c >= 0) ||
18+
(max == b && max - a - c >= 0) ||
19+
(max == c && max - a - b >= 0)) {
20+
return 0;
21+
}
22+
23+
if (a == b || b == c || a == c) {
24+
return 2;
25+
} else {
26+
return 1;
27+
}
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Equilateral Triangle</title>
5+
</head>
6+
<body>
7+
<h2><span th:text="${output}"></span></h2>
8+
<a href="/index"> << Go back</a>
9+
</body>
10+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>SUT</title>
5+
</head>
6+
<body>
7+
<h1>Spring MVC test app</h1>
8+
<h2>Triangle classification</h2>
9+
<h2>Enter side values:</h2>
10+
<form action="/process" method="post">
11+
<label for="a">Side A:</label>
12+
<input type="number" id="a" name="a" required><br><br>
13+
14+
<label for="b">Side B:</label>
15+
<input type="number" id="b" name="b" required><br><br>
16+
17+
<label for="c">Side C:</label>
18+
<input type="number" id="c" name="c" required><br><br>
19+
20+
<button type="submit">Submit</button>
21+
</form>
22+
</body>
23+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Isosceles Triangle</title>
5+
</head>
6+
<body>
7+
<h2><span th:text="${output}"></span></h2>
8+
<a href="/index"> << Go back</a>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Not a Triangle</title>
5+
</head>
6+
<body>
7+
<h2><span th:text="${output}"></span></h2>
8+
<a href="/index"> << Go back</a>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Scalene Triangle</title>
5+
</head>
6+
<body>
7+
<h2><span th:text="${output}"></span></h2>
8+
<a href="/index"> << Go back</a>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)