Skip to content

Commit

Permalink
Merge pull request #50 from Imsyp/main
Browse files Browse the repository at this point in the history
Feat: 가격 정보 저장 로직 수정
  • Loading branch information
Imsyp authored Oct 14, 2024
2 parents 1bbac60 + 526cae8 commit e0552d0
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 18 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified server/build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
28 changes: 10 additions & 18 deletions server/src/main/java/com/swdc/server/service/PriceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PriceService {
*
* file system에서 "{fileSystemPath}/platform/category_name/product_id.txt"에 해당하는 product의 가격 정보를 반환
*
* 가격이 변경되는 시점 & 하루가 바뀌는 시점의 가격 정보만 가져옴
*
*/
public Price getProductDetails(String platform, String category_name, String product_id) {
Path fileSystemPath = Paths.get(BASE_PATH);
Expand All @@ -52,28 +54,24 @@ public Price getProductDetails(String platform, String category_name, String pro
try (BufferedReader bufferedReader = Files.newBufferedReader(productPath)) {
String previousLine = null;
String currentLine;
String previousDate = null;

while ((currentLine = bufferedReader.readLine()) != null) {
String[] parts = currentLine.split(",");
String dateTime = parts[0] + "," + parts[1];
int currentPrice = Integer.parseInt(parts[2]);
String currentDate = parts[0];

// 이전 라인의 가격 정보와 비교
if (previousLine != null) {
String[] prevParts = previousLine.split(",");
int previousPrice = Integer.parseInt(prevParts[2]);

// 가격이 달라졌을 때만 기록
if (previousPrice != currentPrice) {
prices.add(Map.of(dateTime, currentPrice));
}
} else {
// 첫 번째 라인은 비교할 이전 가격이 없으므로 기록
if (previousLine == null || !previousLine.split(",")[2].equals(parts[2])) {
prices.add(Map.of(dateTime, currentPrice));
}

if (previousDate != null && !currentDate.equals(previousDate) && parts[1].equals("00:00")) {
prices.add(Map.of(dateTime, currentPrice));
}

// 현재 라인을 이전 라인으로 업데이트
previousLine = currentLine;
previousDate = currentDate;
}
} catch (FileNotFoundException e) {
System.err.println("File not found: " + productPath + ". Error: " + e.getMessage());
Expand Down Expand Up @@ -101,7 +99,6 @@ public Price getProductDetailsWithoutCategory(String platform, String product_id
List<Map<String, Integer>> allPrices = new ArrayList<>();

try {
// Traverse all directories and files under the platform directory
Files.walk(basePath)
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().equals(product_id + ".txt"))
Expand All @@ -116,21 +113,16 @@ public Price getProductDetailsWithoutCategory(String platform, String product_id
String dateTime = parts[0] + "," + parts[1];
int currentPrice = Integer.parseInt(parts[2]);

// 이전 라인의 가격과 비교
if (previousLine != null) {
String[] prevParts = previousLine.split(",");
int previousPrice = Integer.parseInt(prevParts[2]);

// 가격이 변동되었을 때만 기록
if (previousPrice != currentPrice) {
allPrices.add(Map.of(dateTime, currentPrice));
}
} else {
// 첫 번째 라인은 비교 대상이 없으므로 기록
allPrices.add(Map.of(dateTime, currentPrice));
}

// 현재 라인을 이전 라인으로 설정
previousLine = currentLine;
}
} catch (IOException e) {
Expand Down

0 comments on commit e0552d0

Please sign in to comment.