Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions src/main/java/com/apc/entjavamid40/MyController.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package com.apc.entjavamid40;


import com.apc.entjavamid40.builder.JSONBuilder;
import com.apc.entjavamid40.service.UnitConversionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("convert")
public class MyController {

@Autowired
private UnitConversionService conversionService;

@Autowired
private JSONBuilder jsonBuilder;

/*
* /convert/1/in/cm
*
* out put should be found in sample_out.json
* */

@GetMapping("/{value}/{unit1}/{unit2}/")
public String convert() {
return "";
}

List<String> allowedUnits = Arrays.asList("mm","cm","in","m","ft");

@GetMapping("/{value}/{unit1}/{unit2}")
public Map<String, Object> convert(@PathVariable double value,
@PathVariable String unit1,
@PathVariable String unit2) {
if (!allowedUnits.contains(unit1) || !allowedUnits.contains(unit2)) {
throw new IllegalArgumentException("Invalid unit. Allowed units are: " + allowedUnits);
}

double convertedValue = conversionService.convert(unit1, unit2, value);
return jsonBuilder.buildResponse(unit1, value, unit2, convertedValue);
}

}
27 changes: 27 additions & 0 deletions src/main/java/com/apc/entjavamid40/builder/JSONBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.apc.entjavamid40.builder;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class JSONBuilder {

public Map<String, Object> buildResponse(String unit1, double value, String unit2, double convertedValue) {
Map<String, Object> from = new HashMap<>();
from.put("unit", unit1);
from.put("length", value);

Map<String, Object> to = new HashMap<>();
to.put("unit", unit2);
to.put("length", convertedValue);

Map<String, Object> response = new HashMap<>();
response.put("from", from);
response.put("to", to);

return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.apc.entjavamid40.service;

import org.springframework.stereotype.Service;

@Service
public class UnitConversionService {

//Convert Cases
public double convert(String unit1, String unit2, double value) {
return switch (unit1) {
case "mm" -> fromMm(unit2, value);
case "cm" -> fromCm(unit2, value);
case "in" -> fromIn(unit2, value);
case "m" -> fromM(unit2, value);
case "ft" -> fromFt(unit2, value);
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
//Unit 1 Conversion Cases
private double fromCm(String unit2, double value) {
return switch (unit2) {
case "mm" -> value * 10;
case "in" -> value * 0.393701;
case "m" -> value / 100;
case "ft" -> value * 0.0328084;
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
private double fromIn(String unit2, double value) {
return switch (unit2) {
case "mm" -> value * 25.4;
case "cm" -> value * 2.54;
case "m" -> value * 0.0254;
case "ft" -> value / 12;
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
private double fromM(String unit2, double value) {
return switch (unit2) {
case "mm" -> value * 1000;
case "cm" -> value * 100;
case "in" -> value * 39.3701;
case "ft" -> value * 3.28084;
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
private double fromFt(String unit2, double value) {
return switch (unit2) {
case "mm" -> value * 304.8;
case "cm" -> value * 30.48;
case "in" -> value * 12;
case "m" -> value * 0.3048;
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
private double fromMm(String unit2, double value) {
return switch (unit2) {
case "cm" -> value / 10;
case "in" -> value * 0.0393701;
case "m" -> value / 1000;
case "ft" -> value * 0.00328084;
default -> throw new IllegalArgumentException(unit2 + " is not supported:");
};
}
}
1 change: 1 addition & 0 deletions target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=entjavamid40
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.