By GokiSoft.com|
15:10 23/06/2023|
Java Basic
[Share Code] Java Basic- OOP - Interface - Quản lý mèo (cat) trong java - C2209I
Java Basic- OOP - Interface - Quản lý mèo (cat) trong java
#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 com.gokisoft.c2209i.bt993;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class CatDetail implements ICat {
String loai;
String mau;
String noiSong;
int colorIndex;
public CatDetail() {
}
@Override
public void nhap() {
System.out.println("===== NHAP THON TIN MEO =======");
Scanner scan = new Scanner(System.in);
System.out.println("Nhap loai: ");
loai = scan.nextLine();
//Nhap mau -> danh sach colorManager
//Lay danh sach mau trong ColorManager > Main.colorManager
while(true) {
Main.colorManager.hienthi();
System.out.println("Nhap mau: ");
mau = scan.nextLine();
colorIndex = Main.colorManager.checkColor(mau);
if(colorIndex >= 0) {
break;
}
}
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;
}
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
/*
* 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 com.gokisoft.c2209i.bt993;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class ColorManager implements ICat{
ArrayList<String> colorList;
public ColorManager() {
colorList = new ArrayList<>();
}
public ArrayList<String> getColorList() {
return colorList;
}
@Override
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);
}
}
@Override
public void hienthi() {
System.out.println("Danh sach mau: ");
for (int i = 0; i < colorList.size(); i++) {
System.out.println(i + ". " + colorList.get(i));
}
}
public int checkColor(String color) {
for (int i = 0; i < colorList.size(); i++) {
if(colorList.get(i).equalsIgnoreCase(color)) {
return i;
}
}
return -1;
}
}
#ICat.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.gokisoft.c2209i.bt993;
/**
*
* @author teacher
*/
public interface ICat {
//public final String TEN = "MEO";
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 com.gokisoft.c2209i.bt993;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Main {
static ArrayList<CatDetail> catList = new ArrayList<>();
static Scanner scan = new Scanner(System.in);
public static ColorManager colorManager = new ColorManager();
public static void main(String[] args) {
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:
sortByColor();
break;
case 5:
findByType();
break;
case 6:
sortByColorManager();
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 con meo");
System.out.println("3. Hien thi");
System.out.println("4. Sap xep theo mau");
System.out.println("5. Tim thong tin theo loai");
System.out.println("6. Sap xep theo ColorManager");
System.out.println("7. Thoat");
System.out.println("Chon: ");
}
public static void inputCat() {
if(colorManager.getColorList().isEmpty()) {
System.out.println("Yeu cau 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 catDetail = new CatDetail();
catDetail.nhap();
catList.add(catDetail);
}
}
public static void displayCat() {
System.out.println("Thong tin danh sach MEO: ");
for (CatDetail catDetail : catList) {
catDetail.hienthi();
}
// for (int i = 0; i < catList.size(); i++) {
// catList.get(i).hienthi();
// }
}
public static void sortByColor() {
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
//1: Doi cho, -1: Khong doi cho -> TEST lai.
return o1.getMau().compareToIgnoreCase(o2.getMau());
}
});
displayCat();
}
public static void findByType() {
String loai;
System.out.println("Nhap loai can tim kiem: ");
loai = scan.nextLine();
for (int i = 0; i < catList.size(); i++) {
if(catList.get(i).getLoai().equalsIgnoreCase(loai)) {
catList.get(i).hienthi();
}
}
}
public static void sortByColorManager() {
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
if(o1.getColorIndex() > o2.getColorIndex()) {
return 1;
}
return -1;
}
});
displayCat();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)