By Trần Văn Điêp| 11:23 29/10/2021|
Java Basic

[Video] Java Basic- OOP - Interface - Quản lý mèo (cat) trong java

Nội dung bài tập

Bài tập về kế thừa:

(Kế thừa có thể là từ một lớp trừu tượng (lớp ảo) hoặc là từ một giao diện).

 

* Kế thừa từ 1 giao diện: Làm bài tập sau đây.

Hãy xây dựng một giao diện Cat nằm trong gói cat.exam gồm có thuộc tính và phương thức sau:

-          public final String ten = “Meo”;

-          public void nhap();

-          public void hienthi();

 

+ Xây dựng lớp CatDetail nằm trong gói cat.exam  và thực thi giao diện Cat trên rồi có thêm các thuộc tính:

-          String loai;

-          String mau (Chú ý : Màu nhập vào phải nằm trong ColorManager)

-          String noisong;

 

Cài đặt các Constructor, các phương thức set/get cho các thuộc tính của lớp và Override các phương thức trong giao diện Cat.

+ Xây dựng lớp ColorManager -> quản lý mày

- ArrayList<String> colorList -> quản lý danh sach màu của mèo.

- nhập và hiển thị mã màu trong lớp này. 

+ Cài đặt 1 lớp ManagerCat nằm trong gói manager.cat.

- Khai báo thuộc tính catList kiểu dữ liệu là ArrayList -> được sử dụng để quản lý danh sách mèo nhập vào.


+ Cài đặt 1 lớp UsingManagerCat nằm trong gói manager.cat  có menu sau:

                0.   Nhập danh sách mã màu

1.      Nhập thông tin của n con mèo

2.      Hiển thị thông tin

3.      Sắp xếp danh sách theo mau

4.      Tìm kiếm thông tin theo loai

5.    Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager

6.      Thoát.

 

Nhiệm vụ:

            Trong lớp UsingManagerCat phải khai báo 1 đối tượng của lớp ManagerCat và viết hàm để nhập vào thông tin của n con mèo.

Các nhiệm vụ: Hiển thị, sắp xếp, tìm kiếm thực hiện trên danh sách thông qua đối tượng của lớp ManagerCat. 

Source Code



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cat.exam;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author teach
 */
public class CatDetail implements ICat {

    String loai;
    String mau;
    String noiSong;
    int colorIndex;

    ColorManager colorManager;

    public CatDetail() {
    }

    public CatDetail(ColorManager colorManager) {
        this.colorManager = colorManager;
    }

    public CatDetail(String loai, String mau, String noiSong) {
        this.loai = loai;
        this.mau = mau;
        this.noiSong = noiSong;
    }

    @Override
    public void nhap() {
        Scanner input = new Scanner(System.in);

        System.out.println("Nhap loai : ");
        loai = input.nextLine();

        colorManager.hienThi();
        System.out.println("Nhap mau : ");
        while (true) {
            mau = input.nextLine();
            ArrayList<String> colorList = colorManager.getColorList();

            boolean isFind = false;
            
            for (int i = 0; i < colorList.size(); i++) {
                if(colorList.get(i).equalsIgnoreCase(mau)) {
                    colorIndex = i;
                    isFind = true;
                    break;
                }
            }
            
            if(!isFind) {
                System.out.println("Nhap error -> Yeu cau nhap lai");
            } else {
                break;
            }
        }

        System.out.println("Nhap noi song : ");
        noiSong = input.nextLine();
    }

    @Override
    public void hienThi() {
        System.out.println("");
        System.out.printf("Loai : %s, mau : %s, noi song : %s", loai, mau, noiSong);
    }

    public String getLoai() {
        return loai;
    }

    public void setLoai(String loai) {
        this.loai = loai;
    }

    public String getMau() {
        return mau;
    }

    public void setMau(String mau) {
        this.mau = mau;
    }

    public String getNoiSong() {
        return noiSong;
    }

    public void setNoiSong(String noiSong) {
        this.noiSong = noiSong;
    }

    public int getColorIndex() {
        return colorIndex;
    }

    public void setColorIndex(int colorIndex) {
        this.colorIndex = colorIndex;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cat.exam;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author teach
 */
public class ColorManager {
    ArrayList<String> colorList = new ArrayList<>();
    
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap so mau can them : ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap mau : ");
            String color = scan.nextLine();
            
            colorList.add(color);
        }
    }
    
    public void hienThi() {
        for (String color : colorList) {
            System.out.println("color : " + color);
        }
    }

    public ArrayList<String> getColorList() {
        return colorList;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cat.exam;

/**
 *
 * @author teach
 */
public interface ICat {
    public final String ten = "Meo";
    
    void nhap();
    
    void hienThi();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cat.exam;

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

/**
 *
 * @author teach
 */
public class ManagerCat {
    ArrayList<CatDetail> catDetails = new ArrayList<>();
    ColorManager colorManager = new ColorManager();
    Scanner input = new Scanner(System.in);

    public ColorManager getColorManager() {
        return colorManager;
    }
    
    public void nhap() {
        System.out.println("Nhap so meo can them : ");
        int n = Integer.parseInt(input.nextLine());
        
        for (int i = 0; i < n; i++) {
            CatDetail catDetail = new CatDetail(colorManager);
            catDetail.nhap();
            
            catDetails.add(catDetail);
        }
    }
    
    public void hienThi() {
        for (CatDetail catDetail : catDetails) {
            catDetail.hienThi();
        }
    }
    
    public void sortByColor() {
        Collections.sort(catDetails, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return o1.getMau().compareTo(o2.getMau());
            }
        });
    }
    
    public void searchByLoai() {
        System.out.println("Nhap loai can tim : ");
        String loai = input.nextLine();
        
        for (CatDetail catDetail : catDetails) {
            if(catDetail.getLoai().equalsIgnoreCase(loai)) {
                catDetail.hienThi();
            }
        }
    }
    
    public void sortByColorManager() {
        Collections.sort(catDetails, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return (o1.getColorIndex() > o2.getColorIndex())?1:-1;
            }
        });
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cat.exam;

import java.util.Scanner;

/**
 *
 * @author teach
 */
public class UsingManagerCat {
    static ManagerCat managerCat = new ManagerCat();
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = scan.nextInt();
            
            switch(choose) {
                case 1:
                    managerCat.getColorManager().nhap();
                    break;
                case 2:
                    managerCat.nhap();
                    break;
                case 3:
                    managerCat.hienThi();
                    break;
                case 4:
                    managerCat.sortByColor();
                    managerCat.hienThi();
                    break;
                case 5:
                    managerCat.searchByLoai();
                    break;
                case 6:
                    managerCat.sortByColorManager();
                    managerCat.hienThi();
                    break;
                case 7:
                    System.out.println("Exit");
                    break;
                default:
                    System.out.println("Input failed!");
                    break;
            }
        } while(choose != 7);
    }
    
    static void showMenu() {
        System.out.println("1. Nhập danh sách mã màu");
        System.out.println("2. Nhập thông tin của n con mèo");
        System.out.println("3. Hiển thị thông tin");
        System.out.println("4. Sắp xếp danh sách theo mau");
        System.out.println("5. Tìm kiếm thông tin theo loai");
        System.out.println("6. Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
        System.out.println("7. Thoát.");
        System.out.print("Lua chon : ");
    }
}


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

5

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