Repository navigation
Expand file tree
/
Copy pathBai4.java
More file actions
167 lines (133 loc) · 4.41 KB
/
Copy pathBai4.java
File metadata and controls
167 lines (133 loc) · 4.41 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
package BaiTapBoSung;
import java.util.Scanner;
// Bài 4: Quản lý ca sĩ công ty XYZ và tính lương
// CaSi (abstract) <|—— CaSiChuaNoiTieng, CaSiNoiTieng (+ soGameshow)
// CongTyXYZ ◇—— 0..* CaSi
abstract class CaSi {
private String hoTen;
private int soNamLamViec;
private int soDiaBan;
public CaSi() {
hoTen = "";
soNamLamViec = 0;
soDiaBan = 0;
}
public CaSi(String hoTen, int soNamLamViec, int soDiaBan) {
this.hoTen = hoTen;
this.soNamLamViec = Math.max(0, soNamLamViec);
this.soDiaBan = Math.max(0, soDiaBan);
}
public String getHoTen() { return hoTen; }
public int getSoNamLamViec() { return soNamLamViec; }
public int getSoDiaBan() { return soDiaBan; }
public abstract long tinhLuong();
public abstract String loai();
protected static int docSoKhongAm(Scanner sc, String nhan) {
return NhapLieu.soNguyen(sc, " " + nhan + ": ", 0, Integer.MAX_VALUE);
}
public void nhap(Scanner sc) {
hoTen = NhapLieu.chuoi(sc, " Ho ten: ");
soNamLamViec = docSoKhongAm(sc, "So nam lam viec");
soDiaBan = docSoKhongAm(sc, "So dia da ban");
}
public void xuat() {
System.out.printf("%-18s %-22s Nam: %-3d Dia: %-8d", loai(), hoTen, soNamLamViec, soDiaBan);
}
}
class CaSiChuaNoiTieng extends CaSi {
public CaSiChuaNoiTieng() {
super();
}
public CaSiChuaNoiTieng(String hoTen, int soNamLamViec, int soDiaBan) {
super(hoTen, soNamLamViec, soDiaBan);
}
// Lương = 3.000.000 + 500.000 x số năm + 1.000 x số đĩa
@Override
public long tinhLuong() {
return 3_000_000L + 500_000L * getSoNamLamViec() + 1_000L * getSoDiaBan();
}
@Override
public void xuat() {
super.xuat();
System.out.printf("%-13s Luong: %,d%n", "", tinhLuong());
}
@Override
public String loai() { return "Chua noi tieng"; }
}
class CaSiNoiTieng extends CaSi {
private int soGameshow;
public CaSiNoiTieng() {
super();
soGameshow = 0;
}
public CaSiNoiTieng(String hoTen, int soNamLamViec, int soDiaBan, int soGameshow) {
super(hoTen, soNamLamViec, soDiaBan);
this.soGameshow = Math.max(0, soGameshow);
}
public int getSoGameshow() { return soGameshow; }
// Lương = 5.000.000 + 500.000 x số năm + 1.200 x số đĩa + 500.000 x số gameshow
@Override
public long tinhLuong() {
return 5_000_000L + 500_000L * getSoNamLamViec() + 1_200L * getSoDiaBan()
+ 500_000L * soGameshow;
}
@Override
public void nhap(Scanner sc) {
super.nhap(sc);
soGameshow = docSoKhongAm(sc, "So gameshow tham gia");
}
@Override
public void xuat() {
super.xuat();
System.out.printf("Gameshow: %-3d Luong: %,d%n", soGameshow, tinhLuong());
}
@Override
public String loai() { return "Noi tieng"; }
}
class CongTyXYZ {
private CaSi[] dsCaSi = new CaSi[0];
// Câu a: nhập danh sách ca sĩ, lưu trong mảng
public void nhap(Scanner sc) {
int n;
n = NhapLieu.soNguyen(sc, "So ca si (0-1000): ", 0, 1000);
dsCaSi = new CaSi[n];
for (int i = 0; i < n; i++) {
int loai;
loai = NhapLieu.soNguyen(sc, "Ca si " + (i + 1) + " (1 = chua noi tieng, 2 = noi tieng): ", 1, 2);
dsCaSi[i] = (loai == 1) ? new CaSiChuaNoiTieng() : new CaSiNoiTieng();
dsCaSi[i].nhap(sc);
}
}
public void xuat() {
for (CaSi cs : dsCaSi) {
cs.xuat();
}
}
// Câu b: trả về một ca sĩ có lương cao nhất (null nếu danh sách rỗng)
public CaSi timLuongCaoNhat() {
CaSi max = null;
for (CaSi cs : dsCaSi) {
if (max == null || cs.tinhLuong() > max.tinhLuong()) {
max = cs;
}
}
return max;
}
}
public class Bai4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CongTyXYZ cty = new CongTyXYZ();
cty.nhap(sc);
System.out.println("\n--- Danh sach ca si ---");
cty.xuat();
CaSi max = cty.timLuongCaoNhat();
if (max == null) {
System.out.println("Danh sach rong.");
} else {
System.out.print("Ca si co luong cao nhat: ");
max.xuat();
}
sc.close();
}
}