Skip to content

Commit

Permalink
Refactor: 거리, 시간 관련 변환 값을 constant값으로 변경 (#201)
Browse files Browse the repository at this point in the history
* Refactor: 시간관련 변환 값 constant 값으로 변경

* Refactor: 거리 관련 변환 값 constant 값으로 변경
  • Loading branch information
hee9841 committed Sep 18, 2024
1 parent d6d74b6 commit 4496812
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.time.OffsetDateTime;
import java.util.List;

import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;

@Service
@RequiredArgsConstructor
public class ScaleService {
Expand Down Expand Up @@ -70,7 +72,8 @@ private ScaleCoursesResponse.CurrentCourse calculateCurrentScaleLeftMeter(
.mapToInt(log -> log.scale().sizeMeter())
.sum();

double remainingKm = (currentScale.scale().sizeMeter() + achievedCourseMeterSum - memberRunMeterSum) / 1000.0;
double remainingKm =
(currentScale.scale().sizeMeter() + achievedCourseMeterSum - memberRunMeterSum) / METERS_IN_A_KILOMETER;

String message = String.format("%s까지 %.1fkm 남았어요!", currentScale.scale().endName(), remainingKm);

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/dnd/runus/domain/challenge/Challenge.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dnd.runus.domain.challenge;

import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_HOUR;
import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_MINUTE;

public record Challenge(long challengeId, String name, int expectedTime, String imageUrl, ChallengeType challengeType) {

public Challenge(long challengeId, String name, String imageUrl, ChallengeType challengeType) {
Expand All @@ -11,8 +14,8 @@ public boolean isDefeatYesterdayChallenge() {
}

public String formatExpectedTime() {
int hour = expectedTime / 3600;
int minute = (expectedTime % 3600) / 60;
int hour = expectedTime / SECONDS_PER_HOUR;
int minute = (expectedTime % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
StringBuilder sb = new StringBuilder();

if (hour != 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/dnd/runus/domain/common/Pace.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_MINUTE;

/**
* 러닝 페이스를 나타내는 클래스
*
Expand All @@ -11,8 +13,8 @@
*/
public record Pace(int minute, int second) {
public static Pace ofSeconds(int seconds) {
int minute = seconds / 60;
int second = seconds % 60;
int minute = seconds / SECONDS_PER_MINUTE;
int second = seconds % SECONDS_PER_MINUTE;
return new Pace(minute, second);
}

Expand All @@ -29,6 +31,6 @@ public String getPace() {
}

public int toSeconds() {
return minute * 60 + second;
return minute * SECONDS_PER_MINUTE + second;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.text.DecimalFormat;

import static com.dnd.runus.domain.challenge.GoalMetricType.DISTANCE;
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;
import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_HOUR;
import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_MINUTE;
import static com.dnd.runus.global.constant.RunningResultComment.FAILURE;
import static com.dnd.runus.global.constant.RunningResultComment.SUCCESS;

Expand All @@ -26,7 +29,7 @@ public GoalAchievement(RunningRecord runningRecord, GoalMetricType goalMetricTyp

public String getTitle() {
if (goalMetricType == DISTANCE) {
return KILO_METER_FORMATTER.format(achievementValue / 1000.0) + " 달성";
return KILO_METER_FORMATTER.format(achievementValue / METERS_IN_A_KILOMETER) + " 달성";
}

return formatSecondToKoreanHHMM(achievementValue) + " 달성";
Expand All @@ -41,8 +44,8 @@ public String getIconUrl() {
}

private String formatSecondToKoreanHHMM(int second) {
int hour = second / 3600;
int minute = (second % 3600) / 60;
int hour = second / SECONDS_PER_HOUR;
int minute = (second % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
StringBuilder sb = new StringBuilder();

if (hour != 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/dnd/runus/domain/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.text.DecimalFormat;

import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;

public record Level(long levelId, int expRangeStart, int expRangeEnd, String imageUrl) {
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("0.##km");

public static String formatExp(int exp) {
return KILO_METER_FORMATTER.format(exp / 1000.0);
return KILO_METER_FORMATTER.format(exp / METERS_IN_A_KILOMETER);
}

public static String formatLevelName(long level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ private MetricsConversionFactor() {}

public static final int SECONDS_PER_HOUR = 3600;
public static final int SECONDS_PER_MINUTE = 60;
public static final int MINUTE_PER_HOUR = 60;

public static final double METERS_IN_A_KILOMETER = 1000.0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.time.format.DateTimeFormatter;
import java.time.*;

import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_HOUR;
import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_MINUTE;
import static com.dnd.runus.global.constant.TimeConstant.*;
import static java.time.format.DateTimeFormatter.ofPattern;

Expand Down Expand Up @@ -55,9 +57,9 @@ public static class CustomDurationSerializer extends JsonSerializer<Duration> {
public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider)
throws IOException {
long totalSeconds = duration.getSeconds();
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
long hours = totalSeconds / SECONDS_PER_HOUR;
long minutes = (totalSeconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
long seconds = totalSeconds % SECONDS_PER_MINUTE;
// HH:mm:ss or HHH:mm:ss
String formattedDuration =
String.format("%s:%02d:%02d", (hours < 10 ? "0" + hours : hours), minutes, seconds);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dnd.runus.presentation.v1.running.dto.response;

import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;

import io.swagger.v3.oas.annotations.media.Schema;

import java.text.DecimalFormat;
Expand All @@ -17,6 +19,6 @@ public record RunningRecordMonthlySummaryResponse(
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("0.##km");

public RunningRecordMonthlySummaryResponse(int monthValue, int monthlyTotalMeter, String nextLevelName, String nextLevelKm) {
this(monthValue + "월", KILO_METER_FORMATTER.format(monthlyTotalMeter / 1000.0), nextLevelName, nextLevelKm);
this(monthValue + "월", KILO_METER_FORMATTER.format(monthlyTotalMeter / METERS_IN_A_KILOMETER), nextLevelName, nextLevelKm);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dnd.runus.presentation.v1.scale.dto;

import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;

import io.swagger.v3.oas.annotations.media.Schema;

import java.text.DecimalFormat;
Expand All @@ -24,7 +26,7 @@ public Info(
int totalCourses,
int totalMeter
) {
this(totalCourses, KILO_METER_FORMATTER.format(totalMeter / 1000.0));
this(totalCourses, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER));
}
}

Expand All @@ -41,7 +43,7 @@ public AchievedCourse(
int totalMeter,
LocalDate achievedAt
) {
this(name, KILO_METER_FORMATTER.format(totalMeter / 1000.0), achievedAt);
this(name, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER), achievedAt);
}
}

Expand All @@ -61,7 +63,7 @@ public CurrentCourse(
int achievedMeter,
String message
) {
this(name, KILO_METER_FORMATTER.format(totalMeter / 1000.0), achievedMeter + "m", message);
this(name, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER), achievedMeter + "m", message);
}
}
}

0 comments on commit 4496812

Please sign in to comment.