Java/chapter2

ch04 Student Bus Subway

낭구리 2021. 8. 25. 14:05
package ch04;

public class Bus {

	//버스 번호 
	//승객 수
	//수입금
	
	int busNumber;
	int passengerCount;
	int balance;
	
	public Bus(int busNumber) {
		this.busNumber = busNumber;
	}
	
	public void take(int money) {
//		this.money = this.money + money;
		this.balance += money;
//		passengerCount = passengerCount +1 ;
		passengerCount++;
	}
	
	public void showInfo() {
		System.out.println(busNumber + "번의 승객은");
		System.out.println(passengerCount + "명 이고");
		System.out.println("수입금은 "+balance + "입니다");
	}
}

 

package ch04;

public class Subway {

	int lineNumber;
	int passengerCount;
	int balance;
	
	public Subway(int lineNumber) {
		super();
		this.lineNumber = lineNumber;
	}
	
	public void take(int money) {
		this.balance = money;
		this.passengerCount++;
	}
	
	public void showInfo() {
		System.out.println(lineNumber + "호선의 승객은");
		System.out.println(passengerCount + "명 이고");
		System.out.println("수입금은 "+balance + "입니다");
	}
}

 

package ch04;

public class Student {

	String name;
	int money;
	
	public Student(String name, int money) {
	
		this.name = name;
		this.money= money;
	}
	//정보창 보여주기
	public void showInfo() {
		System.out.println(name + "님의 남은 돈은 :" + money);
	}
	//학생이 버스를 탄다.
	public void takeBus(Bus bus) {
		//주소값이 들어간다 () 안에
		bus.take(1000);
//		this.money = this.money - 1000;
		this.money -=  1000;
	} //설계도를 만드는 과정
	//
	
	//학생이 지하철을 탄다
	
	public void takeSubway(Subway subway) {
		//주소값이 들어간다 () 안에에
		subway. take(1200);
//		this.money = this.money - 1200;
		this.money -=  1200;
	}
	
}

 

package ch04;

public class MainTest1 {

	public static void main(String[] args) {

		//버스객체 2 생성
		//지하철 객체 2 생성
		//학생 2 명 생성
		
		Bus bus1 = new Bus(1);
		Bus bus2 = new Bus(2);
		Bus bus4 = new Bus(4);
		Subway subway1 = new Subway(2);
		Subway subway3 = new Subway(3);
		Student student1 = new Student("티모", 10000);
		Student student2 = new Student("이순신", 20000);
		
		student1.takeBus(bus1);
		student1.showInfo();
	
		bus1.showInfo();
		System.out.println("------");
		student1.takeSubway(subway1);
		student1.showInfo();
		subway1.showInfo();
		System.out.println("------");

		student2.takeBus(bus4);
		student2.takeSubway(subway3);
		student2.showInfo();
		System.out.println("------");
		subway3.showInfo();
		System.out.println("------");
		bus4.showInfo();
		//객체와 객체사이에 상호작용에 의해서 프로그램밍을 할 수 있다.
		
		
	}

}

'Java > chapter2' 카테고리의 다른 글

ch05 별모양 역으로 5->1  (0) 2021.08.25
ch05 Student  (0) 2021.08.25
ch03 UserInfo(생성자)  (0) 2021.08.25
ch03 Person  (0) 2021.08.25
ch03 Bus  (0) 2021.08.25