Message Passing 연습

메인클래스
package do_it_java_ch2;
public class Main {
public static void main(String[] args) {
System.out.println("자동차와 주유소");
Car GM5= new Car();
GasStation GS=new GasStation();
GM5.set_gas(10);
GS.set_gas(500);
GM5.show();
GS.show();
GM5.fill_gas(50,GS);
GM5.show();
GS.show();
GM5.fill_gas(460,GS);
}
}
차클래스
package do_it_java_ch2;
public class Car {
private int gas; //필드 private로 구현
void set_gas(int g) {
gas=g;
}
void show() {
System.out.println("현재 주유량: "+gas+"L");
}
void fill_gas(int g, GasStation st) { //메세지패싱 구현
if(g==st.refuel(g)) // 메소드 호출 필
gas +=g;
else
System.out.println("주유실패");
}
}
주유소클래스
package do_it_java_ch2;
public class GasStation {
private int gas; // 필드 private으로 구현
void set_gas(int g) {
gas = g;
}
void show() {
System.out.println("현재 재고량:" + gas + "L");
}
int refuel(int g) {
if (gas < g) {
return -1;
}
gas -= g;
return g;
}
}