Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

진하늘 과제1 #18

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions hn/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.5")
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

implementation 'org.apache.commons:commons-math3:3.6.1'
}

tasks.named('test') {
Expand Down
2 changes: 2 additions & 0 deletions hn/src/main/java/com/example/hn/HnApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class HnApplication {

public static void main(String[] args) {
Expand Down
13 changes: 13 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/api/YkApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.hn.homework1.api;

import com.example.hn.homework1.response.YkServiceResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@FeignClient(name="homework1-yk", url="http://localhost:8081")
public interface YkApi {
@GetMapping(value="/api/cal/{x}")
YkServiceResponse getAnswer(@PathVariable("x")int x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.hn.homework1.controller;

import com.example.hn.homework1.response.ABResponse;
import com.example.hn.homework1.response.ApiResponse;
import com.example.hn.homework1.service.HomeworkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class HomeworkController {

private final HomeworkService homeworkService;

@GetMapping("/hn/homework1")
public ApiResponse getAnswer(){

ABResponse response=homeworkService.getAnswer();
return ApiResponse.ok(response);
}
}
18 changes: 18 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/response/ABResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.hn.homework1.response;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class ABResponse {
private int a;
private int b;

public static ABResponse of(int a, int b) {
ABResponse abResponse=new ABResponse();
kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved
abResponse.setA(a);
abResponse.setB(b);
return abResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.hn.homework1.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
public class ApiResponse<T> {

private final String message;
private final int code;
private final T data;

public static <T> ApiResponse<T> ok(T data){
return new ApiResponse<>(
"성공",
200,
data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.hn.homework1.response;

import lombok.*;

@Getter
@AllArgsConstructor
public class YkServiceResponse {
private int answer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.hn.homework1.service;

import com.example.hn.homework1.response.ABResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.print.attribute.IntegerSyntax;
import java.util.Arrays;
import java.util.stream.IntStream;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.SingularMatrixException;


@Service
@RequiredArgsConstructor
@Slf4j
public class HomeworkService {

private final YkService ykService;

public ABResponse getAnswer() {
int x1 = 2;
int x2 = 3;
int answer1 = ykService.getAnswer(x1);
int answer2 = ykService.getAnswer(x2);

double[][] matrixData = {
{x1 * x1, x1},
{x2 * x2, x2}
};
log.debug(printMatrix(matrixData));

double[] resultData = {answer1, answer2};


RealMatrix matrix = new Array2DRowRealMatrix(matrixData);
RealMatrix resultMatrix = new Array2DRowRealMatrix(resultData);


RealMatrix inverseMatrix = MatrixUtils.inverse(matrix);
log.debug(printMatrix(inverseMatrix.getData()));


RealMatrix solutionMatrix = inverseMatrix.multiply(resultMatrix);


double[] solution = solutionMatrix.getColumn(0);
int a = (int) Math.round(solution[0]);
int b = (int) Math.round(solution[1]);
log.debug("a:" + a + ", b:" + b);
kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved


return ABResponse.of(a, b);

kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved
}

public String printMatrix(double[][] matrix) {
kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved
StringBuilder str = new StringBuilder();
IntStream.range(0, matrix.length)
.forEach(e -> str.append(Arrays.toString(matrix[e]) + "\n"));

return str.toString();
}
}
20 changes: 20 additions & 0 deletions hn/src/main/java/com/example/hn/homework1/service/YkService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.hn.homework1.service;

import com.example.hn.homework1.api.YkApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
@Slf4j
public class YkService {

private final YkApi ykApi;

public int getAnswer(int x) {
int answer=ykApi.getAnswer(x).getAnswer();
kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved
log.debug("answer="+answer);
kimyoungi99 marked this conversation as resolved.
Show resolved Hide resolved
return answer;
}
}
2 changes: 1 addition & 1 deletion hn/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

server.port=8082