JAVA

[ Java ] - 기초 예제 (대중 교통 관련)

algml0703 2022. 9. 2. 10:26
반응형

bus 클래스

package cooperation;

public class Bus {
	int busNumber;
	int passengerCount;
	int money;
	
	
	
	public Bus(int busNumber) {
		this.busNumber = busNumber;
		
	}
	public void take(int money) {
		this.money += money;
		passengerCount++;
	}
	
	public void showInfo() {
		System.out.println("버스 " + busNumber + "승객 : " + passengerCount );
	}

}

subway 클래스

package cooperation;

public class Subway {
	int subwayNumber;
	int passengerCount;
	int money;
	
	public Subway(int subwayNumber) {
		this.subwayNumber = subwayNumber;
	}
	public void take(int money) {
		this.money += money;
		passengerCount++;
	}
	public void showInfo() {
		System.out.println("지하철 "+subwayNumber + " 승객 : "+passengerCount+"수입 "+money);
	}
	

}

student 클래스

package cooperation;

public class Student {
	String studentName;
	int grade;
	int money;
	
	public Student(String studentName, int money) {
		this.studentName = studentName;
		this.money = money;
		
	}
	
	public void takeBus(Bus bus) {
		bus.take(1000);
		money -= 1000;
	}
	public void takeSubway(Subway subway) {
		subway.take(1500);
		money -= 1500;
	}
	
	public void showInfo() {
		System.out.println(studentName+"님의 용돈은 "+money);
	}
}

takeTrans 클래스

package cooperation;

public class TakeTrans {

	public static void main(String[] args) {
		Student james = new Student("James", 5000);	
		Student tomas = new Student("Tomas", 10000);
		
		Bus bus100 = new Bus(100);
		james.takeBus(bus100);
		james.showInfo();
		bus100.showInfo();
		
		Subway subwayGreen = new Subway(2);
		tomas.takeSubway(subwayGreen);
		tomas.showInfo();
		subwayGreen.showInfo();
	}

}
반응형

'JAVA' 카테고리의 다른 글

[ Java ] - 배열과 ArrayList  (0) 2022.09.04
[ Java ] - 싱글턴(singleton) 패턴  (0) 2022.09.03
[ JAVA ] - 프로젝트 생성 후 hello world 찍기  (0) 2022.08.19
[ JAVA ] - 필드와 메서드  (0) 2022.08.19
[ JAVA ] - 기본 입출력 받기  (0) 2022.08.19