본문 바로가기

카테고리 없음

[내일배움캠프] 자바 Spring 입문_후발대_230103

클래스란, 객체지향, 생성자, 멤버변수,클래스 가져다 쓰기, 인스턴스

  • 클래스란?
  • : 정보를 묶는것

: 현실과 비슷한 개념(객체)을 나타내기 위한 자바의 도구를 클래스 라고 함

  • 클래스에는 기본적으로, 멤버변수, 생성자, 게터와 세터, 메서드가 존재 할 수 있음
  • 클래스를 가져다 쓰면?
  • 1) 코드의 재 사용성이 높아진다. 2) 코드의 관리가 용이하다. 3) 신뢰성이 높은 프로그래밍을 가능하게 한다.
  • 생성자
  • : 여러개를 선언할 수 있다. (파라미터가 다르게)

: 특별한 메소드로 , 반환타입이 없고, 클래스명과 동일한 이름을 가짐.


// 원격강의 자료 복습
//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);
// 카드 생성 프로그램  예제
// 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("비밀번호 불일치로 변경불가!");
            }
        }

    }
}