Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2주차 기본/심화 과제 제출] #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ subprojects {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
}

tasks.named('test') {
Expand Down
16 changes: 16 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/Athletes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.seminar.gosopt;

import lombok.Getter;

public class Athletes extends Person {

private SportsType type;

public SportsType getType() {
return type;
}

public Athletes(SportsType type) {
this.type = type;
}
}
15 changes: 15 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/FootballPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.seminar.gosopt;

import lombok.Getter;

@Getter
public class FootballPlayer extends Athletes{

public FootballPlayer(SportsType type) {
super(type);
}

protected void introduce() {
System.out.println("저는 " + this.getType().getValue() + "선수 입니다.");
}
}
23 changes: 23 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
System.out.println("hello world");

FootballPlayer footballPlayer = new FootballPlayer(SportsType.FOOTBALL);
System.out.println(footballPlayer.getType());
footballPlayer.introduce();


Poketmon ggobugi = new Poketmon("ggobugi");
System.out.println(System.identityHashCode(ggobugi));
System.out.println(ggobugi.getClass());
System.out.println(ggobugi.getName());
System.out.println(ggobugi.getSkill());

List<String> foodList = new ArrayList<>();
foodList.add("apple");
foodList.add("banana");
foodList.add("orange");

for (String food: foodList) {
System.out.println(food);
}
}
}
17 changes: 17 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.seminar.gosopt;

import lombok.Getter;

public abstract class Person {

private String name;
private int age;

public String getName() {
return name;
}

public int getAge() {
return age;
}
}
51 changes: 51 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/Poketmon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.seminar.gosopt;

public class Poketmon {

private String name;
private String type;
private String skill;

public Poketmon(String name) {
this.name = name;
}

public Poketmon(String name, String type) {
this.name = name;
this.type = type;
}

public Poketmon() {

}

public Poketmon(String name, String type, String skill) {
this.name = name;
this.type = type;
this.skill = skill;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public String getSkill() {
return skill;
}

public void setName(String name) {
this.name = name;
}

public void setType(String type) {
this.type = type;
}

public void setSkill(String skill) {
this.skill = skill;
}
}
17 changes: 17 additions & 0 deletions week1/src/main/java/com/seminar/gosopt/SportsType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.seminar.gosopt;

public enum SportsType {
FOOTBALL("축구"),
BASKETBALL("농구"),
TENNIS("테니스");

private final String value;

SportsType(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.seminar.gosopt.interfacepractice;

public class Pikachu implements Poketmon{

@Override
public void 몸통박치기() {
System.out.println("몸통 박치기");
}

public void 백만볼트() {
System.out.println("피카피카");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.seminar.gosopt.interfacepractice;

public interface Poketmon {

public abstract void 몸통박치기();
}
43 changes: 43 additions & 0 deletions week2/src/main/java/com/seminar/week2/api/user/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.seminar.week2.api.user;

import com.seminar.week2.api.user.dto.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class UserController {

private final UserService userService;

@PostMapping("/user")
public String register(@RequestBody final RegisterRequestDto request) {
System.out.println("나이 : " + request.getAge());
System.out.println("이름 " + request.getName());
System.out.println("번호 " + request.getContact());
return "SAVE OK";
}

@PostMapping("/v2/user")
public RegisterResponseDto registerV2(@RequestBody final RegisterRequestDto requestDto) {
RegisterResponseDto dto = userService.register(requestDto);
return dto;
}

@GetMapping("/user/{userId}")
public GetResponseDto get(@PathVariable Long userId) {
return userService.get(userId);
}

@PutMapping("/user/{userId}")
public UpdateResponseDto update(@PathVariable Long userId,
@RequestBody final UpdateRequestDto updateRequestDto) {
return userService.update(userId, updateRequestDto);
}

@DeleteMapping("/user/{userId}")
public String delete(@PathVariable Long userId) {
return userService.delete(userId);
}
}
73 changes: 73 additions & 0 deletions week2/src/main/java/com/seminar/week2/api/user/UserService.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builder Pattern을 어떻게 적용할 수 있는지 잘 보여주는 코드 같습니다 역시 갓유난님.. 👍

Copy link

@jinchiim jinchiim Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 builder patter이 신기해서 찾아보게 됐네요.... 군더더기 없이 깔끔해서 최고네요.....역시 최윤한남자가있어널너무사랑한 답습니다....(아주 대단하다는 뜻.)

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.seminar.week2.api.user;

import com.seminar.week2.api.user.dto.*;
import com.seminar.week2.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserService {

public RegisterResponseDto register(RegisterRequestDto requestDto) {
User user = User.builder()
.id(1L)
.age(requestDto.getAge())
.contact(requestDto.getContact())
.gender("man")
.isStudent(true)
.name(requestDto.getName())
.build();

return new RegisterResponseDto(user.getName(),user.getContact(), user.getAge());
}

public GetResponseDto get(Long userId) {
User user = User.builder()
.id(1L)
.age(20)
.contact("010-1123-1234")
.gender("man")
.isStudent(true)
.name("unan")
.build();

if (userId != user.getId()) {
throw new RuntimeException("유저 존재하지 않습니다.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바로 예외처리를 하는 경우 throw new와 같이 직접 예외처리를 하는 이유가 궁금했었는데, 이해가 되는 코드인것 같습니다. 이부분은 다른 예제에서도 많이 사용할 수 있어서 도움이 된것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니당

}
return new GetResponseDto(
user.getName(), user.getContact(), user.getAge());
}

public UpdateResponseDto update(Long userId, UpdateRequestDto requestDto) {
User user = User.builder()
.id(1L)
.age(20)
.contact("010-1123-1234")
.gender("man")
.isStudent(true)
.name("unan")
.build();

user.setAge(requestDto.getAge());
user.setContact(requestDto.getContact());
user.setName(requestDto.getName());

return new UpdateResponseDto(user.getName(), user.getContact(), user.getAge());
}

public String delete(Long userId) {
User user = User.builder()
.id(1L)
.age(20)
.contact("010-1123-1234")
.gender("man")
.isStudent(true)
.name("unan")
.build();

user = null;
return "OK";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.seminar.week2.api.user.dto;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class GetResponseDto {
private String name;
private String contact;
private int age;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.seminar.week2.api.user.dto;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RegisterRequestDto {

private String name;
private String contact;
private int age;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.seminar.week2.api.user.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@AllArgsConstructor
public class RegisterResponseDto {

private String name;
private String contact;
private int age;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.seminar.week2.api.user.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class UpdateRequestDto {
private String name;
private String contact;
private int age;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.seminar.week2.api.user.dto;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class UpdateResponseDto {
private String name;
private String contact;
private int age;
}
Loading