비행기 정보(생성자)
public class Plane {
String model;
int id, capacity;
public static int plane;
public String toString() {
return "식별번호: " + id + "편\n모델: " + model + "\n승객수: " + capacity +"명\n";
}
public void setModel(String model) { this.model = model;}
public void setId(int id) {this.id = id;}
public void setCapacity(int capacity) {this.capacity = capacity;}
public Plane() {
this(0, "모름", 0);
}
public Plane(int i, String m, int c) {
this.id = i;
this.model = m;
this.capacity = c;
plane++;
}
public static int getPlanes() { return plane;}
}
import java.util.Scanner;
public class Practice_55 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Plane p1 = new Plane();
System.out.println("제주행 비행기의 정보를 입력하세요.");
System.out.print("식별번호: ");
p1.setId(input.nextInt());
System.out.print("모델: ");
p1.setModel(input.next());
System.out.print("승객수: ");
p1.setCapacity(input.nextInt());
System.out.print("제주행 비행기의 정보입니다.");
System.out.print(p1.toString());
System.out.println("현재까지 추가된 비행기는 모두 " + Plane.getPlanes() +"대 입니다.\n");
System.out.println("서울행 비행기의 정보를 입력하세요.");
System.out.print("식별번호: ");
int id = input.nextInt();
System.out.print("모델: ");
String model = input.next();
System.out.print("승객수: ");
int capacity = input.nextInt();
Plane p2 = new Plane(id, model, capacity);
System.out.print("서울행 비행기의 정보입니다.");
System.out.print(p2.toString());
System.out.println("현재까지 추가된 비행기는 모두 " + Plane.getPlanes() +"대 입니다.\n");
input.close();
}
}
결과: