Repository navigation
Expand file tree
/
Copy pathBai1.java
More file actions
221 lines (175 loc) · 5.4 KB
/
Copy pathBai1.java
File metadata and controls
221 lines (175 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package BaiTapBoSung;
import java.util.Scanner;
// Bài 1: Cây phân cấp phương tiện giao thông
//
// Vehicle (reg_num, brand)
// / | \
// PassengerVehicle Truck Motorbike
// (max_pass) (max_load) (type)
// / \
// Car Bus (number)
//
// Car và Bus được gom vào nhóm "xe chở khách" (PassengerVehicle).
abstract class Vehicle {
protected String reg_num; // số đăng ký xe
protected String brand; // hãng
public Vehicle() {
reg_num = "";
brand = "";
}
public Vehicle(String reg_num, String brand) {
this.reg_num = reg_num;
this.brand = brand;
}
public String getRegNum() { return reg_num; }
public String getBrand() { return brand; }
public void input(Scanner sc) {
reg_num = NhapLieu.chuoi(sc, " So dang ky: ");
brand = NhapLieu.chuoi(sc, " Hang: ");
}
public void print() {
System.out.printf("%-10s %-12s %-10s", loai(), reg_num, brand);
}
public abstract String loai();
protected static int docSoKhongAm(Scanner sc, String nhan) {
return NhapLieu.soNguyen(sc, " " + nhan + ": ", 0, Integer.MAX_VALUE);
}
}
abstract class PassengerVehicle extends Vehicle {
protected int max_pass; // số hành khách tối đa
public PassengerVehicle() {
super();
max_pass = 0;
}
public PassengerVehicle(String reg_num, String brand, int max_pass) {
super(reg_num, brand);
this.max_pass = max_pass;
}
public int getMaxPass() { return max_pass; }
@Override
public void input(Scanner sc) {
super.input(sc);
max_pass = docSoKhongAm(sc, "So hanh khach toi da");
}
@Override
public void print() {
super.print();
System.out.print(" Khach toi da: " + max_pass);
}
}
class Car extends PassengerVehicle {
public Car() {
super();
}
public Car(String reg_num, String brand, int max_pass) {
super(reg_num, brand, max_pass);
}
@Override
public void print() {
super.print();
System.out.println();
}
@Override
public String loai() { return "Xe con"; }
}
class Bus extends PassengerVehicle {
private int number; // mã số tuyến
public Bus() {
super();
number = 0;
}
public Bus(String reg_num, String brand, int max_pass, int number) {
super(reg_num, brand, max_pass);
this.number = number;
}
public int getNumber() { return number; }
@Override
public void input(Scanner sc) {
super.input(sc);
number = docSoKhongAm(sc, "Ma so tuyen");
}
@Override
public void print() {
super.print();
System.out.println(" | Tuyen so: " + number);
}
@Override
public String loai() { return "Xe buyt"; }
}
class Truck extends Vehicle {
private int max_load; // số kg hàng hóa tối đa
public Truck() {
super();
max_load = 0;
}
public Truck(String reg_num, String brand, int max_load) {
super(reg_num, brand);
this.max_load = max_load;
}
public int getMaxLoad() { return max_load; }
@Override
public void input(Scanner sc) {
super.input(sc);
max_load = docSoKhongAm(sc, "Tai trong toi da (kg)");
}
@Override
public void print() {
super.print();
System.out.println(" Tai trong: " + max_load + " kg");
}
@Override
public String loai() { return "Xe tai"; }
}
class Motorbike extends Vehicle {
private int type; // loại xe
public Motorbike() {
super();
type = 0;
}
public Motorbike(String reg_num, String brand, int type) {
super(reg_num, brand);
this.type = type;
}
public int getType() { return type; }
@Override
public void input(Scanner sc) {
super.input(sc);
type = docSoKhongAm(sc, "Loai xe (so)");
}
@Override
public void print() {
super.print();
System.out.println(" Loai: " + type);
}
@Override
public String loai() { return "Xe may"; }
}
public class Bai1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = NhapLieu.soNguyen(sc, "So phuong tien can nhap: ", 0, 1000);
Vehicle[] ds = new Vehicle[n];
for (int i = 0; i < n; i++) {
int loai = NhapLieu.soNguyen(sc, "Phuong tien " + (i + 1)
+ " (1 = xe con, 2 = xe buyt, 3 = xe tai, 4 = xe may): ", 1, 4);
switch (loai) {
case 1: ds[i] = new Car(); break;
case 2: ds[i] = new Bus(); break;
case 3: ds[i] = new Truck(); break;
default: ds[i] = new Motorbike();
}
ds[i].input(sc); // đa hình
}
System.out.println("\n--- Danh sach phuong tien ---");
long tongCho = 0; // long: tổng nhiều xe có thể vượt giới hạn int
for (Vehicle v : ds) {
v.print(); // đa hình
// Xử lý riêng nhóm xe chở khách nhưng vẫn duyệt chung một mảng
if (v instanceof PassengerVehicle) {
tongCho += ((PassengerVehicle) v).getMaxPass();
}
}
System.out.println("Tong so cho cua cac xe cho khach: " + tongCho);
sc.close();
}
}