Skip to content

Commit

Permalink
Merge pull request #25 from GDG-Hackathon-77ia/feat/addPetType
Browse files Browse the repository at this point in the history
faet: add pointlog with pet growthButtonType
  • Loading branch information
GitJIHO authored Nov 14, 2024
2 parents 52b6cfb + 89b189b commit 8775ecc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/gdg/kkia/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gdg.kkia.member.entity;

import com.gdg.kkia.common.exception.BadRequestException;
import com.gdg.kkia.pet.dto.PointAndPointLogType;
import com.gdg.kkia.pet.entity.Pet;
import com.gdg.kkia.point.entity.PointLog;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
Expand Down Expand Up @@ -43,16 +45,32 @@ public void earnPoint(int point) {
this.point += point;
}

public int consumePoint(Pet.GrowthButton growthButton) {
public PointAndPointLogType consumePoint(Pet.GrowthButton growthButton) {
int pointToConsume = pointToConsume(growthButton);
if (this.point - pointToConsume < 0) {
throw new BadRequestException("보유 포인트보다 많은 포인트를 소비할 수 없습니다.");
}
this.point -= pointToConsume;
return pointToConsume;
PointLog.Type pointLogType = convertGrowthButtonToPointLogType(growthButton);
return new PointAndPointLogType(pointToConsume, pointLogType);
}

private int pointToConsume(Pet.GrowthButton growthButton) {
return convertByGrowthButton(growthButton, NORMAL_BUTTON_PRICE, PREMIUM_BUTTON_PRICE, SUPER_BUTTON_PRICE);
}

private PointLog.Type convertGrowthButtonToPointLogType(Pet.GrowthButton growthButton) {
switch (growthButton) {
case WATER -> {
return PointLog.Type.GROWTH_WATER;
}
case SUN -> {
return PointLog.Type.GROWTH_SUN;
}
case NUTRIENT -> {
return PointLog.Type.GROWTH_NUTRIENT;
}
default -> throw new BadRequestException("팻 성장 버튼 이름이 올바르지 않습니다.");
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/gdg/kkia/pet/dto/PointAndPointLogType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.gdg.kkia.pet.dto;

import com.gdg.kkia.point.entity.PointLog;

public record PointAndPointLogType(
int point,
PointLog.Type pointLogType
) {
}
28 changes: 14 additions & 14 deletions src/main/java/com/gdg/kkia/pet/entity/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class Pet {
private final int INITIAL_EXPERIENCE = 0;
private final int MAX_LEVEL = 3;
private final int MAX_EXPERIENCE = 100;
private final int NORMAL_PLUS = 1;
private final int PREMIUM_PLUS = 12;
private final int SUPER_PLUS = 30;
private final int WATER_PLUS = 1;
private final int SUN_PLUS = 12;
private final int NUTRIENT_PLUS = 30;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -49,16 +49,16 @@ public Pet(String name, Member member) {
isMaxGrowth = false;
}

public static int convertByGrowthButton(GrowthButton growthButton, int normalPlus, int premiumPlus, int superPlus) {
public static int convertByGrowthButton(GrowthButton growthButton, int waterPlus, int sunPlus, int nutrientPlus) {
switch (growthButton) {
case NORMAL -> {
return normalPlus;
case WATER -> {
return waterPlus;
}
case PREMIUM -> {
return premiumPlus;
case SUN -> {
return sunPlus;
}
case SUPER -> {
return superPlus;
case NUTRIENT -> {
return nutrientPlus;
}
default -> {
throw new BadRequestException("성장버튼 타입이 올바르지 않습니다.");
Expand Down Expand Up @@ -97,13 +97,13 @@ private void levelUP() {
}

private int experienceToEarn(GrowthButton growthButton) {
return convertByGrowthButton(growthButton, NORMAL_PLUS, PREMIUM_PLUS, SUPER_PLUS);
return convertByGrowthButton(growthButton, WATER_PLUS, SUN_PLUS, NUTRIENT_PLUS);
}

public enum GrowthButton {
NORMAL,
PREMIUM,
SUPER
WATER,
SUN,
NUTRIENT
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/gdg/kkia/point/entity/PointLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public enum Type {
ATTENDANCE_DAY_5_OR_MORE,
DAILY_RESPONSE,
DIARY,
PET_GROWTH,
GROWTH_WATER,
GROWTH_SUN,
GROWTH_NUTRIENT,
CHAT_BOT
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/gdg/kkia/point/service/PointLogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gdg.kkia.common.exception.NotFoundException;
import com.gdg.kkia.member.entity.Member;
import com.gdg.kkia.member.repository.MemberRepository;
import com.gdg.kkia.pet.dto.PointAndPointLogType;
import com.gdg.kkia.pet.entity.Pet;
import com.gdg.kkia.point.dto.PointLogResponse;
import com.gdg.kkia.point.dto.PointResponse;
Expand Down Expand Up @@ -159,8 +160,8 @@ public void consumePointAndWriteLog(Member member, Pet.GrowthButton growthButton
if (isMaxGrowth) {
throw new BadRequestException("최고레벨입니다.");
}
int point = member.consumePoint(growthButton);
PointLog newPointLog = new PointLog(PointLog.Type.PET_GROWTH, PointLog.Status.CONSUMED, member, point);
PointAndPointLogType pointAndButtonType = member.consumePoint(growthButton);
PointLog newPointLog = new PointLog(pointAndButtonType.pointLogType(), PointLog.Status.CONSUMED, member, pointAndButtonType.point());
pointLogRepository.save(newPointLog);
}
}

0 comments on commit 8775ecc

Please sign in to comment.