1. Tạo lớp có tên Animal gồm các thuộc tính và phương thức:
· String name
· int age
· String description
· void display() //hiển thị tên, tuổi và mô tả của động vật
void input() //THuc hien nhap thong tin cua Animal
· abstract void showSound()
Tạo hàm tạo không đối và đầy đủ đối số
Tạo getter/setter
2. Tạo các lớp Tiger, Dog, Cat theo các yêu cầu sau:
3. Tạo lớp có tên Room gồm:
· int roomNo
· List<Animal> animalList
· void addAnimal(Animal a) //thêm một con vật vào AnimalList
· void removeAnimal(String name) //xóa con vật có tên tương ứng khỏi AnimalList
void display() -> Hiển thị thông tin mã chuồng + danh sách động vật
void input() -> Nhập mã chuồng
Hàm tạo ko đối và đầy đủ đối số
Tạo getter/setter
public static Animal createAnimal() -> Cho phép khởi tạo 1 động vật bất kỳ Tiger, Dog, Cat. (Hỏi người dùng động vật cần khỏi tạo 1. Tạo Tiger, 2. Tạo Dog, Khác -> Tạo Cat)
4. Tạo lớp có tên Zoo gồm:
· List<Animal> roomList
Tạo hàm tạo không tối -> Thực hiện khởi tạo roomList = new ArrayList<>() trong hàm tạo này.
void addAnimal(int roomNo, Animal a) -> Thực hiện thêm động vật vào chuồng theo mã roomNo
void removeAnimal(int roomNo, String name) -> Thực hiện xóa động vật theo tên -> trong chuồng có mã roomNo
· void addRoom(Room r) //thêm chuồng vào roomList
· void removeRoom(int roomNo) //xóa chuồng có mã tương ứng khỏi roomList
void display() -> Hiển thị thông tin toàn bộ room được quản lý trong Zoo.
5. Tạo lớp có tên TestZoo chứa phương thức main() để quản lý sở thú theo dạng Menu như sau:
6. Khi người dùng lựa chọn 3 yêu cầu người dùng nhập vào mã chuồng (roomNo) muốn thêm động vật. Và thực hiện thêm 1 động vật (Tiger, Dog, hoặc Cat) vào chuồng với roomNo vừa nhập
7. Khi người dùng lựa chọn 3 yêu cầu người dùng nhập vào mã chuồng (roomNo) muốn xóa động vật. Sau đó yêu cầu người dùng nhập tên động vật cần xóa -> Thực hiện xóa động vật đi.
8. Khi người dùng chọn 5 thì hiển thị thông tin cùng tiếng kêu của từng con vật trong sở thú.
Theo dõi cập nhật nội dung học trên Youtube & Facebook
Do Trung Duc [T2008A]
Ngày viết: 15:56 28/02/2021
/*
* 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 CatManager;
import java.util.Scanner;
/**
*
* @author TrungDuc
*/
public class Cat {
String name;
public Cat() {
}
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void NhapthongtinCat(){
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten meo: ");
name = scan.nextLine();
}
public void HienthiCat(){
System.out.println("Ten: "+ name);
}
}
Do Trung Duc [T2008A]
Ngày viết: 15:56 28/02/2021
/*
* 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 CatManager;
import java.util.Scanner;
/**
*
* @author TrungDuc
*/
public class CatDetail extends Cat {
String type;
String color;
String lifeAction;
public CatDetail() {
}
public CatDetail(String type, String color, String lifeAction, String name) {
super(name);
this.type = type;
this.color = color;
this.lifeAction = lifeAction;
}
@Override
public void NhapthongtinCat() {
Scanner scan = new Scanner(System.in);
super.NhapthongtinCat();
System.out.println("Nhap chung loai meo: ");
type = scan.nextLine();
System.out.println("Nhap mau sac meo: ");
color = scan.nextLine();
for(String color: ColorManager.colorList){
if(this.color.equalsIgnoreCase(color)){
return;
}else{
System.out.println("Mau vua nhap khong co trong bang mau,Nhap lai mau sac meo: ");
this.color = scan.nextLine();
}
}
System.out.println("Nhap tap tinh meo: ");
lifeAction = scan.nextLine();
}
@Override
public void HienthiCat() {
super.HienthiCat();
System.out.println("Loai: " + type);
System.out.println("Mau sac: " + color);
System.out.println("Tap tinh: " + lifeAction);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLifeAction() {
return lifeAction;
}
public void setLifeAction(String lifeAction) {
this.lifeAction = lifeAction;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
Do Trung Duc [T2008A]
Ngày viết: 15:56 28/02/2021
/*
* 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 CatManager;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author TrungDuc
*/
public class ColorManager {
String colorNo;
static ArrayList<String> colorList;
public ColorManager() {
this.colorList = new ArrayList<>();
}
public ColorManager(String colorNo, ArrayList<String> colorList) {
this.colorNo = colorNo;
this.colorList = colorList;
}
public String getColorNo() {
return colorNo;
}
public void setColorNo(String colorNo) {
this.colorNo = colorNo;
}
public ArrayList<String> getColorList() {
return colorList;
}
public void setColorList(ArrayList<String> colorList) {
this.colorList = colorList;
}
public void inputColor(String color) {
colorList.add(color);
}
}
Do Trung Duc [T2008A]
Ngày viết: 15:56 28/02/2021
/*
* 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 CatManager;
import java.util.ArrayList;
/**
*
* @author TrungDuc
*/
public class ManagerCat {
ArrayList<CatDetail> catList;
public ManagerCat() {
this.catList = new ArrayList<>();
}
public void addCat(CatDetail cat) {
catList.add(cat);
}
public void DisplayallCat() {
for (int i = 0; i < catList.size(); i++) {
System.out.format("Con meo thu %d : \n", (i + 1));
catList.get(i).HienthiCat();
}
}
public void DisplayallCatByColor(String color) {
for (CatDetail cat : catList) {
if (cat.getColor().equalsIgnoreCase(color)) {
cat.HienthiCat();
}
}
}
public void DisplayallCatByType(String type) {
for (CatDetail cat : catList) {
if (cat.getType().equalsIgnoreCase(type)) {
cat.HienthiCat();
}
}
}
}
Do Trung Duc [T2008A]
Ngày viết: 15:56 28/02/2021
/*
* 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 CatManager;
import java.util.Scanner;
/**
*
* @author TrungDuc
*/
public class UsingManagerCat {
public static void main(String[] agrs) {
int choose;
ManagerCat managerCat = new ManagerCat();
ColorManager colorManager = new ColorManager();
do {
Menu();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap lua chon choose = ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 0:
System.out.println("Nhap mau sac: ");
String color = scan.nextLine();
colorManager.inputColor(color);
break;
case 1:
System.out.println("Nhap so luong meo muon nhap N = ");
int N = Integer.parseInt(scan.nextLine());
for (int i = 0; i < N; i++) {
CatDetail newCat = new CatDetail();
newCat.NhapthongtinCat();
managerCat.addCat(newCat);
}
break;
case 2:
managerCat.DisplayallCat();
break;
case 3:
System.out.println("Nhap mau sac ban muon tim = ");
String colorFind = scan.nextLine();
managerCat.DisplayallCatByColor(colorFind);
break;
case 4:
System.out.println("Nhap chung loai ban muon tim = ");
String typeFind = scan.nextLine();
managerCat.DisplayallCatByColor(typeFind);
break;
case 5:
for (String onecolor : colorManager.colorList) {
for (CatDetail cat : managerCat.catList) {
if (cat.getColor().equalsIgnoreCase(onecolor)) {
cat.HienthiCat();
}
}
}
break;
case 6:
System.out.println("THoat");
break;
}
} while (choose != 6);
}
public static void Menu() {
System.out.println("0.Nhập danh sách mã màu");
System.out.println("1.Nhập thông tin của n con mèo");
System.out.println("2.Hiển thị thông tin");
System.out.println("3.Sắp xếp danh sách theo mau");
System.out.println("4.Tìm kiếm thông tin theo loai");
System.out.println("5. Sắp xếp danh sách màu theo bảng màu trong lớp ColorManager");
System.out.println("6. Thoát");
}
}