By GokiSoft.com| 14:47 26/02/2021|
Java Basic

[Share Code] Hướng dẫn tạo phần mềm quản lý sở thú - quản lý Zoo - Lập trình Java Basic BT2195

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



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


Tags:

Liên kết rút gọn:

https://gokisoft.com/2195

Bình luận