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)

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

2021-02-26 14:32:05



/*
 * 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.Scanner;

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



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-02-26 09:07:38


#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 baitap;

/**
 *
 * @author dong
 */
public class Cat extends Animal{
    public Cat() {
    }

    public Cat(String Ten) {
        super(Ten);
    }

    public Cat(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Cat(String Ten, String MoTa, int Tuoi) {
        super(Ten, Tuoi, MoTa);
    }

    
    

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


#Chuong.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 baitap;

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

/**
 *
 * @author dong
 */
public class Chuong {

    static void addRoom(Chuong room) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    int maChuong;
    List<Animal> animalList;
    
    public Chuong(){
        this.animalList = new ArrayList<>();
    }
    
     public Chuong(int maChuong) {
        this.maChuong = maChuong;
        this.animalList = new ArrayList<>();
    }
    public void addAnimal(Animal animal) {
        animalList.add(animal);
    }
    public void removeAnimal(String Ten) {
        animalList.stream().filter((animal) -> (animal.getName().equalsIgnoreCase(Ten))).forEachOrdered((animal) -> {
            animalList.remove(animal);
        });
    }
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap vao roomNo: ");
        maChuong = Integer.parseInt(scan.nextLine());
    }
    public void display() {
        System.out.println("Ma phong: " + maChuong);
        animalList.forEach((animal) -> {
            animal.display();
        });
    }
    public static Animal createAnimal(){
        Scanner scan = new Scanner(System.in);
        Animal animal = null;
        showMenu();
        int choose = Integer.parseInt(scan.nextLine());
        
        switch(choose){
            case 1:
                animal = new Tiger();
                break;
            case 2:
                animal = new Dog();
                break;
            case 3:
                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 maChuong;
    }

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

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

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


#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 baitap;

/**
 *
 * @author dong
 */


public class Dog extends Animal{
    public Dog() {
    }

    public Dog(String Ten) {
        super(Ten);
    }

    public Dog(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Dog(String Ten, String MoTa, int Tuoi) {
        super(Ten, Tuoi, MoTa);
    }

    
    

    @Override
    public void tiengKeu() {
        System.out.println("gau ...");
    }
}




#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 baitap;

import java.util.Scanner;

/**
 *
 * @author dong
 */
public class testzoo {
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        Scanner scan = new Scanner(System.in);
        int choose = 0 , maChuong;
        
        do{
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
        switch(choose) {
            case 1:
                    Chuong room = new Chuong();
                    room.input();
                    
                    Chuong.addRoom(room);
                    break;
                case 2:
                    System.out.println("Nhap ma chuong(roomNo) can xoa: ");
                    maChuong = Integer.parseInt(scan.nextLine());
                    
                    zoo.removeRoom(maChuong);
                    break;
                case 3:
                    System.out.println("Nhap ma room can them dong vat: ");
                    maChuong = Integer.parseInt(scan.nextLine());
                    
                    Animal animal = Chuong.createAnimal();
                    
                    zoo.addAnimal(maChuong, animal);
                    break;
                case 4:
                    System.out.println("Nhap ma room can xoa dong vat: ");
                    maChuong = Integer.parseInt(scan.nextLine());
                    
                    System.out.println("Nhap ten dong vat can xoa: ");
                    String Ten = scan.nextLine();
                    
                    zoo.removeAnimal(maChuong, 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);
                
        
    }

    private static void showMenu() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}


#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 baitap;

/**
 *
 * @author dong
 */
public class Tiger extends Animal {
     public Tiger() {
    }

    public Tiger(String Ten) {
        super(Ten);
    }

    public Tiger(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Tiger(String Ten, String MoTa, int Tuoi) {
        super(Ten, Tuoi, MoTa);
    }

    
    

    @Override
    public void tiengKeu() {
        System.out.println("Gam ...");
    }
}


#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 baitap;

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

/**
 *
 * @author dong
 */
public class Zoo{
   List<Chuong> roomList;
   public Zoo(){
       roomList = new ArrayList<>();
   }
   public void removeAnimal(int maChuong, String Ten) {
        for (Chuong room : roomList) {
            if(room.getRoomNo() == maChuong) {
                room.removeAnimal(Ten);
                return;
            }
        }
        System.out.println("Khong tim thay roomNo phu hop >> " + maChuong);
    }
    public void addAnimal(int maChuong, Animal animal) {
        for (Chuong room : roomList) {
            if(room.getRoomNo() == maChuong) {
                room.addAnimal(animal);
                return;
            }
        }
        System.out.println("Khong tim thay roomNo phu hop >> " + maChuong);
    }
    
    public void addRoom(Chuong room) {
        roomList.add(room);
    }
    public void display() {
        for (Chuong room : roomList) {
            room.display();
        }
    
    }
    public void removeRoom(int roomNo) {
        boolean isFind = false;
        
        for (Chuong 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 ĐIỆP [Teacher]
TRẦN VĂN ĐIỆP

2021-02-26 07:50:22


#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 lesson07;

import java.util.Scanner;

/**
 *
 * @author DiepTV
 */
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 void display() {
        System.out.format("\nTen: %s, tuoi: %d, mo ta: %s\n", name, age, description);
    }
    
    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();

    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;
    }
}


#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 lesson07;

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

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


#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 lesson07;

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

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


#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 lesson07;

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

/**
 *
 * @author DiepTV
 */
public class Room {
    int roomNo;
    List<Animal> animalList; //null -> Tinh chat da hinh trong lap trinh OOP
    //child class: ArrayList, Vector, LinkedList => ke thua tu List => animalList khoi tao tu ArrayList, Vector hoac LinkedList

    public Room() {
        this.animalList = new ArrayList<>(); //Khoi tao tu class con.
    }

    public Room(int roomNo) {
        this.roomNo = roomNo;
        this.animalList = new ArrayList<>();
    }
    
    /**
     * abc -> animal (cach dat ten bien theo chuan coding convention.)
     * @param abc 
     */
    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;
    }
}


#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 lesson07;

import java.util.Scanner;

/**
 *
 * @author DiepTV
 */
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: ");
    }
}


#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 lesson07;

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

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


#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 lesson07;

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

/**
 *
 * @author DiepTV
 */
public class Zoo {
    List<Room> roomList; //Tinh chat da hinh -> ArrayList ke thua tu List.

    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);
        }
    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-02-25 16:10:29


#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 lesson6.QuanliSoThu;

/**
 *
 * @author MyPC
 */
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;
    }
    
    
    
    void xemThongTin(){
        System.out.println("Ten: " + name);
        System.out.println("Tuoi: " + age);
        System.out.println("Mo ta: " + description);
    }
    
    abstract void tiengKeu();
}


#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 lesson6.QuanliSoThu;

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

    public Cat() {
    }

    public Cat(String name) {
        super(name);
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    public Cat(String name, String description, int age) {
        super(name, description, age);
    }
    
    

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


#Chuong.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 lesson6.QuanliSoThu;

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

/**
 *
 * @author MyPC
 */
public class Chuong{
    int maChuong;
    ArrayList<Animal> AnimalList = new ArrayList<>();
    
    
}


#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 lesson6.QuanliSoThu;

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

    public Dog() {
    }

    public Dog(String name) {
        super(name);
    }

    public Dog(String name, int age) {
        super(name, age);
    }

    public Dog(String name, String description, int age) {
        super(name, description, age);
    }
    
    
    
    @Override
    void tiengKeu() {
        System.out.println("Gau gau...");
    }
    
}


#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 lesson6.QuanliSoThu;

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

/**
 *
 * @author MyPC
 */
public class TestZoo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Zoo zoo = new Zoo();
        Chuong chuong = new Chuong();
        int choose;
        do{
            Menu();
            System.out.println("Lua chon chuong trinh:");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    zoo.themChuong();
                break;
                case 2:
                    zoo.xoaChuong();
                break;
                case 3:
                    zoo.themConVat();
//                    for (Chuong x : zoo.DanhSachChuong) {
//                        System.out.println(x.maChuong);
//                    }
                break;
                case 4:
                    zoo.xoaConVat();
                break;
                case 5:
                    for (Chuong x : zoo.DanhSachChuong) {
                        System.out.println("Ma chuong: " + x.maChuong);
                        if( x.AnimalList.isEmpty() ){
                            System.out.println("Chuong nay khong co con nao");
                            continue;
                        }
                        for( int i = 0 ; i < x.AnimalList.size() ; i++ ){
                            x.AnimalList.get(i).tiengKeu();
                            x.AnimalList.get(i).xemThongTin();
                        }
                    }
                break;
                case 6:
                    System.out.println("Tam biet!!!");
                break;
                default:
                    System.out.println("Nhap sai!!!");
                break;
            }
        }while( choose != 6 );
    }
    
    static void Menu(){
        System.out.println("1.Them chuong");
        System.out.println("2.Xoa chuong");
        System.out.println("3.Them con vat");
        System.out.println("4.Xoa con vat");
        System.out.println("5.Xem tat ca cac con vat");
        System.out.println("6.Thoat");
    }
}


#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 lesson6.QuanliSoThu;

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

    public Tiger() {
    }

    public Tiger(String name) {
        super(name);
    }

    public Tiger(String name, int age) {
        super(name, age);
    }

    public Tiger(String name, String description, int age) {
        super(name, description, age);
    }
    
    

    @Override
    void tiengKeu() {
        System.out.println("Gru gru...");
    }
    
}


#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 lesson6.QuanliSoThu;

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

/**
 *
 * @author MyPC
 */
public class Zoo {
    ArrayList<Chuong> DanhSachChuong = new ArrayList<>();
    void themChuong() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong muon them:");
        int maChuong = scan.nextInt();
        Chuong chuong = new Chuong();
        chuong.maChuong = maChuong;
        DanhSachChuong.add(chuong);
    }
    
    void xoaChuong(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma chuong muon xoa:");
        int maChuong = scan.nextInt();
        for(int i = 0; i < DanhSachChuong.size(); i++){
            Chuong temp = DanhSachChuong.get(i);
            if(temp.maChuong == maChuong){
                DanhSachChuong.remove(i);
                break;
            }
        }
    }
    
    public void themConVat(){
        Scanner scan = new Scanner(System.in);
        animalOptions();
        System.out.println("Lua chon con vat:");
        int choose = Integer.parseInt(scan.nextLine());
        switch( choose ){
            case 1:
                Animal tiger = new Tiger();
                System.out.println("Ten:");
                tiger.name = scan.nextLine();
                System.out.println("Tuoi:");
                tiger.age = Integer.parseInt(scan.nextLine());
                System.out.println("Mo ta:");
                tiger.description = scan.nextLine();
                System.out.println("Chon ma chuong muon cho vao:");
                int maChuong = Integer.parseInt(scan.nextLine());
                for (Chuong x : DanhSachChuong) {
                    if( x.maChuong == maChuong ){
                        x.AnimalList.add(tiger);
                        break;
                    }
                }
                break;
            case 2:
                Animal dog = new Dog();
                System.out.println("Ten:");
                dog.name = scan.nextLine();
                System.out.println("Tuoi:");
                dog.age = Integer.parseInt(scan.nextLine());
                System.out.println("Mo ta:");
                dog.description = scan.nextLine();
                System.out.println("Chon ma chuong muon cho vao:");
                maChuong = Integer.parseInt(scan.nextLine());
                for (Chuong x : DanhSachChuong) {
                    if( x.maChuong == maChuong ){
                        x.AnimalList.add(dog);
                        break;
                    }
                }
                break;
            case 3:
                Animal cat = new Cat();
                System.out.println("Ten:");
                cat.name = scan.nextLine();
                System.out.println("Tuoi:");
                cat.age = Integer.parseInt(scan.nextLine());
                System.out.println("Mo ta:");
                cat.description = scan.nextLine();
                System.out.println("Chon ma chuong muon cho vao:");
                maChuong = Integer.parseInt(scan.nextLine());
                for (Chuong x : DanhSachChuong) {
                    if( x.maChuong == maChuong ){
                        x.AnimalList.add(cat);
                        break;
                    }
                }
                break;  
        }
    }
    public void animalOptions(){
        System.out.println("1.Ho");
        System.out.println("2.Cho");
        System.out.println("3.Meo");
    }
    
    public void xoaConVat(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ten con vat muon xoa:");
        String name = scan.nextLine();
        for (Chuong chuong : DanhSachChuong) {
            for( int i = 0 ; i < chuong.AnimalList.size() ; i++ ){
                if(chuong.AnimalList.get(i).name.equals(name) ){
                    chuong.AnimalList.remove(i);
                }
            }
        }
    }
}



Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-26 14:34:09



package quanlysothu;

public abstract class Animal {

	String ten;
	int tuoi;
	String moTa;
	
	void xemThongTin() {
	System.out.println("Ten : "+ten);
	System.out.println("Tuoi: "+ tuoi);
	System.out.println("Mo ta: "+ moTa);
	}
	
	abstract void tiengKeu() ;
	
	public Animal() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ten
	 */
	public Animal(String ten) {
		super();
		this.ten = ten;
	}

	/**
	 * @param ten
	 * @param tuoi
	 */
	public Animal(String ten, int tuoi) {
		super();
		this.ten = ten;
		this.tuoi = tuoi;
	}

	/**
	 * @param ten
	 * @param tuoi
	 * @param moTa
	 */
	public Animal(String ten, int tuoi, String moTa) {
		super();
		this.ten = ten;
		this.tuoi = tuoi;
		this.moTa = moTa;
	}
	
	void Add() {
	}
	
	void Add(String ten) {
		this.ten= ten;
	}
	
	void Add(String ten, int tuoi) {
		this.ten= ten;
		this.tuoi = tuoi;
	}
	
	void Add(String ten, int tuoi, String moTa) {
		this.ten= ten;
		this.tuoi = tuoi;
		this.moTa = moTa;
	}
}

package quanlysothu;

public class Tiger extends Animal{

	@Override
	void tiengKeu() {
		// TODO Auto-generated method stub
		System.out.println("roar ... roar... roar!!!");
	}

	public void Add(String ten, int tuoi, String moTa) {
		super.Add(ten, tuoi, moTa);
		
	}
	
}

package quanlysothu;

public class Dog extends Animal{

	@Override
	void tiengKeu() {
		// TODO Auto-generated method stub
		System.out.println("bark bark bark....bark");
	}

	
	public void Add(String ten, int tuoi, String moTa) {
		super.Add(ten, tuoi, moTa);
		
	}
	
	
}

package quanlysothu;

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

public class TestZoo {

	public static void main(String[] args) {
		
		Scanner  sc = new Scanner(System.in);
		
		ArrayList<Animal> AnimalList = new ArrayList<Animal>();
		
		ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
		
		Chuong c = new Chuong();
		Zoo zoo = new Zoo();
		Dog dog = new Dog();
		Tiger tiger = new Tiger();
		Cat cat = new Cat();
		
		int n;
		do {
			Menu();
			System.out.println("Moi ban nhap lua chon: ");
			n = Integer.parseInt(sc.nextLine());
			boolean exit = false;
			switch(n) {
			case 1:
				System.out.println("1. Them chuong");
				
				System.out.println("Ma chuong: ");
				int maChuong = Integer.parseInt(sc.nextLine());
				c = new Chuong();
				zoo.themChuong(c, maChuong);
				DanhSachChuong = zoo.getZoo();
				break;
			case 2:
				System.out.println("2. Xoa chuong");
				
				System.out.println("Moi ban nhap ma chuong muon xoa: ");
				
				int ma = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < DanhSachChuong.size(); i++) {
					if(DanhSachChuong.get(i).getMaChuong() == ma) {
					zoo.xoaChuong(ma);
					} else {
						System.out.println("Khong co chuong nao");
					}
				}
				break;
			case 3:
				System.out.println("3. Them con vat");
				System.out.println("Moi ban nhap ma chuong: ");
                int flag_1 = 2;
                int maChuong_ = Integer.parseInt(sc.nextLine());
                for (int i = 0; i < DanhSachChuong.size(); i++) {
                    if (maChuong_ == DanhSachChuong.get(i).maChuong) {
                        flag_1 = 1;
                    }
                }
                if (flag_1 == 1) {
                    c = new Chuong();
                    AnimalList = c.getChuong();
                    System.out.println("Nhập con vật 1.Tiger,2.Dog,3.Cat : ");
                    int ten = Integer.parseInt(sc.nextLine());

                    if (ten == 1) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        tiger.Add(Ten, Tuoi, MoTa);
                        c.themConVat(tiger);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, tiger);
                    }
                    if (ten == 2) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        dog.Add(Ten, Tuoi, MoTa);
                        c.themConVat(dog);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, dog);
                    }
                    if (ten == 3) {
                        System.out.println("Ten : ");
                        String Ten =sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        cat.Add(Ten, Tuoi, MoTa);
                        c.themConVat(cat);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, cat);
                    }
                } else {
                    System.out.println("Khong co chuong nao ");
                }
				break;
			case 4:
				System.out.println("4. Xoa con vat");
				
				System.out.println("Nhap ma chuong chứa con vật muốn xóa : ");
                int maChuong2 = Integer.parseInt(sc.nextLine());
                System.out.println("Nhap ten con vat vừa tạo: ");
                String ten = sc.nextLine();
                String choicemm = sc.nextLine();
                switch (choicemm) {
                    case "1":
                        for (int i = 0; i < DanhSachChuong.size(); i++) {
                            if (DanhSachChuong.get(i).maChuong == maChuong2) {
                                for (int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
                                    if (DanhSachChuong.get(i).animalList.get(j).ten.equals(ten) == true) {
                                        DanhSachChuong.get(i).animalList.remove(j);
                                    }
                                }
                            }

                        }
                        break;
                    case "2":
                        c.xoaConVat(dog);
                        break;
                    case "3":
                        c.xoaConVat(cat);
                        break;
                
                }
				break;
			case 5:
				System.out.println("5. Xem tat ca cac con vat");
				
				for( int i = 0; i < DanhSachChuong.size(); i++) {
					System.out.println(DanhSachChuong.get(i).maChuong);
					ArrayList<Animal> list = DanhSachChuong.get(i).animalList;
					System.out.println("________________________________________________");
					for(int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
						System.out.println(DanhSachChuong.get(i).animalList.get(j).ten);
						System.out.println(list.get(j).tuoi);
						System.out.println(list.get(j).moTa);
						list.get(j).tiengKeu();
					}
					System.out.println("________________________________________________");
				}
				break;
			case 6:
				System.out.println("Goodbye!");
				exit = true;
				break;
			default:
				System.err.println("Nhap sai! Moi nhap lai.");
				break;
			}
		}while(n != 6);
	}
	
	static void Menu() {
		System.out.println("===========================");
		System.out.println("1. Them chuong");
		System.out.println("2. Xoa chuong");
		System.out.println("3. Them con vat");
		System.out.println("4. Xoa con vat");
		System.out.println("5. Xem tat ca cac con vat");
		System.out.println("6. Thoat");
		System.out.println("===========================");
	}
}

package quanlysothu;

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

public class Chuong {

    Scanner  sc = new Scanner(System.in);
	
	int maChuong;
	
	public int getMaChuong() {
		return maChuong;
	}

	public void setMaChuong(int maChuong) {
		this.maChuong = maChuong;
	}

	ArrayList<Animal> animalList = new ArrayList<Animal>();
	
	void themConVat(Animal a) {
		animalList.add(a);
	}
	
	void xoaConVat(Animal a) {
		animalList.remove(a);
//		System.out.println("Moi ban nhap ten con vat muon xoa: ");
//		 ten = sc.nextLine();
//		
//		Iterator<Animal> art = animalList.iterator();
//		while(art.hasNext()) {
//			Animal an = art.next();
//			if(an.equals(ten)) {
//				art.remove();
//			}
//		}
//		
//		for( Animal an : animalList) {
//			System.out.println(an);
//		}
		
	}
	
	public ArrayList<Animal> getChuong(){
		return animalList;
	}
}

package quanlysothu;

import java.util.ArrayList;

public class Zoo {

	ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
	
	public Zoo() {
		// TODO Auto-generated constructor stub
	}
	
	void themChuong(Chuong c, int maChuong) {
		c.maChuong = maChuong;
		DanhSachChuong.add(c);
	}
	
	void xoaChuong(int maChuong) {
		for( int i = 0; i < DanhSachChuong.size(); i++){
			if(DanhSachChuong.get(i).getMaChuong() == maChuong) {
				DanhSachChuong.remove(i);
			}
		}
	}

	void capNhatChuong(int maChuong, ArrayList c, Animal a) {
		for(int i = 0; i< DanhSachChuong.size(); i++) {
			if(DanhSachChuong.get(i).getMaChuong() == maChuong) {
				DanhSachChuong.get(i).animalList.add(a);
			}
		}
	}
	
	public ArrayList<Chuong> getZoo(){
		return DanhSachChuong;
	}
	
}

package quanlysothu;

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

public class TestZoo {

	public static void main(String[] args) {
		
		Scanner  sc = new Scanner(System.in);
		
		ArrayList<Animal> AnimalList = new ArrayList<Animal>();
		
		ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
		
		Chuong c = new Chuong();
		Zoo zoo = new Zoo();
		Dog dog = new Dog();
		Tiger tiger = new Tiger();
		Cat cat = new Cat();
		
		int n;
		do {
			Menu();
			System.out.println("Moi ban nhap lua chon: ");
			n = Integer.parseInt(sc.nextLine());
			boolean exit = false;
			switch(n) {
			case 1:
				System.out.println("1. Them chuong");
				
				System.out.println("Ma chuong: ");
				int maChuong = Integer.parseInt(sc.nextLine());
				c = new Chuong();
				zoo.themChuong(c, maChuong);
				DanhSachChuong = zoo.getZoo();
				break;
			case 2:
				System.out.println("2. Xoa chuong");
				
				System.out.println("Moi ban nhap ma chuong muon xoa: ");
				
				int ma = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < DanhSachChuong.size(); i++) {
					if(DanhSachChuong.get(i).getMaChuong() == ma) {
					zoo.xoaChuong(ma);
					} else {
						System.out.println("Khong co chuong nao");
					}
				}
				break;
			case 3:
				System.out.println("3. Them con vat");
				System.out.println("Moi ban nhap ma chuong: ");
                int flag_1 = 2;
                int maChuong_ = Integer.parseInt(sc.nextLine());
                for (int i = 0; i < DanhSachChuong.size(); i++) {
                    if (maChuong_ == DanhSachChuong.get(i).maChuong) {
                        flag_1 = 1;
                    }
                }
                if (flag_1 == 1) {
                    c = new Chuong();
                    AnimalList = c.getChuong();
                    System.out.println("Nhập con vật 1.Tiger,2.Dog,3.Cat : ");
                    int ten = Integer.parseInt(sc.nextLine());

                    if (ten == 1) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        tiger.Add(Ten, Tuoi, MoTa);
                        c.themConVat(tiger);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, tiger);
                    }
                    if (ten == 2) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        dog.Add(Ten, Tuoi, MoTa);
                        c.themConVat(dog);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, dog);
                    }
                    if (ten == 3) {
                        System.out.println("Ten : ");
                        String Ten =sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        cat.Add(Ten, Tuoi, MoTa);
                        c.themConVat(cat);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, cat);
                    }
                } else {
                    System.out.println("Khong co chuong nao ");
                }
				break;
			case 4:
				System.out.println("4. Xoa con vat");
				
				System.out.println("Nhap ma chuong chứa con vật muốn xóa : ");
                int maChuong2 = Integer.parseInt(sc.nextLine());
                System.out.println("Nhap ten con vat vừa tạo: ");
                String ten = sc.nextLine();
                String choicemm = sc.nextLine();
                switch (choicemm) {
                    case "1":
                        for (int i = 0; i < DanhSachChuong.size(); i++) {
                            if (DanhSachChuong.get(i).maChuong == maChuong2) {
                                for (int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
                                    if (DanhSachChuong.get(i).animalList.get(j).ten.equals(ten) == true) {
                                        DanhSachChuong.get(i).animalList.remove(j);
                                    }
                                }
                            }

                        }
                        break;
                    case "2":
                        c.xoaConVat(dog);
                        break;
                    case "3":
                        c.xoaConVat(cat);
                        break;
                
                }
				break;
			case 5:
				System.out.println("5. Xem tat ca cac con vat");
				
				for( int i = 0; i < DanhSachChuong.size(); i++) {
					System.out.println(DanhSachChuong.get(i).maChuong);
					ArrayList<Animal> list = DanhSachChuong.get(i).animalList;
					System.out.println("________________________________________________");
					for(int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
						System.out.println(DanhSachChuong.get(i).animalList.get(j).ten);
						System.out.println(list.get(j).tuoi);
						System.out.println(list.get(j).moTa);
						list.get(j).tiengKeu();
					}
					System.out.println("________________________________________________");
				}
				break;
			case 6:
				System.out.println("Goodbye!");
				exit = true;
				break;
			default:
				System.err.println("Nhap sai! Moi nhap lai.");
				break;
			}
		}while(n != 6);
	}
	
	static void Menu() {
		System.out.println("===========================");
		System.out.println("1. Them chuong");
		System.out.println("2. Xoa chuong");
		System.out.println("3. Them con vat");
		System.out.println("4. Xoa con vat");
		System.out.println("5. Xem tat ca cac con vat");
		System.out.println("6. Thoat");
		System.out.println("===========================");
	}
}






Phạm Kim Anh [JavaFree]
Phạm Kim Anh

2020-03-26 14:34:09



package quanlysothu;

public abstract class Animal {

	String ten;
	int tuoi;
	String moTa;
	
	void xemThongTin() {
	System.out.println("Ten : "+ten);
	System.out.println("Tuoi: "+ tuoi);
	System.out.println("Mo ta: "+ moTa);
	}
	
	abstract void tiengKeu() ;
	
	public Animal() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ten
	 */
	public Animal(String ten) {
		super();
		this.ten = ten;
	}

	/**
	 * @param ten
	 * @param tuoi
	 */
	public Animal(String ten, int tuoi) {
		super();
		this.ten = ten;
		this.tuoi = tuoi;
	}

	/**
	 * @param ten
	 * @param tuoi
	 * @param moTa
	 */
	public Animal(String ten, int tuoi, String moTa) {
		super();
		this.ten = ten;
		this.tuoi = tuoi;
		this.moTa = moTa;
	}
	
	void Add() {
	}
	
	void Add(String ten) {
		this.ten= ten;
	}
	
	void Add(String ten, int tuoi) {
		this.ten= ten;
		this.tuoi = tuoi;
	}
	
	void Add(String ten, int tuoi, String moTa) {
		this.ten= ten;
		this.tuoi = tuoi;
		this.moTa = moTa;
	}
}

package quanlysothu;

public class Tiger extends Animal{

	@Override
	void tiengKeu() {
		// TODO Auto-generated method stub
		System.out.println("roar ... roar... roar!!!");
	}

	public void Add(String ten, int tuoi, String moTa) {
		super.Add(ten, tuoi, moTa);
		
	}
	
}

package quanlysothu;

public class Dog extends Animal{

	@Override
	void tiengKeu() {
		// TODO Auto-generated method stub
		System.out.println("bark bark bark....bark");
	}

	
	public void Add(String ten, int tuoi, String moTa) {
		super.Add(ten, tuoi, moTa);
		
	}
	
	
}

package quanlysothu;

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

public class TestZoo {

	public static void main(String[] args) {
		
		Scanner  sc = new Scanner(System.in);
		
		ArrayList<Animal> AnimalList = new ArrayList<Animal>();
		
		ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
		
		Chuong c = new Chuong();
		Zoo zoo = new Zoo();
		Dog dog = new Dog();
		Tiger tiger = new Tiger();
		Cat cat = new Cat();
		
		int n;
		do {
			Menu();
			System.out.println("Moi ban nhap lua chon: ");
			n = Integer.parseInt(sc.nextLine());
			boolean exit = false;
			switch(n) {
			case 1:
				System.out.println("1. Them chuong");
				
				System.out.println("Ma chuong: ");
				int maChuong = Integer.parseInt(sc.nextLine());
				c = new Chuong();
				zoo.themChuong(c, maChuong);
				DanhSachChuong = zoo.getZoo();
				break;
			case 2:
				System.out.println("2. Xoa chuong");
				
				System.out.println("Moi ban nhap ma chuong muon xoa: ");
				
				int ma = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < DanhSachChuong.size(); i++) {
					if(DanhSachChuong.get(i).getMaChuong() == ma) {
					zoo.xoaChuong(ma);
					} else {
						System.out.println("Khong co chuong nao");
					}
				}
				break;
			case 3:
				System.out.println("3. Them con vat");
				System.out.println("Moi ban nhap ma chuong: ");
                int flag_1 = 2;
                int maChuong_ = Integer.parseInt(sc.nextLine());
                for (int i = 0; i < DanhSachChuong.size(); i++) {
                    if (maChuong_ == DanhSachChuong.get(i).maChuong) {
                        flag_1 = 1;
                    }
                }
                if (flag_1 == 1) {
                    c = new Chuong();
                    AnimalList = c.getChuong();
                    System.out.println("Nhập con vật 1.Tiger,2.Dog,3.Cat : ");
                    int ten = Integer.parseInt(sc.nextLine());

                    if (ten == 1) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        tiger.Add(Ten, Tuoi, MoTa);
                        c.themConVat(tiger);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, tiger);
                    }
                    if (ten == 2) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        dog.Add(Ten, Tuoi, MoTa);
                        c.themConVat(dog);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, dog);
                    }
                    if (ten == 3) {
                        System.out.println("Ten : ");
                        String Ten =sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        cat.Add(Ten, Tuoi, MoTa);
                        c.themConVat(cat);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, cat);
                    }
                } else {
                    System.out.println("Khong co chuong nao ");
                }
				break;
			case 4:
				System.out.println("4. Xoa con vat");
				
				System.out.println("Nhap ma chuong chứa con vật muốn xóa : ");
                int maChuong2 = Integer.parseInt(sc.nextLine());
                System.out.println("Nhap ten con vat vừa tạo: ");
                String ten = sc.nextLine();
                String choicemm = sc.nextLine();
                switch (choicemm) {
                    case "1":
                        for (int i = 0; i < DanhSachChuong.size(); i++) {
                            if (DanhSachChuong.get(i).maChuong == maChuong2) {
                                for (int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
                                    if (DanhSachChuong.get(i).animalList.get(j).ten.equals(ten) == true) {
                                        DanhSachChuong.get(i).animalList.remove(j);
                                    }
                                }
                            }

                        }
                        break;
                    case "2":
                        c.xoaConVat(dog);
                        break;
                    case "3":
                        c.xoaConVat(cat);
                        break;
                
                }
				break;
			case 5:
				System.out.println("5. Xem tat ca cac con vat");
				
				for( int i = 0; i < DanhSachChuong.size(); i++) {
					System.out.println(DanhSachChuong.get(i).maChuong);
					ArrayList<Animal> list = DanhSachChuong.get(i).animalList;
					System.out.println("________________________________________________");
					for(int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
						System.out.println(DanhSachChuong.get(i).animalList.get(j).ten);
						System.out.println(list.get(j).tuoi);
						System.out.println(list.get(j).moTa);
						list.get(j).tiengKeu();
					}
					System.out.println("________________________________________________");
				}
				break;
			case 6:
				System.out.println("Goodbye!");
				exit = true;
				break;
			default:
				System.err.println("Nhap sai! Moi nhap lai.");
				break;
			}
		}while(n != 6);
	}
	
	static void Menu() {
		System.out.println("===========================");
		System.out.println("1. Them chuong");
		System.out.println("2. Xoa chuong");
		System.out.println("3. Them con vat");
		System.out.println("4. Xoa con vat");
		System.out.println("5. Xem tat ca cac con vat");
		System.out.println("6. Thoat");
		System.out.println("===========================");
	}
}

package quanlysothu;

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

public class Chuong {

    Scanner  sc = new Scanner(System.in);
	
	int maChuong;
	
	public int getMaChuong() {
		return maChuong;
	}

	public void setMaChuong(int maChuong) {
		this.maChuong = maChuong;
	}

	ArrayList<Animal> animalList = new ArrayList<Animal>();
	
	void themConVat(Animal a) {
		animalList.add(a);
	}
	
	void xoaConVat(Animal a) {
		animalList.remove(a);
//		System.out.println("Moi ban nhap ten con vat muon xoa: ");
//		 ten = sc.nextLine();
//		
//		Iterator<Animal> art = animalList.iterator();
//		while(art.hasNext()) {
//			Animal an = art.next();
//			if(an.equals(ten)) {
//				art.remove();
//			}
//		}
//		
//		for( Animal an : animalList) {
//			System.out.println(an);
//		}
		
	}
	
	public ArrayList<Animal> getChuong(){
		return animalList;
	}
}

package quanlysothu;

import java.util.ArrayList;

public class Zoo {

	ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
	
	public Zoo() {
		// TODO Auto-generated constructor stub
	}
	
	void themChuong(Chuong c, int maChuong) {
		c.maChuong = maChuong;
		DanhSachChuong.add(c);
	}
	
	void xoaChuong(int maChuong) {
		for( int i = 0; i < DanhSachChuong.size(); i++){
			if(DanhSachChuong.get(i).getMaChuong() == maChuong) {
				DanhSachChuong.remove(i);
			}
		}
	}

	void capNhatChuong(int maChuong, ArrayList c, Animal a) {
		for(int i = 0; i< DanhSachChuong.size(); i++) {
			if(DanhSachChuong.get(i).getMaChuong() == maChuong) {
				DanhSachChuong.get(i).animalList.add(a);
			}
		}
	}
	
	public ArrayList<Chuong> getZoo(){
		return DanhSachChuong;
	}
	
}

package quanlysothu;

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

public class TestZoo {

	public static void main(String[] args) {
		
		Scanner  sc = new Scanner(System.in);
		
		ArrayList<Animal> AnimalList = new ArrayList<Animal>();
		
		ArrayList<Chuong> DanhSachChuong = new ArrayList<Chuong>();
		
		Chuong c = new Chuong();
		Zoo zoo = new Zoo();
		Dog dog = new Dog();
		Tiger tiger = new Tiger();
		Cat cat = new Cat();
		
		int n;
		do {
			Menu();
			System.out.println("Moi ban nhap lua chon: ");
			n = Integer.parseInt(sc.nextLine());
			boolean exit = false;
			switch(n) {
			case 1:
				System.out.println("1. Them chuong");
				
				System.out.println("Ma chuong: ");
				int maChuong = Integer.parseInt(sc.nextLine());
				c = new Chuong();
				zoo.themChuong(c, maChuong);
				DanhSachChuong = zoo.getZoo();
				break;
			case 2:
				System.out.println("2. Xoa chuong");
				
				System.out.println("Moi ban nhap ma chuong muon xoa: ");
				
				int ma = Integer.parseInt(sc.nextLine());
				for(int i = 0; i < DanhSachChuong.size(); i++) {
					if(DanhSachChuong.get(i).getMaChuong() == ma) {
					zoo.xoaChuong(ma);
					} else {
						System.out.println("Khong co chuong nao");
					}
				}
				break;
			case 3:
				System.out.println("3. Them con vat");
				System.out.println("Moi ban nhap ma chuong: ");
                int flag_1 = 2;
                int maChuong_ = Integer.parseInt(sc.nextLine());
                for (int i = 0; i < DanhSachChuong.size(); i++) {
                    if (maChuong_ == DanhSachChuong.get(i).maChuong) {
                        flag_1 = 1;
                    }
                }
                if (flag_1 == 1) {
                    c = new Chuong();
                    AnimalList = c.getChuong();
                    System.out.println("Nhập con vật 1.Tiger,2.Dog,3.Cat : ");
                    int ten = Integer.parseInt(sc.nextLine());

                    if (ten == 1) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        tiger.Add(Ten, Tuoi, MoTa);
                        c.themConVat(tiger);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, tiger);
                    }
                    if (ten == 2) {
                        System.out.println("Ten : ");
                        String Ten = sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        dog.Add(Ten, Tuoi, MoTa);
                        c.themConVat(dog);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, dog);
                    }
                    if (ten == 3) {
                        System.out.println("Ten : ");
                        String Ten =sc.nextLine();
                        System.out.println("Tuoi : ");
                        int Tuoi = Integer.parseInt(sc.nextLine());
                        System.out.println("Mo Ta : ");
                        String MoTa = sc.nextLine();
                        cat.Add(Ten, Tuoi, MoTa);
                        c.themConVat(cat);
                        AnimalList = c.getChuong();
                        zoo.capNhatChuong(maChuong_, AnimalList, cat);
                    }
                } else {
                    System.out.println("Khong co chuong nao ");
                }
				break;
			case 4:
				System.out.println("4. Xoa con vat");
				
				System.out.println("Nhap ma chuong chứa con vật muốn xóa : ");
                int maChuong2 = Integer.parseInt(sc.nextLine());
                System.out.println("Nhap ten con vat vừa tạo: ");
                String ten = sc.nextLine();
                String choicemm = sc.nextLine();
                switch (choicemm) {
                    case "1":
                        for (int i = 0; i < DanhSachChuong.size(); i++) {
                            if (DanhSachChuong.get(i).maChuong == maChuong2) {
                                for (int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
                                    if (DanhSachChuong.get(i).animalList.get(j).ten.equals(ten) == true) {
                                        DanhSachChuong.get(i).animalList.remove(j);
                                    }
                                }
                            }

                        }
                        break;
                    case "2":
                        c.xoaConVat(dog);
                        break;
                    case "3":
                        c.xoaConVat(cat);
                        break;
                
                }
				break;
			case 5:
				System.out.println("5. Xem tat ca cac con vat");
				
				for( int i = 0; i < DanhSachChuong.size(); i++) {
					System.out.println(DanhSachChuong.get(i).maChuong);
					ArrayList<Animal> list = DanhSachChuong.get(i).animalList;
					System.out.println("________________________________________________");
					for(int j = 0; j < DanhSachChuong.get(i).animalList.size(); j++) {
						System.out.println(DanhSachChuong.get(i).animalList.get(j).ten);
						System.out.println(list.get(j).tuoi);
						System.out.println(list.get(j).moTa);
						list.get(j).tiengKeu();
					}
					System.out.println("________________________________________________");
				}
				break;
			case 6:
				System.out.println("Goodbye!");
				exit = true;
				break;
			default:
				System.err.println("Nhap sai! Moi nhap lai.");
				break;
			}
		}while(n != 6);
	}
	
	static void Menu() {
		System.out.println("===========================");
		System.out.println("1. Them chuong");
		System.out.println("2. Xoa chuong");
		System.out.println("3. Them con vat");
		System.out.println("4. Xoa con vat");
		System.out.println("5. Xem tat ca cac con vat");
		System.out.println("6. Thoat");
		System.out.println("===========================");
	}
}






Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-20 09:59:41



/*
 * 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 Redmibook 14
 */
public abstract class Animal {

    public String Ten, MoTa;
    int Tuoi;

    public void xemThongTin() {
        System.out.println("Ten : " + Ten);
        System.out.println("Tuoi : " + Tuoi);
        System.out.println("Mo Ta : " + MoTa);

    }

    public abstract void tiengKeu();

    void Add() {
    }
    public String getTen(){
        return Ten;
    }
    void Add(String Ten) {
        this.Ten = Ten;
    }

    void Add(String Ten, int Tuoi) {
        this.Ten = Ten;
        this.Tuoi = Tuoi;
    }

    void Add(String Ten, int Tuoi, String MoTa) {
        this.Ten = Ten;
        this.Tuoi = Tuoi;
        this.MoTa = MoTa;
    }

}



/*
 * 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 Redmibook 14
 */
public class Cat extends Animal {
    @Override
    public void tiengKeu(){
         System.out.println("Meo Meo");
    };
    @Override
    public void Add(String Ten,int Tuoi,String MoTa){
        super.Add(Ten,Tuoi,MoTa);
    }
}



/*
 * 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 Redmibook 14
 */
public class Dog extends Animal {

    @Override
    public void tiengKeu() {
         System.out.println("Gau GAu");
    }

    ;
    @Override
    public void Add(String Ten, int Tuoi, String MoTa) {
        super.Add(Ten, Tuoi, MoTa);
    }
}



/*
 * 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 Redmibook 14
 */
public class Tiger extends Animal {
    @Override
    public void tiengKeu(){
        System.out.println("Grrr");
    };
    @Override
    public void Add(String Ten,int Tuoi,String MoTa){
        super.Add(Ten,Tuoi,MoTa);
    }
}



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

/**
 *
 * @author Redmibook 14
 */
public class Chuong {

    public int maChuong;
    public int getMaChuong(){
        return maChuong;
    } 
    ArrayList<Animal> AnimalList = new ArrayList();

    public void themConVat(Animal a) {
        AnimalList.add(a);
    }
    
    public void xoaConVat(Animal a) {
        AnimalList.remove(a);
    }
     public ArrayList<Animal> getChuong() {
        return AnimalList;
    }
    
}



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

/**
 *
 * @author Redmibook 14
 */
public class Zoo {

    ArrayList<Chuong> DanhSachChuong = new ArrayList();

    public void themChuong(Chuong c, int machuong) {
        c.maChuong = machuong;
        DanhSachChuong.add(c);

    }
      public void capNhatChuong(int machuong,ArrayList c,Animal a) {

        for (int i = 0; i < DanhSachChuong.size(); i++) {
            if (DanhSachChuong.get(i).maChuong == machuong) {
                
                DanhSachChuong.get(i).AnimalList.add(a);
            }
        }
    }
    public void xoaChuong(int machuong) {

        for (int i = 0; i < DanhSachChuong.size(); i++) {
            if (DanhSachChuong.get(i).maChuong == machuong) {
                DanhSachChuong.remove(i);
            }
        }
    }

    public ArrayList<Chuong> getZoo() {
        return DanhSachChuong;
    }
}



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

/**
 *
 * @author Redmibook 14
 */
public class TestZoo {

    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        Chuong c = new Chuong();
        Dog dog = new Dog();
        Tiger tiger = new Tiger();
        Cat cat = new Cat();
        ArrayList<Animal> AnimalList = new ArrayList<Animal>();

        ArrayList<Chuong> DanhSachChuong = new ArrayList();
        Scanner input = new Scanner(System.in);
        while (true) {
            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.Thoát");
            String choice = input.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("Mã chuồng : ");
                    int maChuong = Integer.parseInt(input.nextLine());
                    c = new Chuong();
                    zoo.themChuong(c, maChuong);
                    DanhSachChuong = zoo.getZoo();

                    break;
                case "2":
                    System.out.println("Mã chuồng đã tạo : ");
                    int flag = 2;
                    int ma = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < DanhSachChuong.size(); i++) {
                        if (ma == DanhSachChuong.get(i).maChuong) {
                            flag = 1;
                        }
                    }
                    if (flag == 1) {
                        zoo.xoaChuong(ma);
                    } else {
                        System.out.println("Khong co chuong nao ");
                    }

                    break;
                case "3":
                    System.out.println("Mã chuồng đã tạo : ");
                    int flag_1 = 2;
                    int maChuong_ = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < DanhSachChuong.size(); i++) {
                        if (maChuong_ == DanhSachChuong.get(i).maChuong) {
                            flag_1 = 1;
                        }
                    }
                    if (flag_1 == 1) {
                        c = new Chuong();
                        AnimalList = c.getChuong();
                        System.out.println("Nhập con vật 1.Tiger,2.Dog,3.Cat : ");
                        int ten = Integer.parseInt(input.nextLine());

                        if (ten == 1) {
                            System.out.println("Ten : ");
                            String Ten = input.nextLine();
                            System.out.println("Tuoi : ");
                            int Tuoi = Integer.parseInt(input.nextLine());
                            System.out.println("Mo Ta : ");
                            String MoTa = input.nextLine();
                            tiger.Add(Ten, Tuoi, MoTa);
                            c.themConVat(tiger);
                            AnimalList = c.getChuong();
                            zoo.capNhatChuong(maChuong_, AnimalList, tiger);
                        }
                        if (ten == 2) {
                            System.out.println("Ten : ");
                            String Ten = input.nextLine();
                            System.out.println("Tuoi : ");
                            int Tuoi = Integer.parseInt(input.nextLine());
                            System.out.println("Mo Ta : ");
                            String MoTa = input.nextLine();
                            dog.Add(Ten, Tuoi, MoTa);
                            c.themConVat(dog);
                            AnimalList = c.getChuong();
                            zoo.capNhatChuong(maChuong_, AnimalList, dog);
                        }
                        if (ten == 3) {
                            System.out.println("Ten : ");
                            String Ten = input.nextLine();
                            System.out.println("Tuoi : ");
                            int Tuoi = Integer.parseInt(input.nextLine());
                            System.out.println("Mo Ta : ");
                            String MoTa = input.nextLine();
                            cat.Add(Ten, Tuoi, MoTa);
                            c.themConVat(cat);
                            AnimalList = c.getChuong();
                            zoo.capNhatChuong(maChuong_, AnimalList, cat);
                        }
                    } else {
                        System.out.println("Khong co chuong nao ");
                    }

                    break;

                case "4":
                    System.out.println("Nhap ma chuong chứa con vật muốn xóa : ");
                    int maChuong2 = Integer.parseInt(input.nextLine());
                    System.out.println("Nhap ten con vat vừa tạo: ");
                    String ten = input.nextLine();
                    String choicemm = input.nextLine();
                    switch (choicemm) {
                        case "1":
                            for (int i = 0; i < DanhSachChuong.size(); i++) {
                                if (DanhSachChuong.get(i).maChuong == maChuong2) {
                                    for (int j = 0; j < DanhSachChuong.get(i).AnimalList.size(); j++) {
                                        if (DanhSachChuong.get(i).AnimalList.get(j).Ten.equals(ten) == true) {
                                            DanhSachChuong.get(i).AnimalList.remove(j);
                                        }
                                    }
                                }

                            }
                            break;
                        case "2":
                            c.xoaConVat(dog);
                            break;
                        case "3":
                            c.xoaConVat(cat);
                            break;
                    }
                    break;

                case "5":
                    for (int i = 0; i < DanhSachChuong.size(); i++) {
                        System.out.println(DanhSachChuong.get(i).maChuong);
                        ArrayList<Animal> arr = DanhSachChuong.get(i).AnimalList;
                        System.out.println("_______________________________");
                        for (int j = 0; j < DanhSachChuong.get(i).AnimalList.size(); j++) {
                            System.out.println(DanhSachChuong.get(i).AnimalList.get(j).Ten);
                            System.out.println(arr.get(j).Tuoi);
                            System.out.println(arr.get(j).MoTa);
                            arr.get(j).tiengKeu();
                            System.out.println(".........");
                        }
                        System.out.println("_______________________________");
                    }
                    break;
                case "6":
                    return;
            }
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-03-19 15:24:58



package zoo;

public abstract class Animal {
    String Ten;
    int Tuoi;
    String MoTa;

    public Animal() {
    }

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

    public Animal(String Ten, int Tuoi) {
        this.Ten = Ten;
        this.Tuoi = Tuoi;
    }

    public Animal(String Ten, int Tuoi, String MoTa) {
        this.Ten = Ten;
        this.Tuoi = Tuoi;
        this.MoTa = MoTa;
    }

    public String getTen() {
        return Ten;
    }

    public void setTen(String Ten) {
        this.Ten = Ten;
    }

    public int getTuoi() {
        return Tuoi;
    }

    public void setTuoi(int Tuoi) {
        this.Tuoi = Tuoi;
    }

    public String getMoTa() {
        return MoTa;
    }

    public void setMoTa(String MoTa) {
        this.MoTa = MoTa;
    }
    
    public void output(){
        System.out.println("Ten dong vat: "+Ten);
        System.out.println("Tuoi dong vat: "+Tuoi);
        System.out.println("Mo Ta dong vat: "+MoTa);
    }
    
    abstract void TiengKeu();
}



package zoo;

public class Cat extends Animal{

    public Cat() {
    }

    public Cat(String Ten) {
        super(Ten);
    }

    public Cat(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Cat(String Ten, int Tuoi, String MoTa) {
        super(Ten, Tuoi, MoTa);
    }

    @Override
    void TiengKeu() {
        System.out.println("Meow Meow");
    }
    
}



package zoo;

public class Dog extends Animal{

    public Dog() {
    }

    public Dog(String Ten) {
        super(Ten);
    }

    public Dog(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Dog(String Ten, int Tuoi, String MoTa) {
        super(Ten, Tuoi, MoTa);
    }
    
    @Override
    void TiengKeu() {
        System.out.println("Gau Gau");
    }
    
}



package zoo;

public class Tiger extends Animal{

    public Tiger() {
    }

    public Tiger(String Ten) {
        super(Ten);
    }

    public Tiger(String Ten, int Tuoi) {
        super(Ten, Tuoi);
    }

    public Tiger(String Ten, int Tuoi, String MoTa) {
        super(Ten, Tuoi, MoTa);
    }

    @Override
    void TiengKeu() {
        System.out.println("Grrr Grrr");
    }
    
}



package zoo;

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

public class Chuong {
    int maChuong;
    ArrayList<Animal> AnimaList = new ArrayList<>();

    public Chuong() {
    }

    public Chuong(int maChuong) {
        this.maChuong = maChuong;
    }

    public int getMaChuong() {
        return maChuong;
    }

    public void setMaChuong(int maChuong) {
        this.maChuong = maChuong;
    }

    public ArrayList<Animal> getAnimaList() {
        return AnimaList;
    }

    public void setAnimaList(ArrayList<Animal> AnimaList) {
        this.AnimaList = AnimaList;
    }
    
    public void inpMC(){
        Scanner input = new Scanner(System.in);
        maChuong = input.nextInt();
    }
    
    void themConVat(Animal a){
        AnimaList.add(a);
    } 
    void xoaConVat(String ten){
        for(int i=0;i<AnimaList.size();i++){
            if(AnimaList.get(i).getTen().equals(ten)){
                AnimaList.remove(i);
            }
        }
    }
}



package zoo;

import java.util.ArrayList;

public class Zoo {
    ArrayList<Chuong> DanhSachChuong = new ArrayList<>();
    
    public void themChuong(Chuong a){
        DanhSachChuong.add(a);
    } 
    public void xoaChuong(int maChuong){
        for(int i=0;i<DanhSachChuong.size();i++){
            if( DanhSachChuong.get(i).maChuong == maChuong)){
                DanhSachChuong.remove(i);
                break;
            }
        }
    }
    
}



package zoo;

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

public class TestZoo {
    public static void main(String[] args) {
        Zoo z =new Zoo();
        Chuong mamoi = new Chuong();
        ArrayList<Zoo> zoo = new ArrayList<>();
        while(true){
            Scanner input = new Scanner(System.in);
            System.out.println("\n\t1: Thêm chuồng");
            System.out.println("\t2: Xóa chuồng");
            System.out.println("\t3: Thêm con vật");
            System.out.println("\t4: Xóa con vật");
            System.out.println("\t5: Xem tất cả các con vật");
            System.out.println("\t6: Thoat");
            System.out.print("Chon: ");
            int chon = Integer.parseInt(input.nextLine());
            switch(chon){
                case 1:
                    System.out.println("Nhap vao ma chuong");
                    mamoi.inpMC();
                    z.DanhSachChuong.add(mamoi);
                    break;
                case 2:
                    System.out.println("ma chuong: ");
                    int maChuong = Integer.parseInt(input.nextLine());
                    zoo.xoaChuong(maChuong);
                    break;
                case 3:
                    System.out.println("\n\t1: Cat");
                    System.out.println("\t2: Dog");
                    System.out.println("\t3: Tiger");
                    int choose = Integer.parseInt(input.nextLine());
                    switch(choose){
                        case 1:
                            Animal cat = new Cat();
                            System.out.print("Tên: ");
                            cat.Ten = input.nextLine();
                            System.out.print("Tuổi: ");
                            cat.Tuoi = Integer.parseInt(input.nextLine());
                            System.out.print("Mô tả: ");
                            cat.MoTa = input.nextLine();
                            mamoi.AnimaList.add(cat);
                            break;
                        case 2:
                            Animal dog =new Dog();
                            System.out.print("Tên: ");
                            dog.Ten = input.nextLine();
                            System.out.print("Tuổi: ");
                            dog.Tuoi = Integer.parseInt(input.nextLine());
                            System.out.print("Mô tả: ");
                            dog.MoTa = input.nextLine();
                            mamoi.AnimaList.add(dog);
                            break;
                        case 3:
                            Animal tiger=new Tiger();
                            System.out.print("Tên: ");
                            tiger.Ten = input.nextLine();
                            System.out.print("Tuổi: ");
                            tiger.Tuoi = Integer.parseInt(input.nextLine());
                            System.out.print("Mô tả: ");
                            tiger.MoTa = input.nextLine();
                            mamoi.AnimaList.add(tiger);
                            break;
                    }
                    break;
                case 4:
                    break;
                case 5:
                    for(int i=0;i<mamoi.AnimaList.size();i++){
                        Animal tmp = mamoi.AnimaList.get(i);
                        tmp.output();
                        tmp.TiengKeu();
                    }
                    break;
                case 6:
                    System.exit(0);
                    break;
            }
        }
    }
    
}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-19 02:56:36




package aptech;

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

public class TestZoo {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        Zoo zoo = new Zoo();
        Chuong chuong = new Chuong();
        chuong.AnimalList = new ArrayList<>();
        zoo.DanhSachChuong = new ArrayList<>();
        while(true){
            System.out.println("Vui lòng chọn: \n1. Thêm Chuồng\n2. Xóa Chuồng\n3. Thêm con vật\n4. Xóa con vật\n5. Xem tất cả các con vật\n6. Thoát");
            int chon = Integer.parseInt(input.nextLine());
            switch(chon){
                case 1: 
                    System.out.println("Vui lòng nhập mã chuồng: ");
                    int maChuong = Integer.parseInt(input.nextLine());
                    chuong.maChuong = maChuong;
                    zoo.themChuong(chuong);
                    System.out.println(zoo.DanhSachChuong.get(0));
                    break;
                case 2:
                    System.out.println("Vui lòng nhập mã chuồng: ");
                    maChuong = Integer.parseInt(input.nextLine());
                    zoo.xoaChuong(maChuong);
                    break;
                case 3:
                    while(true){
                        System.out.println("Vui lòng chọn: \n1. Tiger\n2. Dog\n3. Cat");
                        int c = Integer.parseInt(input.nextLine());
                        switch(c){
                            case 1:
                                Animal tiger = new Tiger();
                                System.out.print("Tên: ");
                                tiger.ten = input.nextLine();
                                System.out.print("Tuổi: ");
                                tiger.tuoi = Integer.parseInt(input.nextLine());
                                System.out.print("Mô tả: ");
                                tiger.moTa = input.nextLine();
                                chuong.themConVat(tiger);
                                break;
                            case 2:
                                Animal dog = new Dog();
                                System.out.print("Tên: ");
                                dog.ten = input.nextLine();
                                System.out.print("Tuổi: ");
                                dog.tuoi = Integer.parseInt(input.nextLine());
                                System.out.print("Mô tả: ");
                                dog.moTa = input.nextLine();
                                
                                break;
                            case 3:
                                Animal cat = new Cat();
                                System.out.print("Tên: ");
                                cat.ten = input.nextLine();
                                System.out.print("Tuổi: ");
                                cat.tuoi = Integer.parseInt(input.nextLine());
                                System.out.print("Mô tả: ");
                                cat.moTa = input.nextLine();
                                break;
                            default:
                                System.out.println("Vui lòng chọn lại\n");
                        }
                        if(c == 3 || c == 2 || c == 1){
                            break;
                        }
                    }
                    break;
                case 4:
                    System.out.print("Vui lòng nhập tên con vật: ");
                    String ten = input.nextLine();
                    chuong.xoaConVat(ten);
                    break;
                case 5:
                    for(int i = 0; i < chuong.AnimalList.size(); i++){
                        Animal temp = chuong.AnimalList.get(i);
                        temp.xemThongTin();
                        temp.tiengKeu();
                    }
                    break;
                case 6:
                    break;
                default:
                    System.out.println("Vui lòng chọn lại.");
            }
            
            if(chon == 6){
                break;
            }
        }
        
    }
}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-19 02:56:00



package aptech;

import java.util.ArrayList;

public class Zoo {
    ArrayList<Chuong> DanhSachChuong;
    
    void themChuong(Chuong c) {
        DanhSachChuong.add(c);
    }
    
    void xoaChuong(int maChuong){
        for(int i = 0; i < DanhSachChuong.size(); i++){
            Chuong temp = DanhSachChuong.get(i);
            if(temp.maChuong == maChuong){
                DanhSachChuong.remove(i);
            }
        }
    }
}