By GokiSoft.com|
14:37 26/09/2022|
Java Basic
[Source Code] Java Basic- OOP - Interface - Quản lý mèo (cat) trong java - C2109I
Java Basic- OOP - Interface - Quản lý mèo (cat) trong java
ColorManager:
Red -> index: 0
Yellow -> index: 1
Blue -> index: 2
catList:
Loai: A, Mau: Blue (index: 2), NoiSong: A -> colorIndex = 2
Loai: C, Mau: Red (index: 0), NoiSong: C -> colorIndex = 0
Loai: B, Mau: Yellow (index: 1), NoiSong: B -> colorIndex = 1
-> Mau: A -> Z
catList:
Loai: A, Mau: Blue, NoiSong: A
Loai: C, Mau: Red, NoiSong: C
Loai: B, Mau: Yellow, NoiSong: B
-> Mau: Theo thu tu xuat hien cac mau trong ColorManager
Loai: C, Mau: Red, NoiSong: C
Loai: B, Mau: Yellow, NoiSong: B
Loai: A, Mau: Blue, NoiSong: A
-> Code nhu the nao? Giai thuat la gi???
#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 bt993;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class CatDetail implements ICat{
String loai;
String mau;
String noiSong;
int colorIndex;
public CatDetail() {
}
public CatDetail(String loai, String mau, String noiSong) {
this.loai = loai;
this.mau = mau;
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: ");
colorIndex = -1;
Main.colorManager.hienthi();
while(colorIndex == -1) {
mau = scan.nextLine();
colorIndex = Main.colorManager.getColorIndex(mau);
if(colorIndex == -1) {
System.out.println("Nhap lai: ");
}
}
System.out.println("Nhap noi song: ");
noiSong = scan.nextLine();
}
@Override
public void hienthi() {
System.out.println(this);
}
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 String toString() {
return "loai=" + loai + ", mau=" + mau + ", 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 bt993;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class ColorManager {
List<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("Nhap mau can them: ");
String color = scan.nextLine();
colorList.add(color);
}
}
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.println((i+1) + ". " + colorList.get(i));
}
}
}
#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 bt993;
/**
*
* @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 bt993;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static List<CatDetail> catList = new ArrayList<>();
public static ColorManager colorManager = new ColorManager();
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
colorManager.nhap();
break;
case 2:
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);
}
break;
case 3:
System.out.println("Danh sach meo: ");
for (CatDetail cat : catList) {
cat.hienthi();
}
break;
case 4:
System.out.println("Nhap loai can tim kiem: ");
String loai = scan.nextLine();
for (CatDetail cat : catList) {
if (cat.getLoai().equalsIgnoreCase(loai)) {
cat.hienthi();
}
}
break;
case 5:
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
return o1.getMau().compareToIgnoreCase(o2.getMau());
}
});
System.out.println("Danh sach meo: ");
for (CatDetail cat : catList) {
cat.hienthi();
}
break;
case 6:
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
if(o1.getColorIndex() > o2.getColorIndex()) {
return 1;
}
return -1;
}
});
System.out.println("Danh sach meo: ");
for (CatDetail cat : catList) {
cat.hienthi();
}
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");
System.out.println("4. Tim kiem theo loai");
System.out.println("5. Sap xep mau A-Z");
System.out.println("6. Sap xep mau theo Color Manager");
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)