By GokiSoft.com| 20:33 24/04/2024|
Java Basic

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

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

#ManagerCat.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bai2;

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

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

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

    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap so meo can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            CatDetail cat = new CatDetail();
            cat.nhap();
            
            catList.add(cat);
        }
    }

    void hienthi() {
        System.out.println("Danh sach meo: ");
        for (CatDetail catDetail : catList) {
            catDetail.hienthi();
        }
    }

    void sortByColor() {
        //Bai toan sap xep
        //int[] t = {1, 5, 2, 100, 22} -> tang dan -> swap (su dung 2 vong lap long nhau)
        //Collections.sort(t); -> Tu dong sap xep luon theo thu tu tan dan
        
        //ArrayList<CatDetail> catList = ...
        //Sap xep theo ma mau (A -> Z) -> For 2 vong long nhau -> swap
//        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) {
//                    //Doi cho 2 doi tuong trong ArrayList
//                    CatDetail tmp = catList.get(i);
//                    catList.set(i, catList.get(j));
//                    catList.set(j, tmp);
//                }
//            }
//        }
        
        Collections.sort(catList, new Comparator<CatDetail>() {
            @Override
            public int compare(CatDetail o1, CatDetail o2) {
                return o1.getMau().compareTo(o2.getMau());
            }
        });
    }

    void searchByLoai() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap loai can tim: ");
        String loai = scan.nextLine();
        
        for (CatDetail catDetail : catList) {
            if(catDetail.getLoai().equalsIgnoreCase(loai)) {
                catDetail.hienthi();
            }
        }
        System.out.println("========== KET THUC =============");
    }

    void sortByColorManager() {
        
    }
}

#ColorManager.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bai2;

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

/**
 *
 * @author diepvan
 */
public class ColorManager {
    ArrayList<String> colorList;
    
    public ColorManager() {
        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("Them ma mau: ");
            String color = scan.nextLine();
            
            colorList.add(color);
        }
    }
    
    public void hienthi() {
        System.out.println("Danh sach ma mau: ");
        for (int i = 0; i < colorList.size(); i++) {
            System.out.println((i + 1) + ". " + colorList.get(i));
        }
    }
}

#CatDetail.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bai2;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class CatDetail implements ICat{
    String loai;
    String mau;
    String 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;
    }
    
    @Override
    public void nhap() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap loai: ");
        loai = scan.nextLine();
        System.out.println("Nhap mau: ");
        mau = scan.nextLine();
        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;
    }
}

#ICat.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bai2;

/**
 *
 * @author diepvan
 */
public interface ICat {
    String ten = "MEO";
    void nhap();
    void hienthi();
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package bai2;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Main {
    static ColorManager colorManager = new ColorManager();
    static ManagerCat managerCat = new ManagerCat();
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch (choose) {
                case 1:
                    colorManager.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();
                    break;
                case 7:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
            }
        } 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 meo theo mau");
        System.out.println("5. Tim kiem theo loai");
        System.out.println("6. Sap xep meo theo ColorManager");
        System.out.println("7. Thoat");
        System.out.println("Chon: ");
    }
}
Tags:

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

5

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