//학생이 버스 또는 지하철을 탔을 때 코드짜기
package com.day03.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+"명이고, 수입은 "+money+"입니다." );
}
}
package com.day03.cooperation;
public class Subway {
public String lineNumber; //지하철 노선 번호
public int passengerCount; //승객 수
public int money; //수입
public Subway(String lineNumber) {
this.lineNumber=lineNumber;
}
public void take(int money) {
this.money+=money;
passengerCount++;
}
public void showInfo() {
System.out.println(lineNumber+"의 승객은 "+passengerCount+"명이고, 수입은"+money+"입니다.");
}
}
package com.day03.cooperation;
public class Student {
public String studentName; //학생 이름
public int grade; //학년
public int money; //학생이 가지고 있는 돈
public Student(String studentName, int money) {
this.studentName=studentName;
this.money=money;
}
public void takeBus(Bus bus) {
bus.take(1000);
this.money-=1000;
}
public void takeSubway(Subway subway) {
subway.take(1500);
this.money-=1500;
}
public void showInfo() {
System.out.println(studentName+"님의 남은 돈은 "+money+"입니다.");
}
}
package com.day03.cooperation;
public class TakeTrans {
public static void main(String[] args) {
//두 명의 학생을 생성
Student studentJames = new Student("James",5000);
Student studentTomas = new Student("Tomas",10000);
Bus bus100 = new Bus(100);
studentJames.takeBus(bus100);
studentJames.showInfo();
bus100.showInfo();
Subway subwayGreen = new Subway("2호선");
studentTomas.takeSubway(subwayGreen);
studentTomas.showInfo();
subwayGreen.showInfo();
studentTomas.takeBus(bus100);
studentTomas.showInfo();
bus100.showInfo();
}
}