By GokiSoft.com| 13:53 24/03/2020|
Java Basic

Share Code- Chữa bài tập quản lý Zoo - quản lý sở thú - Zoo Project in Java - Zoo Project bằng java

Hướng dẫn chữa bài tập

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



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

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public abstract class Animal {
    String name, description;
    int age;

    public Animal() {
    }

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

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

    public Animal(String name, String description, int age) {
        this.name = name;
        this.description = description;
        this.age = age;
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter name: ");
        name = input.nextLine();
        
        System.out.println("Enter age: ");
        age = Integer.parseInt(input.nextLine());
        
        System.out.println("Enter description: ");
        description = input.nextLine();
    }
    
    public void display() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Animal{" + "name=" + name + ", description=" + description + ", age=" + age + '}';
    }
    
    public abstract void showSound();

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}



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

/**
 *
 * @author Diep.Tran
 */
public class Cat extends Animal{

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



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

/**
 *
 * @author Diep.Tran
 */
public class Dog extends Animal{

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



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

/**
 *
 * @author Diep.Tran
 */
public class Tiger extends Animal{

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



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

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

/**
 *
 * @author Diep.Tran
 */
public class Room {
    int roomCode;
    String roomName;
    ArrayList<Animal> animalList;

    public Room() {
        animalList = new ArrayList<>();
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Room Code: ");
        roomCode = Integer.parseInt(input.nextLine());
        
        System.out.println("Enter Room Name: ");
        roomName = input.nextLine();
    }
    
    public void addAnimal(Animal animal) {
        animalList.add(animal);
    }
    
    public void removeAnimal(String name) {
        for (Animal animal : animalList) {
            if(animal.getName().equalsIgnoreCase(name)) {
                animalList.remove(animal);
            }
        }
//        ArrayList<Animal> animalList2 = new ArrayList<>();
//        for (Animal animal : animalList) {
//            if(!animal.getName().equalsIgnoreCase(name)) {
//                animalList2.add(animal);
//            }
//        }
//        animalList = animalList2;
    }

    public int getRoomCode() {
        return roomCode;
    }

    public void setRoomCode(int roomCode) {
        this.roomCode = roomCode;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }
    
    public ArrayList<Animal> getAnimalList() {
        return animalList;
    }

    public void setAnimalList(ArrayList<Animal> animalList) {
        this.animalList = animalList;
    }
    
    public void display() {
        for (Animal animal : animalList) {
            animal.display();
            animal.showSound();
        }
    }
}



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

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Zoo {
    ArrayList<Room> roomList;

    public Zoo() {
        roomList = new ArrayList<>();
    }
    
    public void addRoom(Room room) {
        roomList.add(room);
    }

    public ArrayList<Room> getRoomList() {
        return roomList;
    }

    public void setRoomList(ArrayList<Room> roomList) {
        this.roomList = roomList;
    }
    
    public void removeRoom(int roomCode) {
        for (Room room : roomList) {
            if(room.getRoomCode() == roomCode) {
                roomList.remove(room);
                break;
            }
        }
    }
    
    public void display() {
        for (Room room : roomList) {
            room.display();
        }
    }
}



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

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        Scanner input = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    System.out.println("Enter number of room: ");
                    int n = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < n; i++) {
                        Room room = new Room();
                        room.input();
                        
                        zoo.addRoom(room);
                    }
                    break;
                case 2:
                    System.out.println("Enter room's name is deleted: ");
                    int roomCode = Integer.parseInt(input.nextLine());
                    zoo.removeRoom(roomCode);
                    break;
                case 3:
                    addAnimal(zoo);
                    break;
                case 4:
                    removeAnimal(zoo);
                    break;
                case 5:
                    zoo.display();
                    break;
                case 6:
                    System.out.println("Exit!!!");
                    break;
                default:
                    System.out.println("Input failed!!!");
                    break;
            }
        } while(choose != 6);
    }
    
    static void addAnimal(Zoo zoo) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of animal: ");
        int n = Integer.parseInt(input.nextLine());
        int choose;
        for (int i = 0; i < n; i++) {
            System.out.println("1. Create Tiger");
            System.out.println("2. Create Dog");
            System.out.println("Other. Create Cat");
            choose = Integer.parseInt(input.nextLine());
            
            Animal animal;
            
            switch(choose) {
                case 1:
                    animal = new Tiger();
                    break;
                case 2:
                    animal = new Dog();
                    break;
                default:
                    animal = new Cat();
                    break;
            }
            animal.input();
            
            System.out.println("==== Room List ====");
            Room room = null;
            for (int j = 0; j < zoo.getRoomList().size(); j++) {
                room = zoo.getRoomList().get(j);
                System.out.format("\n(%d) - %s", room.getRoomCode(), room.getRoomName());
            }
            System.out.println("\nChoose Room Code: ");
            int roomCode = Integer.parseInt(input.nextLine());
            for (int j = 0; j < zoo.getRoomList().size(); j++) {
                room = zoo.getRoomList().get(j);
                if(room.getRoomCode() == roomCode) {
                    break;
                } else {
                    room = null;
                }
            }
            
            if(room != null) {
                room.addAnimal(animal);
            }
        }
    }
    
    static void removeAnimal(Zoo zoo) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is animal's name you want to remove: ");
        String roomName = input.nextLine();
        
        for (Room room : zoo.getRoomList()) {
            room.removeAnimal(roomName);
        }
    }
    
    static void showMenu() {
        System.out.println("1. Thêm chuồng");
        System.out.println("2. Xóa chuồng");
        System.out.println("3. Thêm con vật");
        System.out.println("4. Xóa con vật");
        System.out.println("5. Xem tất cả các con vật");
        System.out.println("6. Thoat");
        System.out.println("Choose: ");
    }
}


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

5

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