BootCamp 30day

GyeongNamยท2024๋…„ 1์›” 1์ผ
0

BootCamp

๋ชฉ๋ก ๋ณด๊ธฐ
28/49
post-thumbnail

๐Ÿ“… 2023๋…„ 12์›” 26์ผ

[java 15์ผ์ฐจ]


30์ผ์ฐจ: java ์‹ฌํ™”(8)

๊ธฐ๋ณธ ํด๋ž˜์Šค

  • Local[Date,Time,DateTime]
LocalDate birthDay = LocalDate.of(1980 ,2, 19);
LocalTime birthTime = LocalTime.of(2,2,19);
LocalDateTime birthDayTime = LocalDateTime.of(birthDay,birthTime);

// ํฌ๋กœ๋…ธํ•„๋“œ enum ํƒ€์ž… ์‚ฌ์šฉ : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํฌ๋กœ๋…ธํ•„๋“œ๋ฅผ ๋ฐ›์•„ ํ”„ํ˜ธ๊ทธ๋žจ์˜ ์œ ์—ฐ์„ฑ ํ–ฅ์ƒ
System.out.println(birthDayTime.get(ChronoField.YEAR));
// 0 : ์˜ค์ „, 1 : ์˜คํ›„
System.out.println(birthDayTime.get(ChronoField.AMPM_OF_DAY));
LocalTime newTime = LocalTime.now();
System.out.println("ํ˜„์žฌ ์‹œ๊ฐ„์€ " + (newTime.get(ChronoField.AMPM_OF_DAY) == 1 ? "์˜คํ›„ " : "์˜ค์ „")+ " ์ž…๋‹ˆ๋‹ค.");
  • Generic
    ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์ง€์ •๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธ
/*
์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ ์ƒ์„ฑ : ๋ฐ˜ํ™˜ ํƒ€์ž… ์›์ชฝ์— <T>
์ œ๋„ค๋ฆญ์€ ๊ฐ์ฒด ํƒ€์ž…์ด ๋“ค์–ด์™€์•ผ ํ•จ์— ์œ ์˜
*/
public static <T> void swap(T[] arr, int a, int b){
	T temp = arr[a];
	arr[a] = arr[b];
	arr[b] = temp;
}
class GenericStudent <T> {
    private String name;
    private T age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(T age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public T getAge() {
        return age;
    }
    public GenericStudent(String name, T age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString(){
        return "์ด๋ฆ„์€ "+this.name +" ๋‚˜์ด๋Š” "+this.age;
    }
}
  • Iterator
    ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐ˜๋ณต๊ธฐ, ์ปฌ๋ ‰์…˜์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฐ์ฒด๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ œ๊ณต
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
for(String str : list){
	System.out.println(str);
}
System.out.println();
Iterator<String> is = list.iterator();
while(is.hasNext()){
	if(is.next().equals("banana")){
	is.remove();
}
  • Enum
    ์—ด๊ฑฐ์ฒด ํด๋ž˜์Šค, ํŠน์ •ํ•œ ๊ฐ’๋“ค๋งŒ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ œํ•œ๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž…
enum Role{
    // ๊ฐ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋Š” ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ , ์ƒ์ˆ˜ ๋ชฉ๋ก ๋์—๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๋งˆ๋ฌด๋ฆฌ
    GENERAL_USER, ADMIN_USER, SUPER_USER
}
class EnumStudent{
    private Role role;
    public Role getRole() {
        return role;
    }
}

์˜ˆ์™ธ์ฒ˜๋ฆฌ

/*
* unchecked exception ์—์„œ throws ๋ฅผ ํ•˜์ง€ ์•Š๋”๋ผ๋„ ์˜ˆ์™ธ๋Š” ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ์ „ํŒŒ
* ์ด๋•Œ์— throws ๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ๋ฆฌ๋Š” ๊ฒƒ์ผ ๋ฟ.
*
* checked exception ์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ•์ œ๋˜๊ณ , ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋“ ์ง€ throws ํ†ตํ•ด ํ˜ธ์ถœํ•œ ๊ณณ์— ์œ„์ž„
* ์ด๋•Œ ํ˜ธ์ถœํ•œ ์ชฝ์—์„œ๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ•์ œ๋œ๋‹ค.
*/
try {
	System.out.println("10์—"+input+"์„ ๋‚˜๋ˆ„๋ฉด"+num/input+"์ž…๋‹ˆ๋‹ค.");
    // catch์—๋Š” try ๊ตฌ๋ฌธ์•ˆ์—์„œ ๋ฐœ์ƒ๊ฐ€๋Šฅํ•œ ์˜ˆ์™ธ์‚ฌํ•ญ์„ ์ ์–ด์•ผ ์ •์ƒ์ ์œผ๋กœ catch๊ฐ€ ๋œ๋‹ค.
    }
catch (NullPointerException e){
	e.printStackTrace();
}
catch (ArithmeticException e){
	// e์•ˆ์— ์˜ˆ์™ธ๊ด€๋ จ๋œ ์ •๋ณด๋“ค์ด ๋“ค์–ด์žˆ์Œ.
    // e.getMessage()๋Š” login ๋ฉ”์„œ๋“œ์—์„œ throw new ํ•œ ๊ณณ์—์„œ ๋„˜์–ด์˜จ ๋ฉ”์‹œ์ง€
    System.out.println(e.getMessage());
	System.out.println("์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
	e.printStackTrace();
}finally {
	// ๋ฐ˜๋“œ์‹œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๊ตฌ๋ฌธ ์‚ฝ์ž…
}

๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์ธ

  • Entity
    ์ž๋ฃŒ์˜ ๊ตฌ์กฐ๋กœ DB์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค.
class Author{
    private Long id;
    private String name;
    private String email;
    private String password;
    private static Long static_count = 0L;
    public Long getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public String getPassword() {
        return password;
    }
    Author(
            String name,
            String email,
            String password
    ){
        static_count++;
        this.id = static_count;
        this.name = name;
        this.email = email;
        this.password = password;
    }
}
  • Controller
    ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด ์ฃผ๋Š” ๊ณณ์œผ๋กœ Model์ด ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ,
    ์š”์ฒญ์˜ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” Service๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ง€์ •๋œ ๋ทฐ์— ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.
    ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” Controller๋ผ๋Š” ์ค‘๊ฐ„ ์ œ์–ด์ž๋ฅผ ๋งŒ๋“ค์–ด ๋กœ๊ทธ์ธ ์„œ๋น„์Šค๋Š” ๋กœ๊ทธ์ธ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ, ๊ฒฐ์ œ ์„œ๋น„์Šค๋Š” ๊ฒฐ์ œ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์—ญํ• ์„ ๋‚˜๋ˆ„์–ด ์„ค๊ณ„ํ•˜๋ฉด ๊ฐœ๋ฐœ๋น„์šฉ์ด๋‚˜ ์œ ์ง€๋ณด์ˆ˜๋น„์šฉ์—์„œ ์œ ๋ฆฌํ•œ ์žฅ์ ์ด ์žˆ๋‹ค.
public class AuthorController {
    public static void main(String[] args) {
        // ๋ฉ”๋ชจ๋ฆฌ DB์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€/์‚ญ์ œ ๋ถ€๋ถ„์€ repository ๋กœ ๋ถ„๋ฆฌ
        AuthorService authorService = new AuthorService();
        top_loop:
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("์‚ฌ์šฉํ•˜์‹ค ์„œ๋น„์Šค๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?");
            System.out.println("1. ํšŒ์›๊ฐ€์ž…");
            System.out.println("2. ๋กœ๊ทธ์ธ");
            int service = sc.nextInt();
            switch (service){
                case 1:
                    System.out.println("ํšŒ์›๊ฐ€์ž… ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.");
                    System.out.println();
                    sc.nextLine();
                    System.out.println("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
                    String name = sc.nextLine();
                    System.out.println("์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
                    String email = sc.nextLine();
                    System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
                    String password = sc.nextLine();
                    Author auth = new Author(name, email, password);
                    try {
                        authorService.register(auth);
                        System.out.println("ํšŒ์›๊ฐ€์ž…์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.");
                    }catch (IllegalArgumentException e){
                        System.out.println(e.getMessage());
                    }
                    System.out.println();
                    break;
                case 2:
                    System.out.println("๋กœ๊ทธ์ธ ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.");
                    System.out.println();
                    sc.nextLine();
                    System.out.println("๋กœ๊ทธ์ธ ํ•˜์‹ค Email์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
                    String login_email = sc.nextLine();
                    System.out.println("๋กœ๊ทธ์ธ ํ•˜์‹ค password๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
                    String login_pwd = sc.nextLine();
                    Optional<Author> login_Author = Optional.empty();
                    try {
                        login_Author = authorService.login(login_email,login_pwd);
                        System.out.println( login_Author.get().getName()+"๋‹˜ ๋กœ๊ทธ์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                    } catch (IllegalArgumentException | NoSuchElementException e) {
                        System.out.println("๋กœ๊ทธ์ธ ์‹คํŒจ");
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }
    }
}
  • Service
    Controller์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ํŠธ๋žœ์žญ์…˜์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
    Controller์—์„œ ๋ฐ”๋กœ Repository๋กœ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    ๊ทธ๋Ÿด ๊ฒฝ์šฐ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฝ”๋“œ๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ๋งˆ๋‹ค ์ค‘๋ณต์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.
    ๋”ฐ๋ผ์„œ ํ™•์žฅ์„ฑ, ์žฌ์‚ฌ์šฉ์„ฑ, ์ค‘๋ณต์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด Service๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
class AuthorService {
    AuthorRepository authorRepository;
    AuthorService(){
        authorRepository = new AuthorRepository();
    }
    void register(Author author) throws IllegalArgumentException{
        // ๋งŒ์•ฝ์— password 5์ž๋ฆฌ ์ดํ•˜์ด๋ฉด ์˜ˆ์™ธ๋ฐœ์ƒ (IllegalArgu)
        Optional <Author> authors = authorRepository.getAuthorByEmail(author.getEmail());
        if (author.getPassword().length()>=5) {
            if(authors.isPresent()){
                throw new IllegalArgumentException("์ด๋ฏธ ๊ฐ™์€ ํšŒ์›์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.");
            }else{
                authorRepository.register(author);
            }
        }else{
            throw new IllegalArgumentException("๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธธ์ด๊ฐ€ ์งง์Šต๋‹ˆ๋‹ค.");
        }
    }
    Optional<Author> login(String email, String pwd ) throws IllegalArgumentException, NoSuchElementException {
        Optional <Author> author = authorRepository.getAuthorByEmail(email);
        if(author.isPresent()){
           if(author.get().getPassword().equals(pwd)){
               return author;
           }else{
               throw new IllegalArgumentException("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.");
           }
        }else{
            throw new NoSuchElementException(email+" : ์ด๋ฉ”์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
        }
    }
}
  • Repository
    DB์— ์ ‘๊ทผํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ Service์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด CRUD๋ฅผ ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
public class AuthorRepository {
    List<Author> authors; // DB ์—ฐ๊ฒฐ์ด ์•ˆ๋˜์–ด ์žˆ์–ด ์ผ๋‹จ์€ Author๋ฆฌ์ŠคํŠธ๋ฅผ DB ๋Œ€์šฉ์œผ๋กœ ์‚ฌ์šฉํ•จ.
    AuthorRepository(){
        authors = new ArrayList<>();
    }
    public void register(Author author) {
        authors.add(author);
    }
    Optional<Author> getAuthorByEmail(String email){
        for(Author author:this.authors){
            if(author.getEmail().equals(email)){
                return Optional.of(author);
            }
        }
        return Optional.empty();
    }
}

github java ์‹ค์Šต ๋‚ด์šฉ

profile
503 Service Unavailable Error

0๊ฐœ์˜ ๋Œ“๊ธ€