By GokiSoft.com| 20:37 08/07/2022|
Java Basic

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

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


#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 bt993;

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

/**
 *
 * @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;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap them N meo: ");
        int N = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < N; i++) {
            CatDetail cat = new CatDetail();
            cat.nhap();
            
            catList.add(cat);
        }
    }
    
    public void display() {
        System.out.println("Thong tin danh sach meo: ");
        for (CatDetail cat : catList) {
            cat.hienthi();
        }
    }
    
    public void sort() {
        Collections.sort(catList, (o1, o2) -> {
            return o1.getMau().compareToIgnoreCase(o2.getMau());
        });
    }
    
    public void search() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap loai can tim: ");
        String loai = scan.nextLine();
        
        System.out.println("Danh sach meo tim thay: ");
        for (CatDetail cat : catList) {
            if(cat.getLoai().equalsIgnoreCase(loai)) {
                cat.hienthi();
            }
        }
    }
    
    public void sortByColorManager() {
        Collections.sort(catList, (o1, o2) -> {
            return o1.getColorIndex() > o2.getColorIndex()?1:-1;
        });
    }
}


#Main.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 bt993;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        ManagerCat managerCat = new ManagerCat();
        
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    ColorManager.getInstance().input();
                    break;
                case 2:
                    managerCat.input();
                    break;
                case 3:
                    managerCat.display();
                    break;
                case 4:
                    managerCat.sort();
                    managerCat.display();
                    break;
                case 5:
                    managerCat.search();
                    break;
                case 6:
                    managerCat.sortByColorManager();
                    managerCat.display();
                    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 mau");
        System.out.println("2. Nhap N meo");
        System.out.println("3. Hien thi meo");
        System.out.println("4. Sap xep mau theo a-z");
        System.out.println("5. Tim kiem theo loai");
        System.out.println("6. Sap xep theo thu tu mau trong ColorManager");
        System.out.println("7. Thoat");
        System.out.println("Chon: ");
    }
}


#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 bt993;

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


#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 bt993;

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

/**
 *
 * @author Diep.Tran
 */
public class ColorManager {
    ArrayList<String> colorList;
    
    private static ColorManager instance = null;

    private ColorManager() {
        colorList = new ArrayList<>();
    }
    
    public static ColorManager getInstance() {
        if(instance == null) {
            instance = new ColorManager();
        }
        return instance;
    }

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

    public void setColorList(ArrayList<String> colorList) {
        this.colorList = colorList;
    }
    
    public void input() {
        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 display() {
        System.out.println("Danh sach ma mau: ");
        for (String color : colorList) {
            System.out.println(color);
        }
    }
}


#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 bt993;

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

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

    public CatDetail() {
    }

    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;
    }
    
    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        ColorManager.getInstance().display();
        ArrayList<String> colorList = ColorManager.getInstance().getColorList();
        
        System.out.println("Nhap loai: ");
        loai = scan.nextLine();
        System.out.println("Nhap noi song: ");
        noisong = scan.nextLine();
        System.out.println("Nhap mau: ");
        mau = scan.nextLine();
        
        for (int i = 0; i < colorList.size(); i++) {
            if(colorList.get(i).equalsIgnoreCase(mau)) {
                colorIndex = i;
                break;
            }
        }
    }

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

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

    public int getColorIndex() {
        return colorIndex;
    }

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


Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó