By GokiSoft.com|
09:20 16/07/2021|
Java Basic
[Share Code] Java Basic- OOP - Interface - Quản lý mèo (cat) trong java - C2010G
Java Basic- OOP - Interface - Quản lý mèo (cat) trong java
#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 lesson06.bt993;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class UsingManagerCat {
static ColorManager colorManager = new ColorManager();
static ManagerCat managerCat = new ManagerCat();
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:
managerCat.nhap(colorManager);
break;
case 3:
managerCat.hienthi();
break;
case 4:
managerCat.sortByColor();
managerCat.hienthi();
break;
case 5:
managerCat.searchingByLoai();
break;
case 6:
managerCat.sortByColorIndex();
managerCat.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 danh sach ma mau");
System.out.println("2. Nhap n Meo");
System.out.println("3. Hien thi thong tin Meo");
System.out.println("4. Sap xep danh sach theo mau");
System.out.println("5. Tim kiem thon tin theo loai");
System.out.println("6. Sap xep theo index bang mau ColorManager");
System.out.println("7. Thoat");
System.out.println("Chon: ");
}
}
#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 lesson06.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 nhap(ColorManager colorManager) {
if(colorManager.getColorList().isEmpty()) {
System.out.println("Khong the nhap Meo khi bang mau rong!!!");
return;
}
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 catDetail = new CatDetail();
catDetail.nhap(colorManager);
catList.add(catDetail);
}
}
public void hienthi() {
for (CatDetail catDetail : catList) {
catDetail.hienthi();
}
}
public void sortByColor() {
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
return o1.getMau().compareToIgnoreCase(o2.getMau());
}
});
}
public void searchingByLoai() {
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();
}
}
}
public void sortByColorIndex() {
Collections.sort(catList, new Comparator<CatDetail>() {
@Override
public int compare(CatDetail o1, CatDetail o2) {
if(o1.getColorIndex() > o2.getColorIndex()) {
return 1;
}
return -1;
}
});
}
}
#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 lesson06.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 lesson06.bt993;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class ColorManager {
ArrayList<String> colorList;
public ColorManager() {
colorList = new ArrayList<>();
}
public void nhap() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ma mau moi: ");
String color = scan.nextLine();
if(colorList.contains(color)) {
System.out.println("Ma mau nay da ton tai -> " + color);
} else {
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));
}
}
public ArrayList<String> getColorList() {
return colorList;
}
public void setColorList(ArrayList<String> colorList) {
this.colorList = colorList;
}
}
#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 lesson06.bt993;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class CatDetail implements ICat {
/**
* mau -> gia tri -> nam trong lop ColorManager.
*/
String loai, mau, 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 noi song: ");
noisong = scan.nextLine();
}
public void nhap(ColorManager colorManager) {
nhap();
ArrayList<String> colorList = colorManager.getColorList();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap mau: ");
while (true) {
colorManager.hienthi();
mau = scan.nextLine();
boolean isFind = false;
for (int i = 0; i < colorList.size(); i++) {
if (colorList.get(i).equalsIgnoreCase(mau)) {
colorIndex = i;
isFind = true;
break;
}
}
if (isFind) {
break;
}
System.out.println("Yeu cau nhap lai ma mau: ");
//
// if(colorManager.getColorList().contains(mau)) {
// break;
// } else {
// System.out.println("Yeu cau nhap lai ma mau: ");
// }
}
}
@Override
public void hienthi() {
System.out.printf("\nLoai: %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;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)