풀스택 개발자 과정 34일차 +@

너구·2026년 6월 24일

풀스택 성장과정

목록 보기
36/79

객체지향 연습문제(1)

package practice.account;

public class Account {
    
    private String owner;
    private int balance;

    public Account(String owner, int balance) {
        this.owner = owner;
        this.balance = balance;
    }

    public void deposit(int amount) {

        balance = balance + amount;

        System.out.println(balance);
    }

    public void withdraw(int amount) {
        if (balance >= amount) {
            balance = balance - amount;
            System.out.println(balance);
        } else {
            System.out.println("잔액이 부족합니다.");
        }
    }

    public void showInfo() {
        System.out.println("예금주: " + owner);
        System.out.println("잔액: " + balance);
    }

    public static void main(String[] args) {

    Account account = new Account("철수", 10000);

    account.deposit(5000);
    account.withdraw(3000);
    account.withdraw(20000);

    account.showInfo();
}
}

객체지향 연습문제(2)

package practice.book2;

public class Book {
    
    private String title;
    private boolean borrowed;

    public Book(String title) {
        this.title = title;
        this.borrowed = false;
    }

    public void borrow() {
        if (borrowed == false) {
            borrowed = true;
        } else {
            System.out.println("이미 대출 중입니다.");
        }
    }

    public void returnBook() {
        if (borrowed == true) {
            borrowed = false;
        } else {
            System.out.println("대출된 책이 아닙니다.");
        }
    }

    public void showInfo() {
        System.out.println("제목: " + title);

        if (borrowed) {
            System.out.println("상태: 대출 중");
        } else {
            System.out.println("상태: 대출 가능");
        }

    }

    public static void main(String[] args) {

        Book book = new Book("자바의 정석");

        book.showInfo();

        book.borrow();
        book.borrow();

        book.showInfo();

        book.returnBook();

        book.showInfo();
    }

}

객체지향 연습문제(3)

package practice.book3;

public class Book {
    
    private String title;

    public Book(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}


class Student {

    private String name;
    private Book book;

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

    public void borrowBook(Book book) {
        this.book = book;
    }

    public void showInfo() {
        System.out.println("학생이름: " + name);

        if (book == null) {
            System.out.println("빌린 책: 없음");
        } else {
            System.out.println("빌린 책: " +book.getTitle());
        }
    }
    public static void main(String[] args) {

        Book book1 = new Book("자바의 정석");

        Student student = new Student("철수");

        student.showInfo();

        student.borrowBook(book1);

        student.showInfo();
    }

}

객체지향 연습문제(4)

package practice.car;

public class Car {
    
    private String model;
    private boolean engineOn;

    public Car(String model) {
        this.model = model;
        engineOn = false;
    }

    public void startEngine() {
        if (engineOn == false) {
            engineOn = true;
        } else {
            System.out.println("이미 시동이 켜져있습니다.");
        }
    }

    public void stopEngine() {
        if (engineOn == true) {
            engineOn = false;
        } else {
            System.out.println("이미 시동이 꺼져있습니다.");
        }
    }

    public boolean isRunning() {
        return engineOn;
    }

    public void showInfo() {
        
        System.out.println("모델: " + model);
        
        if (engineOn == true) {
            System.out.println("상태: 운행 중");
        } else {
            System.out.println("상태: 정지");
        }

    }

    public static void main(String[] args) {

    Car car = new Car("소나타");

    car.showInfo();

    car.startEngine();

    System.out.println("운행 중인가? " + car.isRunning());

    car.showInfo();

    car.stopEngine();

    car.showInfo();
}
}

마무리

부족한 객체지향 연습문제를 꾸준히 풀고 있다.
AI 도움 없이 혼자서 풀려니까 확실히 시간이 오래걸리고 더디긴하지만 하나씩 풀어가는 날 보며 뿌듯함을 느꼈다.
얼른 실력이 무럭무럭 자랐으면 좋겠다!

0개의 댓글