과제2

2-1

컨트롤러
@RestController
public class CalculatorAMMController {
    @GetMapping("/api/v1/calc")
    public CalculatorResponseDTO calculate(CalculatorRequestDTO request){
        return new CalculatorResponseDTO(request);
    }

}
리퀘스트
package com.group.libraryapp.dto.calculator.request;

public class CalculatorRequestDTO {
    private int num1;
    private int num2;

    public CalculatorRequestDTO(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public int getNum1() {
        return num1;
    }

    public int getNum2() {
        return num2;
    }
}
레스폰스
package com.group.libraryapp.dto.calculator.response;

import com.group.libraryapp.dto.calculator.request.CalculatorRequestDTO;

public class CalculatorResponseDTO {
    private int add;
    private int minus;
    private int multiply;

    public CalculatorResponseDTO(CalculatorRequestDTO request) {
        this.add = request.getNum1() + request.getNum2();
        this.minus = request.getNum1() - request.getNum2();
        this.multiply = request.getNum1() * request.getNum2();
    }

    public int getAdd() {
        return add;
    }

    public int getMinus() {
        return minus;
    }

    public int getMultiply() {
        return multiply;
    }
}

2-2

컨트롤러
package com.group.libraryapp.controller.date;

import com.group.libraryapp.dto.Date.reponse.DateResponseDto;
import com.group.libraryapp.dto.Date.request.DateRequestDto;
import org.springframework.web.bind.annotation.*;

@RestController
public class DateController {
    @GetMapping("/dayOfTheWeek")
    public DateResponseDto calculateDay(@RequestBody DateRequestDto request){
        return new DateResponseDto(request);
    }
}
리퀘스트
package com.group.libraryapp.dto.Date.request;

import java.time.LocalDate;

public class DateRequestDto {
    private LocalDate date;

    public LocalDate getDate() {
        return date;
    }
}

 

레스폰스
package com.group.libraryapp.dto.Date.reponse;

import com.group.libraryapp.dto.Date.request.DateRequestDto;

public class DateResponseDto {
    private String dayOfWeek;

    public DateResponseDto(DateRequestDto request) {
        this.dayOfWeek = request.getDate().getDayOfWeek().toString();
    }

    public String getDayOfWeek() {
        return dayOfWeek.substring(0,3);
    }
}

2-3

컨트롤러
@PostMapping("/addAllNumbers")
public int addAllNumbers(@RequestBody CalculatorAddAllNumbers request){
    int sum = 0;
    for(int i=0; i<request.getNumbers().length; i++){
        sum += request.getNumbers()[i];
    }
    return sum;
}



리퀘스트
package com.group.libraryapp.dto.calculator.request;

public class CalculatorAddAllNumbers {
    private int[] numbers;

    public int[] getNumbers() {
        return numbers;
    }
}

댓글을 작성해보세요.

채널톡 아이콘