By GokiSoft.com| 15:06 26/06/2023|
Java Basic

Java Basic- OOP - Tổng hợp - Quản lý sở thú

HỆ THỐNG QUẢN LÝ SỞ THÚ

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:

  • Thừa kế từ lớp Animal
  • Ghi đè phương thức showSound() để thể hiện những tiếng kêu đặc trưng của từng loài vật

3. Tạo lớp có tên Room gồm:

· int roomNo

· ​List<Animal> animalList

· void addAnimal() //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 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<Room> 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() -> Thực hiện thêm động vật vào chuồng theo mã roomNo

  void removeAnimal() -> Thực hiện xóa động vật theo tên -> trong chuồng có mã roomNo

· void addRoom() //thêm chuồng vào roomList

· void removeRoom() //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:

  1. Thêm chuồng
  2. Xóa chuồng
  3. Thêm con vật
  4. Xóa con vật
  5. Xem tất cả các con vật
  6. Thoát

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ú.

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

5

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

Do Trung Duc [T2008A]
Do Trung Duc

2021-02-28 02:28:40



/*
 * 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 zoomanager;

import java.util.Scanner;

/**
 *
 * @author TrungDuc
 */
public class Animal {

    String name;
    int age;

    public Animal() {

    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    public void Input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten dong vat");
        name = scan.nextLine();
        
        System.out.println("Nhap tuoi dong vat");
        age = Integer.parseInt(scan.nextLine());
    }
    
    public void display(){
        System.out.format("Ten: %s, Tuoi: %d", name, age);
    }

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-02-28 02:28:28



/*
 * 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 zoomanager;

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

/**
 *
 * @author TrungDuc
 */
public class Room {

    String roomNo;
    List<Animal> listAnimal;

    public Room() {
        this.listAnimal = new ArrayList<>();
    }

    public Room(String roomNo) {
        this.roomNo = roomNo;
        this.listAnimal = new ArrayList<>();
    }

    public String getRoomNo() {
        return roomNo;
    }

    public void setRoomNo(String roomNo) {
        this.roomNo = roomNo;
    }

    public List getListAnimal() {
        return listAnimal;
    }

    public void setListAnimal(List listAnimal) {
        this.listAnimal = listAnimal;
    }

    public void addAnimal(Animal animal) {
        listAnimal.add(animal);
    }

    public void deleteAnimal() {
        
                Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten dong vat can xoa: ");
        String name = scan.nextLine();

        for (Animal animal : listAnimal) {
               if(animal.getName().equalsIgnoreCase(name)){
                   listAnimal.remove(animal);
               }
        }
    }
    
    public static Animal creatAnimal(){
        createAnimalMenu();
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap lua chon khoi tao doi tuon option: ");
        
        int option = Integer.parseInt(scan.nextLine());
        
        Animal animal = null;
        switch(option){
            case 1:
                animal = new Tiger();
                break;
            case 2:
                animal = new Lion();
                break;
            case 3:
                animal = new Dog();
                break;
            default:
                 System.out.println("Nhap lai option: ");
        }
        animal.Input();
        return animal;
    }
    
    public static void createAnimalMenu(){
        System.out.println("1.Khoi tao Tiger");
        System.out.println("1.Khoi tao Lion");
        System.out.println("1.Khoi tao Dog");
    }
    
    public void Display(){
        System.out.println("Ma Chuong: " + roomNo);
        for(Animal animal : listAnimal ){
            animal.display();
        }
    }
    
    public void Input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap Ma Chuong: ");
        roomNo = scan.nextLine();
    }

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-02-28 02:28:14



/*
 * 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 zoomanager;

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

/**
 *
 * @author TrungDuc
 */
public class Zoo {

    List<Room> roomList;

    public Zoo() {
        roomList = new ArrayList<>();
    }

  

    public Zoo(List<Room> roomList) {
        this.roomList = roomList;
        roomList = new ArrayList<>();
    }

    public void createRoom() {
        Room room = new Room();
        room.Input();
        roomList.add(room);
    }

    public void removeRoom() {
        Scanner scan = new Scanner(System.in);
        System.out.format("Nhap ma chuong can xoa: ");
        String deleteRoomNo = scan.nextLine();

        for (Room room : roomList) {
            boolean findroom = true;
            if (room.getRoomNo().equalsIgnoreCase(deleteRoomNo)) {
                roomList.remove(room);
                findroom = false;
            }
            if (findroom == true) {
                System.out.format("Khong tim thay phong de xoa: ");
            }
        }
    }

    public void updateAnimal() {
        Scanner scan = new Scanner(System.in);
        System.out.format("Nhap ma chuong can them dong vat: ");
        String roolNo = scan.nextLine();

        for (Room room : roomList) {
            if (room.getRoomNo().equalsIgnoreCase(roolNo)) {
                Animal animal = Room.creatAnimal();
                room.addAnimal(animal);
                return;
            }
            System.out.format("Khong tim thay phong de xoa: ");
        }
    }

    public void deleteAnimal() {
        Scanner scan = new Scanner(System.in);
        System.out.format("Nhap ma chuong can xoa dong vat: ");
        String roolNo = scan.nextLine();

        for (Room room : roomList) {
            if (room.getRoomNo().equalsIgnoreCase(roolNo)) {
                room.deleteAnimal();
                return;
            }
            System.out.format("Khong tim thay ma chuong de xoa: " + roolNo);
        }

    }
    
    public void DisplayAll(){
        for(Room room: roomList){
            room.Display();
        }
    }

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-02-28 02:27:53



/*
 * 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 zoomanager;

import java.util.Scanner;

/**
 *
 * @author TrungDuc1
 */
public class ZooManager {


    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        int choose;

        do {
            Menu();
            Scanner scan = new Scanner(System.in);
            System.out.println("Nhap lua chon choose: ");
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    zoo.createRoom();
                    break;

                case 2:
                    zoo.updateAnimal();
                    break;

                case 3:
                    zoo.removeRoom();
                    break;

                case 4:
                    zoo.deleteAnimal();
                    break;

                case 5:
                    zoo.DisplayAll();
                    break;

                case 6:
                    System.out.println("6.Thoat....");
                    break;

                default:
                    System.out.println("Nhap lai choose: ");
                    break;
            }

        } while (choose != 6);

    }

   static void Menu() {
        System.out.println("1.Them chuong");
        System.out.println("2.Them dong vat vao chuong");
        System.out.println("3.Xoa chuong");
        System.out.println("4.Xoa dong vat trong chuong");
        System.out.println("5.Hien thi tat ca cac dong vat");
        System.out.println("6.Thoat chuong trinh");
    }

}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:37:45



/*
 * 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 baitap_new;

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

/**
 *
 * @author Administrator
 */

public class Tiger extends Animal{

    @Override
    public void showSound() {
       System.out.println("Gum,Gum,Gum...");
    }
    
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:35:57



/*
 * 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 baitap_new;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class Zoo {
     List<Room> roomList; 

    public Zoo() {
        roomList = new ArrayList<>();
    }
    
    public void removeAnimal(int roomNo, String name) {
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.removeAnimal(name);
                return;
            }
        }
        System.out.println("Khong tim thay roomNo phu hop >> " + roomNo);
    }
    
    public void addAnimal(int roomNo, Animal animal) {
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.addAnimal(animal);
                return;
            }
        }
        System.out.println("Khong tim thay roomNo phu hop >> " + roomNo);
    }
    
    public void addRoom(Room room) {
        roomList.add(room);
    }
    
    public void display() {
        for (Room room : roomList) {
            room.display();
        }
    }
    
    public void removeRoom(int roomNo) {
        boolean isFind = false;
        
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                System.out.println("Xoa thanh cong!!!");
                roomList.remove(room);
                isFind = true;
            }
        }
        
        if(!isFind) {
            System.out.println("Khong tim thay ma phong nao phu hop >> " + roomNo);
        }
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:35:19



/*
 * 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 baitap_new;

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

/**
 *
 * @author Administrator
 */

public class TestZoo {
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        
        Scanner scan = new Scanner(System.in);
        int choose, roomNo;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    Room room = new Room();
                    room.input();
                    
                    zoo.addRoom(room);
                    break;
                case 2:
                    System.out.println("Nhap ma chuong(roomNo) can xoa: ");
                    roomNo = Integer.parseInt(scan.nextLine());
                    
                    zoo.removeRoom(roomNo);
                    break;
                case 3:
                    System.out.println("Nhap ma room can them dong vat: ");
                    roomNo = Integer.parseInt(scan.nextLine());
                    
                    Animal animal = Room.createAnimal();
                    
                    zoo.addAnimal(roomNo, animal);
                    break;
                case 4:
                    System.out.println("Nhap ma room can xoa dong vat: ");
                    roomNo = Integer.parseInt(scan.nextLine());
                    
                    System.out.println("Nhap ten dong vat can xoa: ");
                    String name = scan.nextLine();
                    
                    zoo.removeAnimal(roomNo, name);
                    break;
                case 5:
                    zoo.display();
                    break;
                case 6:
                    System.out.println("Thoat chuong trinh.");
                    break;
                default:
                    System.out.println("Nhap lai!!!");
                    break;
            }
        } while(choose != 6);
    }
    
    static void showMenu() {
        System.out.println("1. Them Room");
        System.out.println("2. Xoa Room");
        System.out.println("3. Them Animal");
        System.out.println("4. Xoa Animal");
        System.out.println("5. Xem tat ca dong vat");
        System.out.println("6. Thoat");
        System.out.println("Chon: ");
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:33:07



/*
 * 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 baitap_new;

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

/**
 *
 * @author Administrator
 */
public class Room {
    int roomNo;
    List<Animal> animalList;
    public Room(){
        this.animalList = new ArrayList<>();   
    }
    public Room(int roomNo){
        this.roomNo = roomNo;
        this.animalList = new ArrayList<>();
    }
    public void addAnimal(Animal abc){
        animalList.add(abc);
    }
    public void removeAnimal(String name){
         for (Animal animal : animalList) {
            if(animal.getName().equalsIgnoreCase(name)) {
                animalList.remove(animal);
            }
        }
    }
    public void input(){
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap vao roomNo");
        roomNo = Integer.parseInt(scan.nextLine());
    }
    public void display(){
        System.out.println("Ma phong:" + roomNo);
        for (Animal animal : animalList) {
            animal.display();
        }
    }
    public static Animal createAnimal(){
        Scanner scan = new Scanner(System.in);
        Animal animal;
        
        showMenu();
        int choose = Integer.parseInt(scan.nextLine());
        
        switch(choose) {
            case 1:
                animal = new Tiger();
                break;
            case 2:
                animal = new Dog();
                break;
            default:
                animal = new Cat();
                break;
        }
        animal.input();
        
        return animal;
    }
    
    private static void showMenu() {
        System.out.println("1. Khoi tao Tiger");
        System.out.println("2. Khoi tao Dog");
        System.out.println("3. Khoi tao Cat");
        System.out.println("Chon: ");
    }

    public int getRoomNo() {
        return roomNo;
    }

    public void setRoomNo(int roomNo) {
        this.roomNo = roomNo;
    }

    public List<Animal> getAnimalList() {
        return animalList;
    }

    public void setAnimalList(List<Animal> animalList) {
        this.animalList = animalList;
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:32:43



/*
 * 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 baitap_new;

/**
 *
 * @author Administrator
 */
public class Dog extends Animal{

    @Override
    public void showSound() {
       System.out.println("Go,go,go...");
    }
    
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-02-26 14:32:27



/*
 * 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 baitap_new;

/**
 *
 * @author Administrator
 */
public class Cat extends Animal {

    @Override
    public void showSound() {
        System.out.println("Meo Meo Meo...");
    }
    
}