By GokiSoft.com| 23:41 23/11/2020|
Java Basic

Java basic- Assignment - Quản lý xe cộ

* Tạo một giao diện IXe nằm trong gói xeco.info gồm có 2 phương thức:

            - public void nhap();

            - public void hienthi();

 

* Tạo Lớp XeMay cũng nằm trong gói xeco.info thực thi giao diện IXe và có các thuộc tính sau:

            String bienso;

            String loaixe;

            String mauxe;

            float giatien;

Cài đặt Constructor, các phương thức set/get và Override các phương thức trong giao diện IXe.

 

* Tạo lớp XeMayHoaBinh nằm trong gói hoabinh.xemay kế thừa lớp XeMay. Bổ sung thêm thuộc tính:

            - n (kiểu int)    : Dùng để nhập vào số lượng xe

- Một mảng n phần tử kiểu XeMay dùng để lưu thông tin của n xe máy đang quản lý tại tỉnh Hòa Bình.

Cài đặt constructor, override tất cả các phương thức trong lớp XeMay. Phương thức nhập phải nhập vào số lượng xe và gọi nhập thông tin của n xe đó.

 

* Tạo lớp XeMayHaNoi nằm trong gói hanoi.xemay kế thừa lớp XeMay. Bổ sung thêm thuộc tính:

            - n (kiểu int)    : Dùng để nhập vào số lượng xe

- Một mảng n phần tử kiểu XeMay dùng để lưu thông tin của n xe máy đang quản lý tại tỉnh Hà Nội.

Cài đặt constructor, override tất cả các phương thức trong lớp XeMay. Phương thức nhập phải nhập vào số lượng xe và gọi nhập thông tin của n xe đó.

 

* Tạo lớp QuanLyChung nằm trong gói quanlychung.xemay, có menu như sau:

            1. Nhập thông tin cho n xe máy tại tỉnh Hòa Bình.

            2. Nhập thông tin cho n xe máy tại tỉnh Hà Nội.

            3. Sắp xếp danh sách tăng theo biển số xe.

            4. Tìm kiếm thông tin xe theo biển số xe.

            5. Thống kê số lượng xe đang quản lý.

            6. Thoát

 

-          Yêu cầu:

o   Sau khi người sử dụng chọn chức năng 3, 4, 5 sẽ đưa ra thông báo nhập vào tỉnh cần thực hiện (Hòa Bình hoặc Hà Nội).

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-11 15:15:52


package ASSIGNMENT.assignment2.xeco.info;

public interface IXe {
public void nhap();
public void hienthi();
}
package ASSIGNMENT.assignment2.xeco.info;

import java.util.Scanner;

public class XeMay implements IXe {

String bienso;
String loaixe;
String mauxe;
float giatien;

public XeMay() {
}

public XeMay(String bienso, String loaixe, String mauxe, float giatien) {
this.bienso = bienso;
this.loaixe = loaixe;
this.mauxe = mauxe;
this.giatien = giatien;
}

public String getBienso() {
return bienso;
}

public void setBienso(String bienso) {
this.bienso = bienso;
}

public String getLoaixe() {
return loaixe;
}

public void setLoaixe(String loaixe) {
this.loaixe = loaixe;
}

public String getMauxe() {
return mauxe;
}

public void setMauxe(String mauxe) {
this.mauxe = mauxe;
}

public float getGiatien() {
return giatien;
}

public void setGiatien(float giatien) {
this.giatien = giatien;
}

@Override
public void nhap() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap bien so: ");
bienso = scan.nextLine();
System.out.println("Nhap loai xe: ");
loaixe = scan.nextLine();
System.out.println("Nhap mau xe: ");
mauxe = scan.nextLine();
System.out.println("Nhap gia tien");
giatien = Float.parseFloat(scan.nextLine());
System.out.println("");
}

@Override
public void hienthi() {
System.out.println("Bien so la:" + bienso);
System.out.println("Loai xe la:" + loaixe);
System.out.println("Mau xe la:" + mauxe);
System.out.println("Gia tien la:" + giatien);
System.out.println("");
}
}
package ASSIGNMENT.assignment2.xemay.hanoi;
import ASSIGNMENT.assignment2.xeco.info.XeMay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class XeMayHaNoi extends XeMay {
Scanner scan = new Scanner(System.in);
int n;
ArrayList<XeMay> list = new ArrayList<>();

public XeMayHaNoi() {
}

public XeMayHaNoi(String bienso, String loaixe, String mauxe, float giatien, Scanner scan, int n, ArrayList<XeMay> list) {
super(bienso, loaixe, mauxe, giatien);
this.scan = scan;
this.n = n;
this.list = list;
}

@Override
public void nhap() {
System.out.print("Nhap so xe: ");
n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
XeMay xm = new XeMay();
System.out.println("Nhap thong tin xe thu " + (i + 1) + " :");
xm.nhap();
list.add(xm);
}
}

@Override
public void hienthi() {
for (int j = 0; j < list.size(); j++) {
System.out.println("xe thu: "+(j+1));
list.get(j).hienthi();
}
}

public void sapxep() {
Collections.sort(list, new Comparator<XeMay>() {
@Override
public int compare(XeMay xeMay, XeMay t1) {
if (xeMay.getBienso().equalsIgnoreCase(t1.getBienso())) {
return -1;
}
return 1;
}
});
hienthi();
}

public void timkiem(){
System.out.println("Nhap bien so xe can tim: ");
String biensoxe=scan.nextLine();
for (int k = 0 ; k<list.size();k++){
if (list.get(k).getBienso().equalsIgnoreCase(biensoxe)){
list.get(k).hienthi();
}
System.out.println("Khong tim thay xe co bien so "+biensoxe);
}
}

public void thongKe(){
System.out.println("Thong ke thong tin cac xe dang quan ly tai Ha Noi: ");
hienthi();
}
}
package ASSIGNMENT.assignment2.xemay.hoabinh;

import ASSIGNMENT.assignment2.xeco.info.XeMay;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class XeMayHoaBinh extends XeMay {
Scanner scan = new Scanner(System.in);
int n;

ArrayList<XeMay> list = new ArrayList<>();

public XeMayHoaBinh() {
}

public XeMayHoaBinh(String bienso, String loaixe, String mauxe, float giatien, Scanner scan, int n, ArrayList<XeMay> list) {
super(bienso, loaixe, mauxe, giatien);
this.scan = scan;
this.n = n;
this.list = list;
}

@Override
public void nhap() {
System.out.print("Nhap so xe: ");
n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
XeMay xm = new XeMay();
System.out.println("Nhap thong tin xe thu " + (i + 1) + " :");
xm.nhap();
list.add(xm);
}
}

@Override
public void hienthi() {
for (int j = 0; j < list.size(); j++) {
System.out.println("xe thu: "+(j+1));
list.get(j).hienthi();
}
}

public void sapxep() {
Collections.sort(list, new Comparator<XeMay>() {
@Override
public int compare(XeMay xeMay, XeMay t1) {
if (xeMay.getBienso().equalsIgnoreCase(t1.getBienso())) {
return -1;
}
return 1;
}
});
hienthi();
}

public void timkiem(){
System.out.println("Nhap bien so xe can tim: ");
String biensoxe=scan.nextLine();
for (int k = 0 ; k<list.size();k++){
if (list.get(k).getBienso().equalsIgnoreCase(biensoxe)){
list.get(k).hienthi();
}
System.out.println("Khong tim thay xe co bien so "+biensoxe);
}
}

public void thongKe(){
System.out.println("Thong ke thong tin cac xe dang quan ly tai Hoa Binh: ");
hienthi();
}
}
package ASSIGNMENT.assignment2.xemay.QuanLyChung;
import ASSIGNMENT.assignment2.xemay.hanoi.XeMayHaNoi;
import ASSIGNMENT.assignment2.xemay.hoabinh.XeMayHoaBinh;
import java.util.Scanner;
public class QuanLyChung {
public static void main(String[] args) {
XeMayHoaBinh xmhb = new XeMayHoaBinh();
XeMayHaNoi xmhn = new XeMayHaNoi();
Scanner scan = new Scanner(System.in);
int choose;
do {
showMenu();
System.out.print(" Nhap lua chon :");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
xmhb.nhap();
break;
case 2:
xmhn.nhap();
break;
case 3:
System.out.println("1.Ha Noi");
System.out.println("2.Hoa Binh");
System.out.print(" Nhap lua chon :");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
xmhn.sapxep();
break;
case 2:
xmhb.sapxep();
break;
default:
break;
}
break;
case 4:
System.out.println("1.Ha Noi");
System.out.println("2.Hoa Binh");
System.out.print(" Nhap lua chon :");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
xmhn.timkiem();
break;
case 2:
xmhb.timkiem();
break;
default:
break;
}
break;
case 5:
System.out.println("1.Ha Noi");
System.out.println("2.Hoa Binh");
System.out.print(" Nhap lua chon :");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
xmhn.thongKe();
break;
case 2:
xmhb.thongKe();
break;
default:
break;
}
break;
case 6:
System.out.println("Thoat..");
break;
default:
System.err.println("Nhap sai !!!");
break;
}
} while (choose != 6);
}

public static void showMenu() {
System.out.println("*================*");
System.out.println("1. Nhập thông tin cho n xe máy tại tỉnh Hòa Bình.");
System.out.println("2. Nhập thông tin cho n xe máy tại tỉnh Hà Nội.");
System.out.println("3. Sắp xếp danh sách tăng theo biển số xe.");
System.out.println("4. Tìm kiếm thông tin xe theo biển số xe.");
System.out.println("5. Thống kê số lượng xe đang quản lý. ");
System.out.println("6. Thoát");
System.out.println("*================*");

}
}