Created
March 24, 2022 10:42
Do It JAVA 06 Cooperation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "명이고, 수입은 " + money + "입니다."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package 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 + "입니다."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cooperation; | |
public class Subway { | |
String lineNumber; | |
int passengerCount; | |
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 + "입니다."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cooperation; | |
public class TakeTrans { | |
public static void main(String[] args) { | |
Student studentHong = new Student("Hong", 10000); | |
Bus bus100 = new Bus(100); | |
Subway sub1 = new Subway("1호선"); | |
studentHong.takeBus(bus100); | |
studentHong.takeSubway(sub1); | |
studentHong.showInfo(); | |
bus100.showInfo(); | |
sub1.showInfo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment