By GokiSoft.com| 15:35 21/06/2023|
Java Basic

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

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 ICat 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 đặt trong package cat.color

- 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 cat.manager

- 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 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. 

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

5

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

GokiSoft.com [Teacher]
GokiSoft.com

2021-08-03 12:56:08



colorList -> ColorManager

0 -> Green
1 -> Blue
2 -> Black
3 -> Orange
4 -> Red

catList -> MangerCat -> A-Z
Black
Blue
Green
Orange
Red

catList -> ManagerCat -> Index (Color: colorList -> ColorManager)
0 -> Green
1 -> Blue
2 -> Black
3 -> Orange
4 -> Red

CatDetail -> color -> index color : colorList
-> sort index -> tang dan


#UsingManagerCat.java


/*
 * 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 lesson12.bt993;

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

/**
 *
 * @author Diep.Tran
 */
public class UsingManagerCat {
    public static ColorManager colorManager;
    public static ManagerCat catManager;
    public static Scanner scan;
    
    public static void main(String[] args) {
        colorManager = new ColorManager();
        catManager = new ManagerCat();
        
        scan = new Scanner(System.in);
        
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    colorManager.nhap();
                    break;
                case 2:
                    inputCat();
                    break;
                case 3:
                    displayCat();
                    break;
                case 4:
                    sortCatByAZ();
                    break;
                case 5:
                    searchByLoai();
                    break;
                case 6:
                    sortCatByIndex();
                    break;
                case 7:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 7);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap ma mau");
        System.out.println("2. Nhap n meo");
        System.out.println("3. Hien thi");
        System.out.println("4. Sap xep theo mau (A-Z)");
        System.out.println("5. Tim kiem theo loai");
        System.out.println("6. Sap xep theo mau (index ColorManager)");
        System.out.println("7. Thoat");
        System.out.println("Chon: ");
    }

    private static void inputCat() {
        if(colorManager.getColorList().isEmpty()) {
            System.out.println("Yeu cau nhap ma mau truoc khi nhap MEO");
            return;
        }
        
        System.out.println("Nhap N meo: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            CatDetail cat = new CatDetail();
            cat.nhap();
            
            catManager.getCatList().add(cat);
        }
    }

    private static void displayCat() {
        System.out.println("Hien thi danh sach meo: ");
        for (CatDetail catDetail : catManager.getCatList()) {
            catDetail.hienthi();
        }
    }

    private static void sortCatByAZ() {
        System.out.println("Hien thi danh sach mau theo mau (A-Z):");
        Collections.sort(catManager.getCatList(), new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return o1.getMau().compareToIgnoreCase(o2.getMau());
            }
        });
        
        displayCat();
    }

    private static void searchByLoai() {
        System.out.println("Nhap loai can tim kiem: ");
        String loai = scan.nextLine();
        
        for (CatDetail catDetail : catManager.getCatList()) {
            if(catDetail.getLoai().equalsIgnoreCase(loai)) {
                catDetail.hienthi();
            }
        }
    }

    private static void sortCatByIndex() {
        System.out.println("Hien thi danh sach meo theo mau (index): ");
        
        Collections.sort(catManager.getCatList(), new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                if(o1.getIndex() > o2.getIndex()) {
                    return 1;
                }
                return -1;
            }
        });
        
        displayCat();
    }
}


#ManagerCat.java


/*
 * 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 lesson12.bt993;

import java.util.ArrayList;

/**
 * Singleton
 * @author Diep.Tran
 */
public class ManagerCat {
    ArrayList<CatDetail> catList;

    public ManagerCat() {
        catList = new ArrayList<>();
    }

    public ArrayList<CatDetail> getCatList() {
        return catList;
    }

    public void setCatList(ArrayList<CatDetail> catList) {
        this.catList = catList;
    }
}


#ICat.java


/*
 * 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 lesson12.bt993;

/**
 *
 * @author Diep.Tran
 */
public interface ICat {
    String TEN = "Meo"; //public static final
    void nhap();//public
    void hienthi();//public
}


#ColorManager.java


/*
 * 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 lesson12.bt993;

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

/**
 * Singleton
 * @author Diep.Tran
 */
public class ColorManager {
    ArrayList<String> colorList;

    public ColorManager() {
        colorList = new ArrayList<>();
    }

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

    public void setColorList(ArrayList<String> colorList) {
        this.colorList = colorList;
    }
    
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ma mau moi: ");
        while(true) {
            String color = scan.nextLine();
            if(colorList.contains(color)) {
                System.out.println("Nhap lai ma mau khac: ");
            } else {
                colorList.add(color);
                break;
            }
        }
    }
    
    public int getColorIndex(String color) {
        for (int i = 0; i < colorList.size(); i++) {
            if(colorList.get(i).equalsIgnoreCase(color)) {
                return i;
            }
        }
        return -1;
    }
    
    public void hienthi() {
        System.out.println("Danh sach ma mau: ");
        for (int i = 0; i < colorList.size(); i++) {
            System.out.printf("\n%d. %s", i+1, colorList.get(i));
        }
        System.out.println("");
    }
}


#CatDetail.java


/*
 * 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 lesson12.bt993;

import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class CatDetail implements ICat{
    String loai, mau, noisong;
    int index;

    public CatDetail() {
    }

    public CatDetail(String loai, String mau, String noisong) {
        this.loai = loai;
        this.mau = mau;
        this.noisong = 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 getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
    
    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap loai: ");
        loai = scan.nextLine();
        
        System.out.println("Nhap mau: ");
        
        while(true) {
            UsingManagerCat.colorManager.hienthi();
            mau = scan.nextLine();
            
            index = UsingManagerCat.colorManager.getColorIndex(mau);
            if(index >= 0) {
                break;
            } else {
                System.out.println("Nhap lai mai mau: ");
            }
        }
        
        
        System.out.println("Nhap noi song: ");
        noisong = scan.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "loai=" + loai + ", mau=" + mau + ", noisong=" + noisong;
    }
}



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-03-03 06:45:22


#CatDetail.java


/*
 * 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 dong
 */
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;
    }
}


#ColorManager.java


/*
 * 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 dong
 */
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;
    }
}


#ICat.java


/*
 * 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 dong
 */
public interface ICat  {
     public final String ten = "Meo";
    
    void nhap();
    
    void hienThi();
}


#ManagerCat.java


/*
 * 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 dong
 */
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() {
        catDetails.forEach((catDetail) -> {
            catDetail.hienThi();
        });
    }
    
    public void sortByColor() {
        Collections.sort(catDetails, (CatDetail o1, CatDetail o2) -> o1.getMau().compareTo(o2.getMau()));
    }
    
    public void searchByLoai() {
        System.out.println("Nhap loai can tim : ");
        String loai = input.nextLine();
        
        catDetails.stream().filter((catDetail) -> (catDetail.getLoai().equalsIgnoreCase(loai))).forEachOrdered((catDetail) -> {
            catDetail.hienThi();
        });
    }
    
    public void sortByColorManager() {
        Collections.sort(catDetails, (CatDetail o1, CatDetail o2) -> (o1.getColorIndex() > o2.getColorIndex())?1:-1);
    }
}


#UsingManagerCat.java


/*
 * 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 dong
 */
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 : ");
    }
}






TRẦN VĂN ĐIỆP [Teacher]
TRẦN VĂN ĐIỆP

2021-03-01 07:47:07


#CatDetail.java


/*
 * 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 lesson08;

import java.util.Scanner;

/**
 *
 * @author DiepTV
 */
public class CatDetail implements ICat{
    String loai;
    String mau;
    int colorIndex = -1;
    String noisong;

    public CatDetail() {
    }

    public CatDetail(String loai, String mau, String noisong) {
        this.loai = loai;
        this.mau = mau;
        this.noisong = 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;
    }
    
    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap loai: ");
        loai = scan.nextLine();
        
        //Yeu cau : nhap mau ton tai trong danh sach quan ly ColorManager.
        //Van de 1: Nhap dung du lieu color (mau) trong ColorManager -> Neu nhap sai -> yeu cau nhap lai.
        //Van de 2: Hien thi duoc danh sach ma mua trong ColorManager -> chung ta biet -> lua chon duoc
        //Truy cap thong qua bien: UsingManagerCat.colorManager
        ColorManager colorManager = UsingManagerCat.colorManager;
        colorManager.hienThi();
        
        while(true) {
            System.out.println("\nNhap mau: ");
            
            mau = scan.nextLine();
            colorIndex = colorManager.checkExistColor(mau);
            
            if(colorIndex >= 0) {
               break;
            } else {
                System.err.println("Nhap sai ma mau .. yeu cau nhap lai !!!");
            }
        }
        
        System.out.println("Nhap noi song: ");
        noisong = scan.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.format("\nLoai: %s, mau: %s, noi song: %s\n", loai, mau, noisong);
    }
    
}


#ColorManager.java


/*
 * 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 lesson08;

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

/**
 *
 * @author DiepTV
 */
public class ColorManager {
    ArrayList<String> colorList;
    
    public ColorManager() {
        colorList = new ArrayList<>();
    }
    
    public int checkExistColor(String color) {
        for (int i = 0; i < colorList.size(); i++) {
            if(colorList.get(i).equalsIgnoreCase(color)) {
                return i;
            }
        }
        return -1;
    }
    
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        String choose;
        
        do {
            System.out.println("Nhap ma mau: ");
            String color = scan.nextLine();
            colorList.add(color);
            
            System.out.println("Ban co muon tiep tuc nhap color nua hay ko Y/N?");
            choose = scan.nextLine();
        } while(!choose.equalsIgnoreCase("N"));
    }
    
    public void hienThi() {
        System.out.println("Danh sach ma mau:");
        for (int i = 0; i < colorList.size(); i++) {
            System.out.format("\n%d. %s", i + 1, colorList.get(i));
        }
    }

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

    public void setColorList(ArrayList<String> colorList) {
        this.colorList = colorList;
    }
}


#ICat.java


/*
 * 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 lesson08;

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


#UsingManagerCat.java


/*
 * 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 lesson08;

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

/**
 *
 * @author DiepTV
 */
public class UsingManagerCat {
    public static ColorManager colorManager;
    public static List<CatDetail> catList;
    
    static Scanner scan;
    
    public static void main(String[] args) {
        //Khoi tao du lieu
        colorManager = new ColorManager();
        catList = new ArrayList<>();
        scan = new Scanner(System.in);
        
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    inputColors();
                    break;
                case 2:
                    inputCats();
                    break;
                case 3:
                    displayCats();
                    break;
                case 4:
                    sortCatsByColorAlpha();
                    break;
                case 5:
                    searchCatsByType();
                    break;
                case 6:
                    sortCatsByColorManager();
                    break;
                case 7:
                    System.out.println("Ket thuc chuong trinh!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    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.println("Lua chon: ");
    }

    private static void inputColors() {
        System.out.println("Bat dau nhap ma mau: ");
        colorManager.nhap();
    }

    private static void inputCats() {
        if(colorManager.getColorList().isEmpty()) {
            System.err.println("Bang mau khong ton tai!!! Vui long nhap ma mau truoc!!!");
            return;
        }
        
        System.out.println("Nhap so meo can them N = ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            CatDetail cat = new CatDetail();
            cat.nhap();
            
            catList.add(cat);
        }
    }

    private static void displayCats() {
        for (CatDetail catDetail : catList) {
            catDetail.hienthi();
        }
    }

    private static void sortCatsByColorAlpha() {
        Collections.sort(catList, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return o1.getMau().compareToIgnoreCase(o2.getMau());
            }
        });
        
        displayCats();
    }

    private static void searchCatsByType() {
        System.out.println("Nhap loai meo can tim: ");
        String searchType = scan.nextLine();
        
        for (CatDetail catDetail : catList) {
            if(catDetail.getLoai().equalsIgnoreCase(searchType)) {
                catDetail.hienthi();
            }
        }
    }

    private static void sortCatsByColorManager() {
        Collections.sort(catList, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                if(o1.getColorIndex() > o2.getColorIndex()) {
                    return 1;
                }
                return -1;
            }
        });
        
        displayCats();
    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-02-26 09:18:26


#Cat.java


/*
 * 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 lesson7.QuanLyMeo;

/**
 *
 * @author MyPC
 */
public abstract class Cat {
    public final String ten = "Meo";
    String mau , loai;
    public abstract void nhap();
    
    public abstract void hienThi();
    
    public String getMau(){
        return mau;
    }
    public String getLoai(){
        return loai;
    }
}


#CatDetail.java


/*
 * 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 lesson7.QuanLyMeo;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class CatDetail extends Cat{
    String loai;
    String noisong;

    public CatDetail() {
    }

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

    @Override
    public String getLoai() {
        return loai;
    }

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

    @Override
    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;
    }
    
    

    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Loai meo:");
        loai = scan.nextLine();
        System.out.println("Mau meo:");
        mau = scan.nextLine();
        System.out.println("Noi song:");
        noisong = scan.nextLine();
    }

    @Override
    public void hienThi() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "CatDetail{" + "loai=" + loai + ", mau=" + mau + ", noisong=" + noisong + '}';
    }
    
}


#ColorManager.java


/*
 * 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 lesson7.QuanLyMeo;

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

/**
 *
 * @author MyPC
 */
public class ColorManager {
    ArrayList<String> colorList = new ArrayList<>();
    
    public void input(){ 
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so luong mau muon cho vao:");
        int colorNum = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < colorNum; i++) {
            String color = scan.nextLine();
            colorList.add(color);
        }
    }
    public void hienThi(){
        for (String x : colorList) {
            System.out.println(x);
        }
    }
}


#ManagerCat.java


/*
 * 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 lesson7.QuanLyMeo;

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

/**
 *
 * @author MyPC
 */
public class ManagerCat {
    ArrayList<Cat> catList = new ArrayList<>();
    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("So con meo muon them:");
        int n = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap thong tin " + n + " con meo:");
        for (int i = 0; i < n; i++) {
            Cat cat = new CatDetail();
            cat.nhap();
            catList.add(cat);
        }
    }
    
    public void hienThi(){
        for (Cat cat : catList) {
            cat.hienThi();
        }
    }
    
    public void sapXepASCII(){
        Collections.sort(catList,new Comparator<Cat>() {
            @Override
            public int compare(Cat o1, Cat o2) {
                return  o1.getMau().compareToIgnoreCase(o2.getMau());
            }
        });
    }
    
    public void searchLoai(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Loai meo can tim kiem:");
        String searchLoai = scan.nextLine();
        for (Cat cat : catList) {
            if(cat.getLoai().equalsIgnoreCase(searchLoai)){
                cat.hienThi();
            }
        }
    }
}


#UsingManagerCat.java


/*
 * 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 lesson7.QuanLyMeo;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class UsingManagerCat {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ManagerCat cat = new ManagerCat();
        ColorManager color = new ColorManager();
        int choose;
        do{ 
            Menu();
            System.out.println("Lua chon chuong trinh:");
            choose = Integer.parseInt(scan.nextLine());
            switch( choose ){
                case 0:
                    color.input();
                    break;
                case 1:
                    cat.input();
                    break;
                case 2:
                    cat.hienThi();
                    break;
                case 3:
                    cat.sapXepASCII();
                    break;
                case 4:
                    cat.searchLoai();
                    cat.hienThi();
                    break;
                case 5:
                    for (String mau : color.colorList) {
                        for( int i = 0 ; i < cat.catList.size() ; i++ ){
                            if( cat.catList.get(i).getMau().equalsIgnoreCase(mau)){
                                cat.catList.get(i).hienThi();
                            }
                        }
                    }
                    break;
                case 6:
                    System.out.println("Tam biet!!");
                    break;
                default:
                    System.out.println("Nhap sai!!");
                    break;
            }
        }while(choose != 6);
    }
    
    static void Menu(){
        System.out.println("0.Nhap danh sach ma mau");
        System.out.println("1.Nhap thong tin cua N con meo");
        System.out.println("2.Hien thi thong tin");
        System.out.println("3.Sap xep danh sach theo mau");
        System.out.println("4.Tim kiem thong tin theo loai");
        System.out.println("5.Sap xep danh sach mau theo bang mau trong lop ColorManager");
        System.out.println("6.Thoat");
    }
}



Thành Lâm [T1907A]
Thành Lâm

2020-04-29 07:19:53



/*
 * 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 BT3.manager.cat;

/**
 *
 * @author Thannh Lam
 */
public class UsingmanagerCat {
    public static void main(String[] args) {
        ManagerCat  managerCat = new ManagerCat();
        ColorManager colorManager = new ColorManager();
        int choose;
        
        do{
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose){
                case 0:
                    colorManager.input();
                    break;
                case 1:
                    managerCat.input(colorManager);
                    break;
                case 2:
                    managerCat.display();
                    break;
                case 3:
                    managerCat.sort();
                    break;
                case 4:
                    break;
                case 5:
                    managerCat.sortByColorTable();
                    break;
                case 6:
                    System.out.println("Thoát chương trình!");
                    break;
                default:
                    System.err.println("Nhập sai!");
                    break;
            }
        }while(choose != 6);
    }
    
    static void showMenu(){
        System.out.println("0.Nhập danh sách mã màu: ");
        System.out.println("1.Nhập thông tin N ocn mèo: ");
        System.out.println("2.Hiển thị: ");
        System.out.println("3.Sắp xếp theo màu:");
        System.out.println("4.Tìm kiếm thông tin theo loại: ");
        System.out.println("5.Sắp xếp theo bảng màu: ");
        System.out.println("6.Thoát");
        System.out.println("choose: ");
    }
}



/*
 * 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 Thannh Lam
 */
public interface ICat {
    public static 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 BT3.manager.cat.ColorManager;
import java.util.Scanner;

/**
 *
 * @author Thannh Lam
 */
public class CatDetail implements ICat{
    String loai;
    String mau;
    int idColor;
    String noisong;

    public CatDetail() {
    }

    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 Loại: ");
        loai = input.nextLine();
        
       
        
        System.out.println("Noi song: ");
        noisong = input.nextLine();
    }
    
    public void nhap(ColorManager colorManager){
        nhap();
        
        Scanner input = new Scanner(System.in);
        
         System.out.println("Nhap mau: ");
        while(true) {
            mau = input.nextLine();
            idColor = colorManager.checkColor(mau);
            if(idColor != -1){
                break;
            }else{
                colorManager.display();
                System.err.println("Nhập lại mã màu: ");
            }
        }
    }

    @Override
    public void hienthi() {
    }

    @Override
    public String toString() {
        return "CatDetail{" + "ten= " + ten + ",loai=" + loai + ", mau=" + mau + ", noisong=" + 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 getIdColor() {
        return idColor;
    }

    public void setIdColor(int idColor) {
        this.idColor = idColor;
    }
    
    
        
}



/*
 * 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 BT3.manager.cat;

import cat.exam.CatDetail;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author Thannh Lam
 */
public class ManagerCat {
    ArrayList<CatDetail> catList = new ArrayList<>();

    public ManagerCat() {
    }
    
    public void input(ColorManager colorManager) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập số mèo cần thêm: ");
        int n = Integer.parseInt(input.nextLine());
        
        for(int i = 0; i <n ; i++){
            CatDetail catDetail = new CatDetail();
            catDetail.nhap(colorManager);
            
            //Lưu thông tin mèo vào bảng list
            catList.add(catDetail);
        }
    }
    
    public void display(){
        for(int i = 0; i < catList.size(); i++) {
            catList.get(i).hienthi();
        }
    }
    
    public void sort(){
        Collections.sort(catList, new Comparator<CatDetail>(){

           
            public int compare(Catdetail o1, Catdetail o2) {
                return o1.getMau().compareToIgnoreCase(o2.getMau());
            }
        });
        
        display();
    }
    
    public void sortByColorTable(){
    Collections.sort(catList, new Comparator<CatDetail>(){

           
            public int compare(Catdetail o1, Catdetail o2) {
                if(o1.getIdColor() <= o2.getIdColor()){
                    return -1;
                }
                return 1;
            }
        });
    }
    
    public void find(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập loại cần tìm kiếm: ");
        String loai = input.nextLine();
        int count = 0;
        
        
        for (CatDetail catDetail: catList){
            if(catDetail.getLoai().equalsIgnoreCase(loai)){
                count++;
                catDetail.hienthi();
            }
        }
        
        if(count == 0) {
        
            System.out.println("không thấy còn mào nào có loại ("+loai+")");}
    }
}



/*
 * 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 BT3.manager.cat;

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

/**
 *
 * @author Thannh Lam
 */
public class ColorManager {
    ArrayList<String> colorList = new ArrayList<>();

    public ColorManager() {
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        while(true){
            String color = input.nextLine();
            colorList.add(color);
            
            System.out.println("Tiếp tục nhập hay không (Y/N");
            String option = input.nextLine();
            if(option.equalsIgnoreCase("n")){
                break;
            }
        }
    }
    
    public void dispay() {
        for (int i = 0; i < colorList.size(); i++ ){
            System.out.println(" " +colorList.get(i));
        }
        System.out.println("");
    }
    
    public int checkColor(String color) {
        //return colorList.contains(color);
        for(int i = 0; i <colorList.size(); i++){
            if(colorList.get(i).equalsIgnoreCase(color)){
            }
            return i;
        }
        return -1;
    }

    public void display() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}



hoangkhiem [C1907L]
hoangkhiem

2020-03-29 09:22:31



package cat.exam;

import cat.color.ColorManager;

public interface Cat {
    public final String ten = "Meo";
    
    void nhap();
    
    void hienthi();
}



package cat.exam;

import cat.color.ColorManager;
import java.util.Scanner;

public class CatDetail implements Cat {

    String loai;
    String mau;
    String noisong;

    public CatDetail() {
    }

    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();
        System.out.println("Nhap mau : ");
        mau = input.nextLine();
        System.out.println("nhap noi song : ");
        noisong = input.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "CatDetail{"+"ten" + ten + ",loai=" + loai + ", mau=" + mau + ", noisong=" + 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 void adđ(CatDetail catDetail) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}



package cat.manager;

import cat.color.ColorManager;
import cat.exam.CatDetail;
import com.sun.org.apache.bcel.internal.classfile.Code;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class ManagerCat {
    ArrayList<CatDetail> catList = new ArrayList<>();

    public ManagerCat() {
    }
    public  void input(){
        Scanner input = new Scanner(System.in);
        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();
            catDetail.nhap();
            catDetail.adđ(catDetail);
        }
    }
    public  void  display(){
        for (int i = 0; i < catList.size(); i++) {
            catList.get(i).hienthi();
        }
    }
    public void sort(){
        Collections.sort(catList, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return o1.getMau().compareToIgnoreCase(o2.getMau());
                }
        });
        display();
    }
    public  void find(){
        Scanner input = new Scanner(System.in);
        System.out.println("NHap loai can tim kiem");
        String loai = input.nextLine();
        for (CatDetail catDetail : catList) {
            if (catDetail.getLoai().equalsIgnoreCase(loai)) {
                catDetail.hienthi();
            }
        }
    }
}



package cat1;

import cat.exam.CatDetail;
import cat.manager.ManagerCat;
import cat.color.ColorManager;
import java.util.Collections;
import java.util.Scanner;

public class UsingManagerCat {

    public static void main(String[] args) {
        ManagerCat managerCat = new ManagerCat();
        int choose;
        Scanner input = new Scanner(System.in);
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            switch (choose) {
                case 1:
                    managerCat.input();
                    break;
                case 2:
                    managerCat.display();
                    break;
                case 3:
                    managerCat.sort();
                    break;
                case 4:
                     managerCat.find();
                    break;
                case 5:
                    System.out.println("Baibai");
                    break;
                 default:
                     System.err.println("Moi chon dung");
                     break;
            }
        } while (choose != 5);
    }

    static void showMenu() {
        //   System.out.println("1. Nhập danh sách mã màu");
        System.out.println("1. Nhập thông tin của n con mèo");
        System.out.println("2. Hiển thị thông tin");
        System.out.println("3. Sắp xếp danh sách theo mau");
        System.out.println("4. 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("5. Thoát.");
        System.out.print("Lựa chọn : ");
    }
}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-27 17:04:31



/*
 * 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 cat.color.ColorManager;
/**
 *
 * @author ADMIN
 */
public interface Cat {
    public final String ten = "Meo";
    
    public void nhap(ColorManager color);
    public 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 cat.color.ColorManager;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class CatDetail implements Cat{
    String 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 CatDetail() {
    }

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

    @Override
    public void hienThi() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "CatDetail{" + "loai=" + loai + ", mau=" + mau + ", noiSong=" + noiSong + '}';
    }

    @Override
    public void nhap(ColorManager color) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập vào loài mèo: ");
        this.loai = scanner.nextLine();
        System.out.println("Chọn màu: ");
        this.mau = color.ColorList();
        System.out.println("Nhập nơi sống: ");
        this.noiSong = scanner.nextLine();
    }
    
}



/*
 * 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.color;
import java.util.ArrayList;
import java.util.Scanner;
/**
 *
 * @author ADMIN
 */
public class ColorManager {
    Scanner scan = new Scanner(System.in);
    ArrayList<String> colorList = new ArrayList<>();

    public void input() {

        System.out.println("Nhập số màu cần thêm : ");
        int n = Integer.parseInt(scan.nextLine());

        for (int i = 0; i < n; i++) {
            System.out.println("Nhập màu : ");
            String color = scan.nextLine();

            colorList.add(color);
        }
    }

    public String ColorList() {
        String mau;
        for (int i = 0; i < colorList.size(); i++) {
            System.out.println(i + "Màu " + colorList.get(i));
        }
        int index = Integer.parseInt(scan.nextLine());

        mau = colorList.get(index);

        return mau;
    }
    
    public void sort(){
        for(int i = 0; i < colorList.size(); i++){
            for(int y= i + 1; y < colorList.size(); y++){
                if (colorList.get(i).compareTo(colorList.get(y)) > 0) {
                    String temp = colorList.get(i);
                    colorList.set(i, colorList.get(y));
                    colorList.set(y, temp);
                }
            }
        }
    }

    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.manager;
import cat.color.ColorManager;
import cat.exam.CatDetail;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class ManagerCat {
    public ArrayList<CatDetail> catList = new ArrayList<>();
    Scanner scan = new Scanner(System.in);

    public void input(ColorManager color) {

        System.out.println("Nhập vào số mèo: ");
        int c = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < c; i++) {
            System.out.println("Nhập thông tin mèo thứ: " + (i + 1));
            CatDetail catDetail = new CatDetail();
            catDetail.nhap(color);
            catList.add(catDetail);
        }
    }

    public void output() {
        for (int i = 0; i < catList.size(); i++) {
            catList.get(i).hienThi();
        }
    }

    public void sort() {
        for (int i = 0; i < catList.size(); i++) {
            for (int y = 0; y < catList.size(); y++) {
                if (catList.get(i).getMau().compareTo(catList.get(y).getMau()) > 0) {
                    CatDetail temp = catList.get(i);
                    catList.set(i, catList.get(y));
                    catList.set(y, temp);

                }
            }
        }
    }

    public void seach() {
        System.out.println("Nhập loài mèo muốn tìm kiếm: ");
        String loai = scan.nextLine();
        for (int i = 0; i < catList.size(); i++) {
            if (catList.get(i).getLoai().equalsIgnoreCase(loai)) {
                catList.get(i).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;
import cat.color.ColorManager;
import cat.manager.ManagerCat;

import java.util.Scanner;
/**
 *
 * @author ADMIN
 */
public class UsingManagerCat {
    public static void main(String[] args) {
        ManagerCat managerCat = new ManagerCat();
        Scanner scan = new Scanner(System.in);
        ColorManager colorList = new ColorManager();
        int choose;
        do {
            System.out.print("Nhập vào lựa chọn: ");
            choose = Integer.parseInt(scan.nextLine());
            switch (choose) {
                case 0:
                    colorList.input();
                    break;
                case 1:
                    managerCat.input(colorList);
                    break;
                case 2:
                    managerCat.output();
                    break;
                case 3:
                    managerCat.sort();
                    for (int i = 0; i < managerCat.catList.size(); i++) {
                        managerCat.catList.get(i).hienThi();
                    }
                    break;
                case 4:
                    managerCat.seach();
                    break;
                case 5:
                    colorList.sort();
                    break;
                case 6:
                    System.out.println("Thoát.");
                    break;
                default:
                    System.err.println("Nhập sai !!!");
                    break;
            }
        } while (choose != 6);
    }

    public static void showMenu() {
        System.out.println("0.Nhập danh sách mã màu");
        System.out.println("1.Nhập thông tin của n con mèo");
        System.out.println("2.Hiển thị thông tin");
        System.out.println("3.Sắp xếp danh sách theo mau");
        System.out.println("4.Tìm kiếm thông tin theo loai");
        System.out.println("5.Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
        System.out.println("6.Thoát.");
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-26 14:58:54



/*
 * 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 Redmibook 14
 */
interface  cat {
    public final String Ten = "Meo";
    public void nhap();
    public 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.*;

/**
 *
 * @author Redmibook 14
 */
public class UsingManagerCat {

    static void menu() {
        System.out.println("0.     Nhập danh sách mã màu");
        System.out.println("1.      Nhập thông tin của n con mèo");
        System.out.println("2.      Hiển thị thông tin");
        System.out.println("3.      Sắp xếp danh sách theo mau");
        System.out.println("4.      Tìm kiếm thông tin theo loai");
        System.out.println("5.      Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
        System.out.println("6.      Thoát.");
    }

    public static void main(String[] args) {
        ArrayList<CatDetail> arrcat = new ArrayList();
        ManagerCat managercat = new ManagerCat();
        ColorManager colormanager = new ColorManager();
        while (true) {
            menu();
            Scanner input = new Scanner(System.in);
            int choice = Integer.parseInt(input.nextLine());
            switch (choice) {
                case 0:
                    colormanager.nhap();
                    break;
                case 1:

                    managercat.nhap(colormanager);

                    break;
                case 2:

                    for (int i = 0; i < managercat.catList.size(); i++) {
                        managercat.catList.get(i).hienthi();
                    }
                    break;
                case 3:
                    managercat.sort();
                    for (int i = 0; i < managercat.catList.size(); i++) {
                        managercat.catList.get(i).hienthi();
                    }
                    break;
                case 4:
                    System.out.println("Nhap loai meo : ");
                    String loai = input.nextLine();
                    for (int i = 0; i < managercat.catList.size(); i++) {
                        if (managercat.catList.get(i).loai.compareTo(loai) == 0) {
                             managercat.catList.get(i).hienthi();
                        }
                    }
                    break;
                case 5:
                    colormanager.sort();
                    System.out.println("input any number to continue");
                    colormanager.ColorList();
                    
                    break;
                case 6:
                    return;
            }
        }

    }
}



/*
 * 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.*;

/**
 *
 * @author Redmibook 14
 */
public class ManagerCat {

    ArrayList<CatDetail> catList = new ArrayList();

    public void nhap(ColorManager colorlist) {
        CatDetail cat = new CatDetail();
        cat.nhap(colorlist);
        catList.add(cat);
    }

    public void sort() {
        for (int i = 0; i < catList.size(); i++) {
            for (int j = 0; j < catList.size(); j++) {
                if (catList.get(i).mau.compareTo(catList.get(j).mau) > 0) {
                    CatDetail temp = catList.get(i);
                    catList.set(i, catList.get(j));
                    catList.set(j, temp);

                }
            }
        }
    }
}



/*
 * 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.*;

/**
 *
 * @author Redmibook 14
 */
public class ColorManager {

    ArrayList<String> colorList = new ArrayList();
    Scanner input = new Scanner(System.in);

    public void nhap() {
        Boolean exit = true;
        while (exit) {
            System.out.println("1.Nhap vao ma mau");
            System.out.println("2.Exit ");
            String choice = input.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("Nhap vao: ");
                    String MaMau = input.nextLine();
                    colorList.add(MaMau);
                    break;
                case "2":
                    exit = false;
                    break;
                default:
                    System.out.println("Please choose : ");
                    break;
            }

        }
    }

    public String ColorList() {
        String mau;
        for (int i = 0; i < colorList.size(); i++) {
            System.out.println((i) + ".Mau " + colorList.get(i));
        }
        int index = Integer.parseInt(input.nextLine());

        mau = colorList.get(index);

        return mau;
    }

    public void sort() {
        for (int i = 0; i < colorList.size(); i++) {
            for (int j = 0; j < colorList.size(); j++) {
                if (colorList.get(i).compareTo(colorList.get(j)) > 0) {
                    String temp = colorList.get(i);
                    colorList.set(i, colorList.get(j));
                    colorList.set(j, temp);
                }
            }
        }

    }
}



/*
 * 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.*;

/**
 *
 * @author Redmibook 14
 */
public class CatDetail implements cat {

    String mau;
    String loai;
    String noisong;
    Scanner input = new Scanner(System.in);

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

    public CatDetail() {
    }
    
    @Override
    public void nhap() {
        
    }
    public void nhap(ColorManager colorlist) {
        System.out.println("Nhap vao loai meo : ");
        this.loai = input.nextLine();
        System.out.println("Chon mau : ");
        this.mau = colorlist.ColorList();
        System.out.println("Nhap noi song : ");
        this.noisong = input.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println("Ten : " + Ten);
        System.out.println("Loai : " + loai);
        System.out.println("Mau : " + mau);
        System.out.println("Noi Song : " + noisong);
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-03-25 16:18:54



package cat.exam;

import cat.color.ColorManager;

public interface Cat {
    public final String ten = "Meo";
    
    void nhap(ColorManager a);
    
    void hienthi();
}



package cat.exam;

import cat.color.ColorManager;
import java.util.Scanner;

public class CatDetail implements Cat{
    String type;
    String color;
    String noisong;

    public CatDetail() {
    }

    public CatDetail(String type, String color, String noisong) {
        this.type = type;
        this.color = color;
        this.noisong = noisong;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getNoisong() {
        return noisong;
    }

    public void setNoisong(String noisong) {
        this.noisong = noisong;
    }

    @Override
    public void nhap(ColorManager clMg) {
        Scanner input = new Scanner(System.in);
        System.out.print("Insert type: ");
        type = input.nextLine();
        int dk=1;
        while(dk==1){
            System.out.print("Insert color: ");
            color = input.nextLine();
            for(int i=0;i<clMg.getColorList().size();i++){
                if(color.equals(clMg.getColorList().get(i))){
                    dk=0;
                }
            }
            if(dk==1){
                System.err.println("Wrong Color.");
            }
        }
        System.out.print("Insert place to live: ");
        noisong = input.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println("Type: "+ type);
        System.out.println("Color: "+ color);
        System.out.println("Place: "+ noisong);
    }
    
    
}



package cat.color;

import java.util.ArrayList;

public class ColorManager {
    ArrayList<String> colorList = new ArrayList<>();

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

    public void setColorList(ArrayList<String> colorList) {
        this.colorList = colorList;
    }
    
    public void input(String color){
        this.colorList.add(color);
    }
    
    public void sortList(){
        for(int i=0;i<colorList.size();i++){
            for(int j=0;j<colorList.size();j++){
                if(colorList.get(i).equals(colorList.get(j))){
                    String tmp = colorList.get(j);
                    colorList.set(j, colorList.get(i));
                    colorList.set(i, tmp);
                }
            }
        }
    }
}



package cat.manager;

import cat.color.ColorManager;
import cat.exam.CatDetail;
import java.util.ArrayList;

public class ManagerCat {
    ArrayList<CatDetail> catList = new ArrayList<>();

    public ArrayList<CatDetail> getCatList() {
        return catList;
    }

    public void setCatList(ArrayList<CatDetail> catList) {
        this.catList = catList;
    }
    
    public void input(ColorManager a){
        CatDetail detail = new CatDetail();
        System.out.println("Insert Cat: ");
        detail.nhap(a);
        catList.add(detail);
    }
    
    public void sorted(){
        for(int i=0;i<catList.size()-1;i++){
            for(int j=i+1;j<catList.size();j++){
                if(catList.get(i).getColor().equals(catList.get(j).getColor())){
                    CatDetail tmp = catList.get(j);
                    catList.set(j, catList.get(i));
                    catList.set(i, tmp);
                }
            }
        }
    }
}



package cat;

import cat.exam.CatDetail;
import cat.manager.ManagerCat;
import cat.color.ColorManager;
import java.util.Collections;
import java.util.Scanner;

public class UsingManagerCat {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        ManagerCat catlist = new ManagerCat();
        ColorManager colist = new ColorManager();
        while(true){
            System.out.println("\n0.Nhập danh sách mã màu");
            System.out.println("1.Nhập thông tin của n con mèo");
            System.out.println("2.Hiển thị thông tin");
            System.out.println("3.Sắp xếp danh sách theo mau");
            System.out.println("4.Tìm kiếm thông tin theo loai");
            System.out.println("5.Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
            System.out.println("6.Thoát.");
            int choose= Integer.parseInt(inp.nextLine());
            switch(choose){
                case 0:
                    String color = inp.nextLine();
                    System.out.print("\nInput Color List: ");
                    colist.input(color);
                    break;
                case 1:
                    System.out.print("Input number of cat: ");
                    int catnumb= Integer.parseInt(inp.nextLine());
                    for(int i=0;i<catnumb;i++){
                        System.out.println("\nCat number "+(i+1)+":");
                        catlist.input(colist);
                    }
                    break;
                case 2:
                    for( int i=0; i<catlist.getCatList().size();i++){
                        System.out.println("\nCat number "+(i+1)+":");
                        catlist.getCatList().get(i).hienthi();
                    }
                    break;
                case 3:
                    catlist.sorted();
                    System.out.println("Array Sorted");
                    break;
                case 4:
                    System.out.println("Find: ");
                    String cat = inp.nextLine();
                    int vt=0;
                    for(int i=0;i<catlist.getCatList().size();i++){
                        if(cat.equals(catlist.getCatList().get(i).getType())){
                            vt=i;
                            break;
                        }
                    }
                    catlist.getCatList().get(vt).hienthi();
                    break;
                case 5:
                    colist.sortList();
                    break;
                case 6:
                    System.exit(0);
                    break;
            }
        }
    }
}



trung [C1907L]
trung

2020-03-25 15:37:08



/*
 * 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 cat.color.ColorManager;

/**
 *
 * @author prdox
 */
interface ICat {

    public final String TEN = "Meo";

    public void nhap(ColorManager cList);

    public 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 cat.color.ColorManager;
import java.util.Scanner;

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

    String loai;

    String mau;

    String noisong;

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

    public String getLoai() {
        return loai;
    }

    public String getMau() {
        return mau;
    }

    public String getNoisong() {
        return noisong;
    }

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

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

    public void setNoisong(String noisong) {
        this.noisong = noisong;
    }
    
    @Override
    public void nhap(ColorManager cList) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao loai meo");
        this.loai = input.nextLine();
        this.mau = cList.colorMenu();
        System.out.println("Nhap vao noi song");
        this.noisong = input.nextLine();
    }

    @Override
    public void hienthi() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "CatDetail{" + "loai=" + loai + ", mau=" + mau + ", noisong=" + noisong + '}';
    }
}



/*
 * 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.color;

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

/**
 *
 * @author prdox
 */
public class ColorManager {
    ArrayList<String> colorList;

    public ColorManager() {
        this.colorList = new ArrayList<String>();
    }
    
    public void addColor(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao mau");
        this.colorList.add(input.nextLine());
    }
    
    public void clearList(){
        this.colorList.clear();
    }
    
    public void sortList(){
        for (int i = 0; i<colorList.size()-1; i++){
            for (int j = i+1; j < colorList.size(); j++){
                if (colorList.get(i).compareTo(colorList.get(j)) > 0){
                    //exchange
                    String temp = colorList.get(j);
                    colorList.set(j, colorList.get(i));
                    colorList.set(i, temp);
                }
            }
        }
    }
    
    public String colorMenu(){
        Scanner input = new Scanner(System.in);
        int colorIndex = -1;
        while(colorIndex < 1 || colorIndex > colorList.size()){
            for (int i = 0; i < colorList.size(); i++) {
                System.out.format("%d. %s", i+1, colorList.get(i));
            }
            colorIndex = Integer.parseInt(input.nextLine());
        }
        return colorList.get(colorIndex-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.manager;

import cat.color.ColorManager;
import cat.exam.CatDetail;
import java.util.ArrayList;

/**
 *
 * @author prdox
 */
public class ManagerCat {
    ArrayList<CatDetail> catList;

    public ManagerCat() {
        this.catList = new ArrayList<>();
    }
    
    public void input(ColorManager cList) {
        CatDetail newCat = new CatDetail();
        newCat.nhap(cList);
        catList.add(newCat);
    }
    
    public void output(){
        for (CatDetail catDetail : catList) {
            catDetail.hienthi();
        }
    }
    
    public void sortByColor(){
        for (int i = 0; i<catList.size()-1; i++){
            for (int j = i+1; j < catList.size(); j++){
                if (catList.get(i).getMau().compareTo(catList.get(j).getMau())>0){
                    //exchange
                    CatDetail temp = catList.get(j);
                    catList.set(j, catList.get(i));
                    catList.set(i, temp);
                }
            }
        }
    }
    
    public void searchByType(String type){
        for (CatDetail catDetail : catList) {
            if (catDetail.getLoai().compareTo(type) == 0){
                catDetail.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;

import cat.color.ColorManager;
import cat.manager.ManagerCat;
import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class UsingCatManager {
    public static int printMenu(){
        Scanner input = new Scanner(System.in);
        int x = -1;
        while (x<0 || x>6) {            
            System.out.println("0.   Nhập danh sách mã màu");
            System.out.println("1.   Nhập thông tin của n con mèo");
            System.out.println("2.   Hien thi thong tin");
            System.out.println("3.   Sắp xếp danh sách theo mau");
            System.out.println("4.   Tìm kiếm thông tin theo loai");
            System.out.println("5.   Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
            System.out.println("6.   Thoát.");
            x = Integer.parseInt(input.nextLine());
        }
        return x;
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ManagerCat manCat = new ManagerCat();
        ColorManager cList = new ColorManager();
        boolean exit = false;
        while (!exit){
            int option = printMenu();
            switch(option){
                case 0:
                    cList.clearList();
                    System.out.println("Nhap vao so luong mau");
                    int n = Integer.parseInt(input.nextLine());
                    for (int i = 0; i<n; i++) cList.addColor();
                    break;
                case 1:
                    manCat.input(cList);
                    break;
                case 2:
                    manCat.output();
                    break;
                case 3:
                    manCat.sortByColor();
                    break;
                case 4:
                    System.out.println("Nhap vao loai meo can tim kiem");
                    String type = input.nextLine();
                    manCat.searchByType(type);
                    break;
                case 5:
                    cList.sortList();
                    break;
                case 6:
                    exit = true;
                    break;
            }
        }
    }
}