[인프런 워밍업 클럽_0기 BE] 일곱번째 과제

imageimageimage

<문제 1번>

<fruit 파트>

@Entity(name="fruit")
@NoArgsConstructor
@Getter
public class Fruit {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private LocalDate warehousingDate;
    private long price;
    private boolean isSold;

    public Fruit(String name, LocalDate warehousingDate, long price) {
        this.name = name;
        this.warehousingDate = warehousingDate;
        this.price = price;
    }

    public void updateIsSold(){
      this.isSold = true;
   }
}

 

<fruitRepositroy 파트>

public interface FruitRepository extends JpaRepository<Fruit,Long> {
    List<Fruit> findByNameAndIsSold(String name,boolean isSold);
    
    // 2번
    List<Fruit> findAllByName(String name);
    
    // 3번
    List<Fruit> findAllByPriceGreaterThanEqualAndIsSold(long price,boolean isSold);
    List<Fruit> findAllByPriceLessThanEqualAndIsSold(long price,boolean isSold);
}

 

<fruitService 파트>

public void saveFruit(Fruit fruit){
   fruitRepository.save(fruit);
}

public void sellFruit(long id){
     String sql = "UPDATE fruit SET isSold=1 WHERE id=?";
     Fruit fruit = fruitRepository.findById(id)
                     .orElseThrow();
     fruit.updateIsSold();
     fruitRepository.save(fruit);
 }

// 2번
public FruitCountResponse countFruit(String name){
        long count = fruitRepository.findAllByName(name).stream().count();
        return new FruitCountResponse(count);
}

// 3번
public List<FruitListResponse> returnFruitList(String option, long price){

        if(option.equals("GTE")){
            return fruitRepository.findAllByPriceGreaterThanEqualAndIsSold(price,false)
                    .stream().map(fruit-> new FruitListResponse(fruit.getName(),fruit.getPrice(),fruit.getWarehousingDate()))
                    .collect(Collectors.toList());

        }else{
            return fruitRepository.findAllByPriceLessThanEqualAndIsSold(price,false)
                    .stream().map(fruit-> new FruitListResponse(fruit.getName(),fruit.getPrice(),fruit.getWarehousingDate()))
                    .collect(Collectors.toList());
        }
    }

 

<price 파트>

public FruitResponse calcPrice(String name){

     long salesAmount = fruitRepository.findByNameAndIsSold(name,true)
             .stream().mapToLong(fruit->fruit.getPrice()).sum();
     long notSalesAmount = fruitRepository.findByNameAndIsSold(name,false)
             .stream().mapToLong(fruit->fruit.getPrice()).sum();

     return new FruitResponse(salesAmount,notSalesAmount);
 }

 

<문제 2번>

<fruitcontroller>

@GetMapping("/api/v1/fruit/count")
public FruitCountResponse countFruit(@RequestParam String name){
    return fruitService.countFruit(name);
}

// 3번
@GetMapping("/api/v1/fruit/list")
public List<FruitListResponse> returnFruitList(@RequestParam String option, long price){
        return fruitService.returnFruitList(option,price);
}

 

<fruitcountResponse>

@Getter
public class FruitCountResponse {
    private long count;

    public FruitCountResponse(long count) {
        this.count = count;
    }
}

 

<문제 3번>

<fruitListResponse>

@Getter
public class FruitListResponse {
    private String name;
    private long price;
    private LocalDate warehousingDate;

    public FruitListResponse(String name, long price, LocalDate warehousingDate) {
        this.name = name;
        this.price = price;
        this.warehousingDate = warehousingDate;
    }
}

 

  • 과제를 하면서 많은 에러상황들이 있었고 한번에 내 것으로 만드는 것이 안될꺼 같아 자주 돌려보며 나중에 싹 다 지우고 다시 만들어 봐야 겠다는 생각을 하였음

  • 혼자서 코드를 짜는게 아닌 다른 사람이 적은 코드를 보고 모르는 것들은 찾아 최대한 이해를 하는 식으로 접근을 하였음

댓글을 작성해보세요.

채널톡 아이콘