본문 바로가기

내일배움캠프 4기 스프링/내배캠 TIL📘

01. 02 개인 과제 수행/ 팀 프로젝트/ 코딩 테스트 입문/ 자바 기초 수업

728x90

1. 개인 과제 수행

삭제 기능 구현 중 만난 예외 : Referential integrity constraint violation

JPA 관점에서 고아 객체 제거 기능 활용한 자식 엔티티 제거 구현

DB 관점에서 -> 참조 키가 바라보고 있는 기본 키의 테이블이 삭제될 경우 FK 키의 테이블이 삭제되는 테이블 속성을 추가한다.

 

Spring Security를 적용한 백엔드 서버 구축 실습 시작

1) WebSecurityConfig

2) JwtAuthFilter

생성 및 사용 메서드, 어노테이션의 선언위치 공부 등

2. 팀 프로젝트

주말 간 UML 작성을 통해 프로젝트를 구체화했고, 오늘은 이를 바탕으로 코드구현을 위한 회의와 협업 시간을 가졌다.

코드 리뷰를 통한 스터디 및 코드 흐름 파악

3. 코딩 테스트 입문

배열 뒤집기

4. 자바 기초 수업

클래스와 메서드

클래스 -> 정보를 묶는 것, 멤버변수, 생성자, 접근자와 설정자, 메서드가 존재할 수 있다.

클래스를 가져다 쓰면?

- 코드의 재사용성이 높아진다, 코드의 관리가 보다 용이해진다, 신뢰성 있는 프로그래밍을 가능하게 한다

 

생성자 : 여러 개를 선언할 수 있다(파라미터가 다르게), 반환타입이 없고 클래스명과 동일한 이름을 가진다

 

실습코드

// 원격강의 자료 복습
//Course 클래스 
public class Course {
     // 멤버변수
		// title, tutor, days 가 Course 라는 맥락 아래에서 의도가 분명히 드러나죠!
    public String title;
    public String tutor;
    public int days;
}

// (기본)생성자 = 특별한 메서드, 
// 클래스명과 이름 동일한 메서드 
// 반환값이 없다.
public Course(){}

// 생성자 (파라미터가 3개짜리 생성자)
// 단축키는 alt+insert 를 통해 생성이 가능하지만 
// 기본기를 다지는 시기에는 그냥 코드를 치며 문법을 외우고 생김새를 익히도록 해보자.
public Course(String title, String tutor, int days) {
this.title =title;
this.tutor =tutor;
this.days =days;
}

--
// 메인클래스
public class Prac6 {
 // 객체화, 그리고 클래스
//    이렇게 정보를 묶은 '클래스'로 만들어 놓으면 우리는 이제 변수처럼 사용을 할 수 있음.

    public static void main(String[] args) {

        Course course = new Course();
        course.title = "Spring";
        course.tutor = "남병관";
				course.days =35;

        System.out.println(course.title);
        System.out.println(course.tutor);
				System.out.println(course.days);

			
    }
}


// 클래스의 생성자 중 파라미터가 3개인 생성자를 통해 객체화, 인스턴스 생성
String title = "Spirng";
String tutor = "남병관";
int days =35;

Course course = new Course(title, tutor, days);
System.out.println(title);
System.out.println(tutor);
System.out.println(days);




-------
//예제) Tv


public class Tv {
    public String color;
    public boolean power;
    public int channel;

 // 전원이 꺼져있으면 켜고 켜져있으면 끄라는 의미 
    public void power() {
        power = ! power;

    }

    public void channerUp() {
        channel++;
    }

    public void channelDown() {
        channel--;
    }
}

----

public class Prac06 {
    public static void main(String[] args) {

    Tv t = new Tv();
    t.channel =7;
    t.channelDown();

        System.out.println("현재 채널은 " + t.channel+"입니다.");

    }
}


----------------
// 하나의 클래스에서 위의 코드를 작성한다면? Prac06 클래스에서 전체 작성한 코드의 예
class Tv {
    String color;
    boolean power;
    int channel;

    void power() {
        power = ! power;
    }

    void channelUp() {
        channel++;
    }
    void channelDown() {
        channel--;
    }
}


public class Prac06 {
    public static void main(String[] args) {
        Tv t = new Tv();
        t.channel = 7;
        t.channelDown();
        System.out.println("현재채널은" + t.channel + "입니다.");
        
       
    }
}

---------------

예제) BookShop

public class Book {
    
    // 멤버변수로 4개 선언하기 (title, author, publisher, price)
    // public (접근제어자) 으로 하기
		// 기본생성자 만들기 
		// 파라미터 4개짜리 생성자 만들기 

    public String title;
    public String author;
    public String publisher;
    public int price;

public Book(){;}
public Book(String title, String author, String publisher, int price) {
this.title =title;
this.author =author;
this.publisher =publisher;
this.price = price;
}

--------
public class Prac {
    public static void main(String[] args) {
// 1-1.
        Book book1 = new Book();
        book1.title = "타이탄의 도구들";
        book1.author = "ABC";
        book1.publisher = "교보문고";
        book1.price = 23000;
        
        System.out.println(book1.title);
        System.out.println(book1.author);
        System.out.println(book1.publisher);
        System.out.println(book1.price);
        System.out.println("내가 산 책은" + book1.title+"이고, "+ "작가는 "+ book1.author+"이다.");
    }
}

--------
public class Prac {
    public static void main(String[] args) {
// 1-2
				String title ="타이탄의 도구들";
        String author ="ABC";
        String publisher = "교보문고";
        int price = 23000;

        Boook book2 = new Boook(title, author,publisher,price);
        System.out.println(title);
        System.out.println(author);
        System.out.println(publisher);
        System.out.println(price);

----


// 카드 생성 프로그램  예제
// 3장의 카드를 발급함
// 발급, 결제, 비밀번호 변경의 기능을 넣은 프로그램 
public class Card {
    public String user;
    public int pw;
    public int bal;

    public Card() {;}

    public Card(String user,int pw,int bal){
        this.user=user;
        this.pw=pw;
        this.bal=bal;
        System.out.println(this.user+"님, 카드등록완료!");
    }

    public int getPw() {
        return pw;
    }
    public void setPw(int pw) {
        this.pw = pw;
        System.out.println("비밀번호 변경완료!");
    }
}

---
public class Prac06 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
   
       Card[] c=new Card[3];
	
        for(int i=0;i<3;i++) {
            System.out.print("이름: ");
            String name=sc.nextLine();
            System.out.print("비밀번호: ");
            int pw=sc.nextInt();
            System.out.print("잔액: ");
            int bal=sc.nextInt();
            // 버퍼를 비워줌
            sc.nextLine(;
            c[i]=new Card(name,pw,bal);
        }


				//결제
        // 비밀번호 일치여부 확인> 5000원씩 결제되는 프로그램을 구현
        for(int i=0;i<3;i++) {
            System.out.println(c[i].user+"님, 결제진행중입니다.");
            System.out.print("pw입력: ");
            int pw=sc.nextInt();
            // 비밀번호 일치시 먼저 else 까지 쓰고
            if(pw==c[i].getPw()) {
                // 그안에 비밀번호 일치시 처리할 로직을 구현하고
                if(c[i].bal<5000) {
                    System.out.println("잔액부족!");
                }
                else {
                    c[i].bal-=5000;
                    System.out.println("결제완료");
                }
            }
            else {
                System.out.println("비밀번호 불일치!");
            }
        }


       // 비밀번호 변경
        for(int i=0;i<3;i++) {
            System.out.print(c[i].user+"님, pw입력: ");
            int pw=sc.nextInt();
            if(pw==c[i].getPw()) {
                System.out.print("새로운 pw입력: ");
                pw=sc.nextInt();
                if(pw==c[i].getPw()) {
                    System.out.println("기존 비밀번호와 동일합니다!");
                }
                else {
                    c[i].setPw(pw);
                }
            }
            else {
                System.out.println("비밀번호 불일치로 변경불가!");
            }
        }

    }
}

생성자, 접근자 및 설정자 등에 대해서는 아직도 부족함이 많이 느껴진다. 시간 날 때마다 복습하기!

728x90