오늘은 클래스 설계 실습 문제 두 가지를 풀었다.
기초 문제에서는 Car 클래스를, 심화 문제에서는 은행 계좌 시스템을 구현했다.
기초brand(제조사), model(모델명), speed(속도)accelerate(int amount) — 속도 증가breakSpeed(int amount) — 속도 감소 (0 미만이면 0)getInfo() — 제조사, 모델명, 속도 출력class Car {
private String brand;
private String model;
private int speed;
public Car(String brand, String model, int speed) {
this.brand = brand;
this.model = model;
this.speed = speed;
}
public void accelerate(int amount) {
if (amount < 0) {
System.out.println("음수값은 입력이 불가합니다.");
} else {
this.speed = this.speed + amount;
System.out.println("⬆️ 속도 " + amount + " 증가");
}
}
public void breakSpeed(int amount) {
if (amount < 0) {
System.out.println("음수는 입력이 불가합니다.");
} else {
this.speed = this.speed - amount;
if (this.speed < 0) { this.speed = 0; }
System.out.println("⬇️ 속도 " + amount + " 감소");
}
}
public void getInfo() {
System.out.println("제조사 : " + this.brand);
System.out.println("모델명 : " + this.model);
System.out.println("속도 : " + this.speed);
}
}
Car car = new Car("테슬라", "모델S", 1);
Car car2 = new Car("제네시스", "GV70", 2);
System.out.println("-------- car 1 --------");
car.getInfo();
car.accelerate(9);
car.getInfo();
car.breakSpeed(4);
car.getInfo();
System.out.println("-------- car 2 --------");
car2.getInfo();
car2.accelerate(9);
car2.getInfo();
car2.breakSpeed(2);
car2.getInfo();
-------- car 1 --------
제조사 : 테슬라
모델명 : 모델S
속도 : 1
⬆️ 속도 9 증가
제조사 : 테슬라
모델명 : 모델S
속도 : 10
⬇️ 속도 4 감소
제조사 : 테슬라
모델명 : 모델S
속도 : 6
-------- car 2 --------
제조사 : 제네시스
모델명 : GV70
속도 : 2
⬆️ 속도 9 증가
제조사 : 제네시스
모델명 : GV70
속도 : 11
⬇️ 속도 2 감소
제조사 : 제네시스
모델명 : GV70
속도 : 9
심화balance(잔액, private), owner(예금주)deposit(long amount) — 입금 (음수면 무시)withdraw(long amount) — 출금 (잔액 부족 시 "잔액 부족" 출력)transfer(BankAccount to, long amount) — 이체 송금class BankAccount {
private int balance;
private String owner;
public BankAccount(String owner, int balance) {
this.balance = balance;
this.owner = owner;
}
public void deposit(long amount) {
if (amount < 0) {
System.out.println("값을 잘못 입력하였습니다.");
} else {
this.balance += amount;
System.out.println(amount + "원 입금이 완료되었습니다.");
System.out.println(this.owner + "님의 잔액은 총 " + this.balance + "원 입니다.");
}
}
public void withdraw(long amount) {
if (this.balance < amount) {
System.out.println("잔액 부족");
} else {
this.balance -= amount;
System.out.println(amount + "원 출금이 완료되었습니다.");
System.out.println(this.owner + "님의 잔액은 총 " + this.balance + "원 입니다.");
}
}
public int transfer(BankAccount to, long amount) {
String toOwner = to.owner;
int toBalance = to.balance;
if (this.balance < amount) {
System.out.println("잔액이 부족하여 이체가 불가 합니다. 현재 총 잔액 : " + this.balance);
} else if (amount == 0) {
System.out.println("최소 1원이상 부터 이체가 가능합니다.");
} else {
this.balance -= amount;
to.balance += amount;
System.out.println(toOwner + "님께 총" + amount + "원 이체가 완료 되었습니다.");
return toBalance;
}
return 0;
}
public void getBalance() {
System.out.println("이름 : " + this.owner);
System.out.println("총 남은 잔액 : " + this.balance);
}
}
BankAccount owner1 = new BankAccount("홍길동", 0);
owner1.deposit(100);
owner1.withdraw(50);
BankAccount owner2 = new BankAccount("김유신", 0);
owner2.deposit(300);
owner2.withdraw(100);
owner2.transfer(owner1, 200);
owner1.getBalance();
owner2.getBalance();
100원 입금이 완료되었습니다.
홍길동님의 잔액은 총 100원 입니다.
50원 출금이 완료되었습니다.
홍길동님의 잔액은 총 50원 입니다.
300원 입금이 완료되었습니다.
김유신님의 잔액은 총 300원 입니다.
100원 출금이 완료되었습니다.
김유신님의 잔액은 총 200원 입니다.
홍길동님께 총200원 이체가 완료 되었습니다.
이름 : 홍길동
총 남은 잔액 : 250
이름 : 김유신
총 남은 잔액 : 0
transfer() 구현 중 상대 계좌의 잔액이 바뀌지 않는 문제가 발생했다.
원인은 객체의 필드값을 지역 변수에 복사하면 원본 객체와 연결이 끊긴다는 것.
// ❌ 지역 변수에 복사 — 원본 객체 잔액 그대로
int toBalance = to.balance;
toBalance += amount;
// ✅ 객체 직접 접근 — 실제 잔액 변경됨
to.balance += amount;
Java에서
int같은 기본형은 값을 복사해서 전달한다.
객체의 필드를 지역 변수에 담으면 그 순간 원본과의 연결이 끊기기 때문에,
다른 객체의 상태를 변경하려면 객체 자체(to.balance)를 통해 직접 접근해야 한다.