-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from PDA4-Phoenix/feat/stock-recommend
Feat/stock recommend
- Loading branch information
Showing
7 changed files
with
172 additions
and
79 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/com/bull4jo/kkanbustock/common/CustomParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.bull4jo.kkanbustock.common; | ||
|
||
import com.bull4jo.kkanbustock.stock.domain.MarketType; | ||
import com.bull4jo.kkanbustock.stock.domain.Stock; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CustomParser { | ||
|
||
public static Stock parseSingleJsonResponse(String jsonResponse) throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode responseJson = objectMapper.readTree(jsonResponse); | ||
|
||
JsonNode item = responseJson | ||
.path("response") | ||
.path("body") | ||
.path("items") | ||
.path("item") | ||
.get(0); | ||
|
||
float fltRt = CustomParser.formatFltRt(item.get("fltRt").asText()); | ||
Stock stock = Stock.builder() | ||
.srtnCd(item.get("srtnCd").asText()) | ||
.itmsNm(item.get("itmsNm").asText()) | ||
.mrktCtg(MarketType.valueOf(item.get("mrktCtg").asText())) | ||
.clpr(item.get("clpr").asInt()) | ||
.mkp(item.get("mkp").asInt()) | ||
.basDt(item.get("basDt").asInt()) | ||
.vs(item.get("vs").asInt()) | ||
.fltRt(fltRt) | ||
.trPrc(item.get("trPrc").asLong()) | ||
.mrktTotAmt(item.get("mrktTotAmt").asLong()) | ||
.build(); | ||
|
||
return stock; | ||
} | ||
|
||
public static List<Stock> parseJsonResponses(String jsonResponse) throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode responseJson = objectMapper.readTree(jsonResponse); | ||
JsonNode items = responseJson.path("response").path("body").path("items").path("item"); | ||
|
||
List<Stock> list = new ArrayList<>(); | ||
|
||
|
||
for (JsonNode itemNode : items) { | ||
float fltRt = CustomParser.formatFltRt(itemNode.get("fltRt").asText()); | ||
|
||
Stock stock = Stock.builder() | ||
.srtnCd(itemNode.get("srtnCd").asText()) | ||
.itmsNm(itemNode.get("itmsNm").asText()) | ||
.mrktCtg(MarketType.valueOf(itemNode.get("mrktCtg").asText())) | ||
.clpr(itemNode.get("clpr").asInt()) | ||
.mkp(itemNode.get("mkp").asInt()) | ||
.basDt(itemNode.get("basDt").asInt()) | ||
.vs(itemNode.get("vs").asInt()) | ||
.fltRt(fltRt) | ||
.trPrc(itemNode.get("trPrc").asLong()) | ||
.mrktTotAmt(itemNode.get("mrktTotAmt").asLong()) | ||
.build(); | ||
|
||
list.add(stock); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
public static float formatFltRt(String input) { | ||
try { | ||
// 문자열을 부동 소수점 숫자로 파싱 | ||
double parsedValue = Double.parseDouble(input); | ||
|
||
// 소수점 이하 두 자리까지 포맷 | ||
String formattedValue = String.format("%.2f", parsedValue); | ||
// 포맷된 문자열을 float로 파싱하여 반환 | ||
return Float.parseFloat(formattedValue); | ||
} catch (NumberFormatException e) { | ||
return 0.0f; | ||
} | ||
} | ||
|
||
} |
66 changes: 0 additions & 66 deletions
66
src/main/java/com/bull4jo/kkanbustock/common/ParseJsonResponse.java
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
src/main/java/com/bull4jo/kkanbustock/common/RecentWeekdayFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.bull4jo.kkanbustock.common; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
public class RecentWeekdayFinder { | ||
|
||
public static String findMostRecentWeekday() { | ||
Calendar calendar = Calendar.getInstance(); | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); | ||
String today = dateFormat.format(new Date()); | ||
|
||
while (true) { | ||
calendar.add(Calendar.DAY_OF_MONTH, -1); // 하루씩 이전 날짜로 이동 | ||
Date currentDate = calendar.getTime(); | ||
String currentDateStr = dateFormat.format(currentDate); | ||
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); | ||
|
||
// 주말(토요일 또는 일요일)이 아니면 반환 | ||
if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY) { | ||
return currentDateStr; | ||
} | ||
|
||
// 오늘과 같은 날짜면 루프 종료 | ||
if (currentDateStr.equals(today)) { | ||
break; | ||
} | ||
} | ||
|
||
return null; // 가장 최근 평일을 찾을 수 없을 경우 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/bull4jo/kkanbustock/stock/repository/RecommendStockRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package com.bull4jo.kkanbustock.stock.repository; | ||
|
||
import com.bull4jo.kkanbustock.member.domain.entity.InvestorType; | ||
import com.bull4jo.kkanbustock.stock.domain.RecommendStock; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface RecommendStockRepository extends JpaRepository<RecommendStock, String> { | ||
@Override | ||
Page<RecommendStock> findAll(Pageable pageable); | ||
|
||
Optional<Page<RecommendStock>> findByInvestorType(InvestorType investorType, Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters