Skip to content

Instantly share code, notes, and snippets.

@crisine
Created March 24, 2022 10:42
Show Gist options
  • Save crisine/6b546a89f54f8e9cb38f1264bbcbeb01 to your computer and use it in GitHub Desktop.
Save crisine/6b546a89f54f8e9cb38f1264bbcbeb01 to your computer and use it in GitHub Desktop.
Do It JAVA 06 Cooperation
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 + "입니다.");
}
}
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 + "입니다.");
}
}
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 + "입니다.");
}
}
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