|
| 1 | +package nguyentuankiet_19; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class QuanLyXe { |
| 6 | + ArrayList<Xe> arrayList = new ArrayList<Xe>(); |
| 7 | + Scanner scanner = new Scanner(System.in); |
| 8 | + void menu(){ |
| 9 | + int chon; |
| 10 | + do { |
| 11 | + System.out.println("0. Thoat"); |
| 12 | + System.out.println("1. Nhap thong tin xe noi thanh"); |
| 13 | + System.out.println("2. Nhap thong tin xe ngoai thanh"); |
| 14 | + System.out.println("3. Xuất chuyến xe nội thành"); |
| 15 | + System.out.println("4. Xuất chuyến xe ngoại thành"); |
| 16 | + System.out.println("5. Xuat tất cả chuyến xe"); |
| 17 | + System.out.println("6. Xuat tổng doanh thu cho từng loại xe"); |
| 18 | + System.out.println("7. Sắp xếp các chuyến xe tăng dần theo doanh thu"); |
| 19 | + System.out.println("8. Sắp xếp các chuyến xe ngoại thành giảm dần theo doanh thu"); |
| 20 | + System.out.println("9. Xoá xe nội thành có doanh thu thấp nhất"); |
| 21 | + System.out.println("10. Sắp xếp các chuyến xe nội thành giảm dần theo họ tên tài xế"); |
| 22 | + System.out.println("Moi ban chon: "); |
| 23 | + chon = scanner.nextInt(); |
| 24 | + scanner.nextLine(); |
| 25 | + switch (chon) { |
| 26 | + case 1: |
| 27 | + NoiThanh nt = new NoiThanh(); |
| 28 | + System.out.println("Nhap ma so chuyen: "); |
| 29 | + nt.setMaChuyen(scanner.nextLine()); |
| 30 | + System.out.println("Nhap ho ten tai xe: "); |
| 31 | + nt.setHoTen(scanner.nextLine()); |
| 32 | + System.out.println("Nhap so xe: "); |
| 33 | + nt.setSoXe(scanner.nextLine()); |
| 34 | + System.out.println("Nhap so tuyen: "); |
| 35 | + nt.setSoTuyen(scanner.nextInt()); |
| 36 | + System.out.println("Nhap so km di duoc: "); |
| 37 | + nt.setSoKm(scanner.nextInt()); |
| 38 | + System.out.println("Nhap doanh thu: "); |
| 39 | + nt.setDoanhThu(scanner.nextDouble()); |
| 40 | + scanner.nextLine(); |
| 41 | + arrayList.add(nt); |
| 42 | + break; |
| 43 | + case 2: |
| 44 | + NgoaiThanh ngt = new NgoaiThanh(); |
| 45 | + System.out.println("Nhap ma so chuyen: "); |
| 46 | + ngt.setMaChuyen(scanner.nextLine()); |
| 47 | + System.out.println("Nhap ho ten tai xe: "); |
| 48 | + ngt.setHoTen(scanner.nextLine()); |
| 49 | + System.out.println("Nhap so xe: "); |
| 50 | + ngt.setSoXe(scanner.nextLine()); |
| 51 | + System.out.println("Nhap noi den: "); |
| 52 | + ngt.setNoiDen(scanner.nextLine()); |
| 53 | + System.out.println("Nhap so ngay di duoc: "); |
| 54 | + ngt.setSoNgay(scanner.nextInt()); |
| 55 | + System.out.println("Nhap doanh thu: "); |
| 56 | + ngt.setDoanhThu(scanner.nextDouble()); |
| 57 | + scanner.nextLine(); |
| 58 | + arrayList.add(ngt); |
| 59 | + break; |
| 60 | + case 3: |
| 61 | + for (Xe xe : arrayList) { |
| 62 | + if (xe instanceof NoiThanh nt1) { |
| 63 | + System.out.println(xe.getHoTen()); |
| 64 | + System.out.println(xe.getMaChuyen()); |
| 65 | + System.out.println(xe.getSoXe()); |
| 66 | + System.out.println(xe.getDoanhThu()); |
| 67 | + System.out.println(nt1.getSoTuyen()); |
| 68 | + System.out.println(nt1.getSoKm()); |
| 69 | + } |
| 70 | + } |
| 71 | + break; |
| 72 | + case 4: |
| 73 | + for (Xe xe : arrayList) { |
| 74 | + if (xe instanceof NgoaiThanh ngt1) { |
| 75 | + System.out.println(xe.getHoTen()); |
| 76 | + System.out.println(xe.getMaChuyen()); |
| 77 | + System.out.println(xe.getSoXe()); |
| 78 | + System.out.println(xe.getDoanhThu()); |
| 79 | + System.out.println(ngt1.getNoiDen()); |
| 80 | + System.out.println(ngt1.getSoNgay()); |
| 81 | + } |
| 82 | + } |
| 83 | + break; |
| 84 | + case 5: |
| 85 | + for (Xe xe : arrayList) { |
| 86 | + System.out.println(xe.toString()); |
| 87 | + System.out.println(xe.getHoTen()); |
| 88 | + System.out.println(xe.getSoXe()); |
| 89 | + System.out.println(xe.getDoanhThu()); |
| 90 | + if (xe instanceof NoiThanh nt1) { |
| 91 | + System.out.println(nt1.getSoTuyen()); |
| 92 | + System.out.println(nt1.getSoKm()); |
| 93 | + } else { |
| 94 | + NgoaiThanh ngt1 = (NgoaiThanh) xe; |
| 95 | + System.out.println(ngt1.getNoiDen()); |
| 96 | + System.out.println(ngt1.getSoNgay()); |
| 97 | + } |
| 98 | + } |
| 99 | + break; |
| 100 | + case 6: |
| 101 | + double tongNoiThanh = 0; |
| 102 | + double tongNgoaiThanh = 0; |
| 103 | + for (Xe xe : arrayList) { |
| 104 | + if (xe instanceof NoiThanh nt1) { |
| 105 | + tongNoiThanh += xe.getDoanhThu(); |
| 106 | + } else { |
| 107 | + tongNgoaiThanh += xe.getDoanhThu(); |
| 108 | + } |
| 109 | + } |
| 110 | + System.out.println("Tong doanh thu xe noi thanh: " + tongNoiThanh); |
| 111 | + System.out.println("Tong doanh thu xe ngoai thanh: " + tongNgoaiThanh); |
| 112 | + break; |
| 113 | + case 7: |
| 114 | +// for (int i = 0; i < arrayList.size() - 1; i++) { |
| 115 | +// for (int j = i + 1; j < arrayList.size(); j++) { |
| 116 | +// if (arrayList.get(i).getDoanhThu() > arrayList.get(j).getDoanhThu()) { |
| 117 | +// Xe xe = arrayList.get(i); |
| 118 | +// arrayList.set(i, arrayList.get(j)); |
| 119 | +// arrayList.set(j, xe); |
| 120 | +// } |
| 121 | +// } |
| 122 | +// } |
| 123 | + // Tạo một Comparator cho đối tượng Xe dựa trên doanhThu |
| 124 | + Comparator<Xe> comparator = (xe1, xe2) -> { |
| 125 | + // So sánh theo thuộc tính doanhThu |
| 126 | + return Double.compare(xe1.getDoanhThu(), xe2.getDoanhThu()); |
| 127 | + }; |
| 128 | + // Sắp xếp ArrayList sử dụng Comparator đã tạo |
| 129 | + arrayList.sort(comparator); |
| 130 | + break; |
| 131 | + case 8: |
| 132 | +// for (int i = 0; i < arrayList.size() - 1; i++) { |
| 133 | +// for (int j = i + 1; j < arrayList.size(); j++) { |
| 134 | +// if (arrayList.get(i).getDoanhThu() < arrayList.get(j).getDoanhThu() && arrayList.get(i) instanceof NgoaiThanh && arrayList.get(j) instanceof NgoaiThanh) { |
| 135 | +// Xe xe = arrayList.get(i); |
| 136 | +// arrayList.set(i, arrayList.get(j)); |
| 137 | +// arrayList.set(j, xe); |
| 138 | +// } |
| 139 | +// } |
| 140 | +// } |
| 141 | + // Tạo một Comparator cho đối tượng Xe dựa trên doanhThu của NgoaiThanh |
| 142 | + Comparator<Xe> comparator2 = (xe1, xe2) -> { |
| 143 | + // Kiểm tra xem cả hai đối tượng có phải là NgoaiThanh hay không |
| 144 | + // Nếu cả hai đều là NgoaiThanh, so sánh theo thuộc tính doanhThu |
| 145 | + if (xe1 instanceof NgoaiThanh && xe2 instanceof NgoaiThanh) { |
| 146 | + return Double.compare(xe1.getDoanhThu(), xe2.getDoanhThu()); |
| 147 | + } else { |
| 148 | + return 0; // Nếu cả hai đều không phải NgoaiThanh, không cần sắp xếp |
| 149 | + } |
| 150 | + }; |
| 151 | + // Sắp xếp ArrayList sử dụng Comparator đã tạo |
| 152 | + arrayList.sort(comparator2); |
| 153 | + break; |
| 154 | + case 9: |
| 155 | + double min = -1; |
| 156 | + for (Xe value : arrayList) { |
| 157 | + if (value instanceof NoiThanh) { |
| 158 | + min = value.getDoanhThu(); |
| 159 | + } |
| 160 | + } |
| 161 | + if(min!=-1){ |
| 162 | + for (Xe xe : arrayList) { |
| 163 | + if (xe instanceof NoiThanh nt1) { |
| 164 | + if (xe.getDoanhThu() < min) { |
| 165 | + min = xe.getDoanhThu(); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + for (int i = 0; i < arrayList.size(); i++) { |
| 170 | + if (arrayList.get(i).getDoanhThu() == min && arrayList.get(i) instanceof NoiThanh) { |
| 171 | + System.out.println("Xoa xe thu " + i ); |
| 172 | + arrayList.remove(i); |
| 173 | + i--; |
| 174 | + } |
| 175 | + } |
| 176 | + }else{ |
| 177 | + System.out.println("Khong co xe noi thanh trong danh sach"); |
| 178 | + } |
| 179 | + break; |
| 180 | + case 10: |
| 181 | + /*for (int i = 0; i < arrayList.size() - 1; i++) { |
| 182 | + for (int j = i + 1; j < arrayList.size(); j++) { |
| 183 | + if (arrayList.get(i).getHoTen().compareTo(arrayList.get(j).getHoTen()) > 0 && arrayList.get(i) instanceof NoiThanh && arrayList.get(j) instanceof NoiThanh) { |
| 184 | + Xe xe = arrayList.get(i); |
| 185 | + arrayList.set(i, arrayList.get(j)); |
| 186 | + arrayList.set(j, xe); |
| 187 | + } |
| 188 | + } |
| 189 | + }*/ |
| 190 | + // Tạo một Comparator cho đối tượng Xe |
| 191 | + Comparator<Xe> comparator3 = (xe1, xe2) -> { |
| 192 | + // Kiểm tra xem cả hai đối tượng có phải là NoiThanh hay không |
| 193 | + // Nếu cả hai đều là NoiThanh, so sánh theo thuộc tính hoTen |
| 194 | + if (xe1 instanceof NoiThanh && xe2 instanceof NoiThanh) { |
| 195 | + return xe1.getHoTen().compareTo(xe2.getHoTen()); |
| 196 | + } else { |
| 197 | + return 0; // Nếu cả hai đều không phải NoiThanh, không cần sắp xếp |
| 198 | + } |
| 199 | + }; |
| 200 | + // Sắp xếp ArrayList sử dụng Comparator đã tạo |
| 201 | + arrayList.sort(comparator3); |
| 202 | + |
| 203 | + break; |
| 204 | + default: |
| 205 | + System.out.println("Nhap sai, moi ban nhap lai"); |
| 206 | + } |
| 207 | + }while (chon != 0); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments