By GokiSoft.com| 10:22 14/07/2021|
Java Basic

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

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


#Zoo.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 lesson05.bt990;

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

/**
 *
 * @author Diep.Tran
 */
public class Zoo {
    ArrayList<Room> roomList;
    
    public Zoo() {
        roomList = new ArrayList<>();
    }
    
    public void addRoom(Room r) {
        roomList.add(r);
    }
    
    public void removeRoom(int roomNo) {
        for (int i = 0; i < roomList.size(); i++) {
            if(roomList.get(i).getRoomNo() == roomNo) {
                roomList.remove(i);
                i--;
            }
        }
    }
    
    public void addAnimal(int roomNo, Animal a) {
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.addAnimal(a);
            }
        }
    }
    
    public void addAnimal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong can them dong vat: ");
        int roomNo = Integer.parseInt(scan.nextLine());
        
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.addAnimal();
            }
        }
    }
    
    public void removeAnimal(int roomNo, String name) {
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.removeAnimal(name);
            }
        }
    }
    
    public void removeAniamal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong can xoa dong vat: ");
        int roomNo = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap ten dong vat can xoa: ");
        String name = scan.nextLine();
        
        removeAnimal(roomNo, name);
    }
    
    public void display() {
        for (Room room : roomList) {
            room.display();
        }
    }
}


#Tiger.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 lesson05.bt990;

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

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


#TestZoo.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 lesson05.bt990;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class TestZoo {
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        Scanner scan = new Scanner(System.in);
        int choose;
        
        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 can xoa: ");
                    int roomNo = Integer.parseInt(scan.nextLine());
                    
                    zoo.removeRoom(roomNo);
                    break;
                case 3:
                    zoo.addAnimal();
                    break;
                case 4:
                    zoo.removeAniamal();
                    break;
                case 5:
                    zoo.display();
                    break;
                case 6:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 6);
    }
    
    static void showMenu() {
        System.out.println("1. Them chuong");
        System.out.println("2. Xoa chuong");
        System.out.println("3. Them dong vat");
        System.out.println("4. Xoa dong vat");
        System.out.println("5. Xem tat ca dong vat");
        System.out.println("6. Thoat");
        System.out.println("Chon: ");
    }
}


#Room.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 lesson05.bt990;

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

/**
 *
 * @author Diep.Tran
 */
public class Room {
    int roomNo;
    ArrayList<Animal> animalList;
    
    public Room() {
        animalList = new ArrayList<>();
    }

    public int getRoomNo() {
        return roomNo;
    }

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

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

    public void setAnimalList(ArrayList<Animal> animalList) {
        this.animalList = animalList;
    }
    
    public void addAnimal(Animal a) {
        animalList.add(a);
    }
    
    public void addAnimal() {
        Animal animal = createAnimal();
        
        animalList.add(animal);
    }
    
    /**
     * TH1: name se la duy nhat
     * TH2: name dong co the trung ten -> code theo cach nay.
     * @param name 
     */
    public void removeAnimal(String name) {
        for (int i = 0; i < animalList.size(); i++) {
            if(animalList.get(i).getName().equalsIgnoreCase(name)) {
                animalList.remove(i);
                //1, 2, 3, 4, 5, 6: length = 6, index = 0 -> 5
                //Xoa di index = 2 (gia tri 3)-> Vi du xu ly dong 50
                //1, 2, 4, 5, 6: Length = 5, index = 0 -> 4
                //index = 2 (gia tri 3 truoc) -> index = 2 (gia tri 4)
                i--;
            }
        }
    }
    
    public void display() {
        System.out.println("Ma Chuong: " + roomNo);
        for (Animal animal : animalList) {
            animal.showSound();
            animal.display();
        }
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ma chuong: ");
        roomNo = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap so dong vat");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            Animal animal = createAnimal();
            animalList.add(animal);
        }
    }
    
    public Animal createAnimal() {
        Scanner scan = new Scanner(System.in);
        Animal animal;
        
        System.out.println("1. Tao Tiger");
        System.out.println("2. Tao Dog");
        System.out.println("Khac. Tao Cat");
        System.out.println("Chon: ");
        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;
    }
}


#Dog.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 lesson05.bt990;

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

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


#Cat.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 lesson05.bt990;

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

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


#Animal.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 lesson05.bt990;

import java.util.Scanner;

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

    public Animal() {
    }

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

    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;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        
        System.out.println("Nhap mo ta: ");
        description = scan.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
    }
    
    public void display() {
        System.out.format("\nTen: %s, tuoi: %d, mo ta: %s\n", name, age, description);
    }
    
    public abstract void showSound();
}


Tags:

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

5

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