By GokiSoft.com| 19:08 26/04/2024|
Java Basic

[Share Code] ava Basic- OOP - Tổng hợp - Quản lý sở thú Xem Danh Sách Lớp - C2307L

ava Basic- OOP - Tổng hợp - Quản lý sở thú Xem Danh Sách Lớp

#Test.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

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

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        Animal a1 = new Tiger();
        Tiger t1 = new Tiger();
        
        ArrayList<Animal> animals = new ArrayList<>();
        List<Animal> animals2 = new ArrayList<>();
    }
}

#Zoo.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

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

/**
 *
 * @author diepvan
 */
public class Zoo {
    ArrayList<Room> roomList;

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

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

    public void setRoomList(ArrayList<Room> roomList) {
        this.roomList = roomList;
    }
    
    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();
                break;
            }
        }
    }
    
    public void removeAnimal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong can xoa dong vat: ");
        int roomNo = Integer.parseInt(scan.nextLine());
        
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                room.removeAnimal();
                break;
            }
        }
    }
    
    public void addRoom() {
        System.out.println("======= THEM ROOM ==========");
        Room room = new Room();
        room.input();
        
        roomList.add(room);
        System.out.println("======= XONG ========");
    }
    
    public void removeRoom() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong can xoa: ");
        int roomNo = Integer.parseInt(scan.nextLine());
        boolean isFind = false;
        
        for (Room room : roomList) {
            if(room.getRoomNo() == roomNo) {
                roomList.remove(room);
                isFind = true;
                break;
            }
        }
        
        if(!isFind) {
            System.out.println("Ko tim thay chuong can xoa");
        }
    }
    
    public void display() {
        System.out.println("THONG TIN ZOO");
        for (Room room : roomList) {
            room.display();
        }
        System.out.println("==============");
    }
}

#Room.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

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

/**
 *
 * @author diepvan
 */
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 animal = createAnimal();
        
        animalList.add(animal);
    }
    
    public void removeAnimal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten dong vat can xoa: ");
        String name = scan.nextLine();
        
        for (Animal animal : animalList) {
            if(animal.getName().equalsIgnoreCase(name)) {
                animalList.remove(animal);
            }
        }
    }
    
    public void display() {
        System.out.println("Ma chuong: " + roomNo);
        System.out.println("Danh sach dong vat: ");
        for (Animal animal : animalList) {
            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 can them: ");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            addAnimal();
        }
    }
    
    public Animal createAnimal() {
        Animal animal;
        
        Scanner scan = new Scanner(System.in);
        System.out.println("1. Tao Tiger");
        System.out.println("2. Tao Dog");
        System.out.println("3. 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();
        }
        
        animal.input();
        
        return animal;
    }
}

#Cat.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

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

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

#Dog.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

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

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

#Tiger.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

/**
 *
 * @author diepvan
 */
public class Tiger extends Animal{

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

#Animal.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public abstract class Animal {
    String name;
    int age;
    String description;

    public Animal() {
    }

    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 String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    
    public void display() {
        System.out.println(this);
        System.out.println("Am thanh: ");
        showSound();
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap mo ta: ");
        description = scan.nextLine();
    }
    
    public abstract void showSound();

    @Override
    public String toString() {
        return "name=" + name + ", age=" + age + ", description=" + description;
    }
    
    
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson08;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Main {

    public static Zoo zoo = new Zoo();

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    zoo.addRoom();
                    break;
                case 2:
                    zoo.removeRoom();
                    break;
                case 3:
                    zoo.addAnimal();
                    break;
                case 4:
                    zoo.removeAnimal();
                    break;
                case 5:
                    zoo.display();
                    break;
                case 6:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while (choose != 6);
    }

    public 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 thong tin");
        System.out.println("6. Thoat");
        System.out.println("Chon: ");
    }
}
Tags:

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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó