By GokiSoft.com| 08:43 12/10/2021|
C Sharp

Chường trình quản lý sở thú - Lập trình C# - Lập trình C Sharp BT1734

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 Ten

· int Tuoi

· ​String MoTa

· void XemThongTin() //hiển thị tên, tuổi và mô tả của động vật

void NhapThongTin() //Nhap thong tin cua dong vat

· abstract void TiengKeu()

2. Tạo một số hàm tạo cho lớp Animal như sau:

· 0 tham số & đầy đủ đối sô

3. 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 TiengKeu() để thể hiện những tiếng kêu đặc trưng của từng loài vật
  • Thực thi các hàm tạo sử dụng từ khóa super

4. Tạo lớp có tên Chuong gồm:

· int maChuong

· ​List<Animal> AnimalList

· void ThemConVat() //thêm một con vật vào AnimalList

· void XoaConVat(String ten) //xóa con vật có tên tương ứng khỏi AnimalList

5. Tạo lớp có tên Zoo gồm:

· List<Chuong> DanhSachChuong

· void ThemChuong() //thêm chuồng vào DanhSachChuong

· void XoaChuong() //xóa chuồng có mã tương ứng khỏi DanhSachChuong

6. 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. Lưu File => Yêu cầu chuyển Zoo thành string json => Lưu vào file data.json
  7. Thoát

7. Khi người dùng chọn 3 thì yêu cầu người dùng nhập vào loại con vật muốn thêm (Tiger, Dog, Cat) rồi nhập các thông tin của con vật và thêm vào AnimaList.

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ú.


Chú ý: Bổ sung thêm các hàm còn thiếu cho chương trình trên.

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

https://gokisoft.com/1734

Bình luận

avatar
Đào Mạnh Dũng [C2010L]
2021-10-06 03:32:02


#Animal.cs


using System;

namespace _1734
{
    public abstract class Animal
    {
        public string Ten { get; set; }

        public int Tuoi { get; set; }

        public string MoTa { get; set; }

        public void XemThongTin()
        {
            Console.WriteLine(this);
        }

        public abstract void TiengKeu();

        public override string ToString()
        {
            return Ten + ", " + Tuoi + ", " + MoTa;
        }

        public Animal()
        {
        }

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

        internal void input()
        {
            Console.WriteLine("nhap ten : ");
            this.Ten = Console.ReadLine();
            Console.WriteLine("nhap Tuoi : ");
            this.Tuoi = int.Parse(Console.ReadLine());
            Console.WriteLine("nhap MoTa : ");
            this.MoTa = Console.ReadLine();
        }
    }
}


#Cat.cs


using System;

namespace _1734
{
    internal class Cat : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("meo meo");
        }

        public Cat()
        {
        }

        public Cat(string Ten, int Tuoi, string MoTa)
        {
            this.Ten = Ten;
            this.Tuoi = Tuoi;
            this.MoTa = MoTa;
        }
    }
}


#Chuong.cs


using System;
using System.Collections.Generic;

namespace _1734
{
    internal class Chuong
    {
        public int maChuong { get; set; }

        public List<Animal> AnimalList;

        public Chuong()
        {
            AnimalList = new List<Animal>();
        }

        public void ThemConVat(Animal animal)
        {
            AnimalList.Add(animal);
        }

        public void XoaConVat(String ten)
        {
            AnimalList.RemoveAll(item => item.Equals(ten));
        }
    }
}


#Dog.cs


using System;

namespace _1734
{
    internal class Dog : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("gau gau");
        }

        public Dog()
        {
        }

        public Dog(string Ten, int Tuoi, string MoTa)
        {
            this.Ten = Ten;
            this.Tuoi = Tuoi;
            this.MoTa = MoTa;
        }
    }
}


#TestZoo.cs


using Newtonsoft.Json;
using System;
using System.IO;

namespace _1734
{
    internal class TestZoo
    {
        private static void Main(string[] args)
        {
            Zoo zoo = new Zoo();

            while (true)
            {
                switch (menu())
                {
                    case 1:
                        {
                            Chuong chuong = new Chuong();
                            Console.WriteLine("nhap id chuong :");
                            chuong.maChuong = int.Parse(Console.ReadLine());
                            zoo.ThemChuong(chuong);
                            break;
                        }

                    case 2:
                        {
                            Console.WriteLine("nhap id chuong can xoa:");
                            int id = int.Parse(Console.ReadLine());
                            zoo.XoaChuong(id);
                            break;
                        }

                    case 3:
                        {
                            Console.WriteLine("1. Tiger\n" +
                               "2. Dog\n" +
                               "3. Cat"
                               );
                            Console.WriteLine("them con gi :");
                            Animal animal = null;
                            switch (int.Parse(Console.ReadLine()))
                            {
                                case 1:
                                    animal = new Tiger();
                                    break;

                                case 2:
                                    animal = new Dog();
                                    break;

                                case 3:
                                    animal = new Cat();
                                    break;

                                default:
                                    break;
                            }
                            animal.input();
                            zoo.DanhSachChuong[zoo.DanhSachChuong.Count - 1].ThemConVat(animal);
                            break;
                        }

                    case 4:
                        {
                            Console.WriteLine("nhap ten can xoa : ");
                            zoo.DanhSachChuong[zoo.DanhSachChuong.Count - 1].XoaConVat(Console.ReadLine());
                            break;
                        }

                    case 5:
                        {
                            foreach (var chuong in zoo.DanhSachChuong)
                            {
                                foreach (var item in chuong.AnimalList)
                                {
                                    item.TiengKeu();
                                    item.XemThongTin();
                                    Console.WriteLine();
                                }
                            }
                            break;
                        }

                    case 6:
                        {
                            CreateFileJSONWithSystemIO(zoo);
                            break;
                        }

                    case 7:
                        {
                            Environment.Exit(0);
                            break;
                        }

                    default:
                        {
                            break;
                        }
                }
            }

            static int menu()
            {
                Console.Write("1 .Thêm chuồng\n" +
                "2 .Xóa chuồng\n" +
                "3 .Thêm con vật\n" +
                "4 .Xóa con vật\n" +
                "5 .Xem tất cả các con vật\n" +
                "6 .Lưu File\n" +
                "7 .Thoát\n" +
                "Chon : ");
                int choose;
                while (true)
                {
                    try
                    {
                        choose = int.Parse(Console.ReadLine());
                        break;
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Nhập lại.. Đầu vào không hợp lệ!");
                    }
                }

                return choose;
            }
        }

        private static void CreateFileJSONWithSystemIO(Zoo zoo)
        {
            string jsOnList = JsonConvert.SerializeObject(zoo);
            File.WriteAllText(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "data.json"), jsOnList);
        }
    }
}


#Tiger.cs


using System;

namespace _1734
{
    public class Tiger : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("guguuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
        }

        public Tiger()
        {
        }

        public Tiger(string Ten, int Tuoi, string MoTa)
        {
            this.Ten = Ten;
            this.Tuoi = Tuoi;
            this.MoTa = MoTa;
        }
    }
}


#Zoo.cs


using System.Collections.Generic;

namespace _1734
{
    internal class Zoo
    {
        public List<Chuong> DanhSachChuong;

        public Zoo()
        {
            DanhSachChuong = new List<Chuong>();
        }

        public void ThemChuong(Chuong chuong)
        {
            DanhSachChuong.Add(chuong);
        }

        public void XoaChuong(int id)
        {
            DanhSachChuong.RemoveAll(item => item.maChuong == id);
        }
    }
}


avatar
vuong huu phu [T2008A]
2021-05-19 10:14:09



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
    class Zoo
    {
        List<Chuong> DanhSachChuong = new List<Chuong>();
        public void ThemChuong(Chuong c)
        {
            DanhSachChuong.Add(c);
        }
        public void Xoachuong (int machuong)
        {
            for (int i = 0; i < DanhSachChuong.Count; i++)
            {
                if (DanhSachChuong[i].machuong == machuong)
                {
                    DanhSachChuong.RemoveAt(i);
                }
            }
        }

    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
     class Chuong
    {
        //public ​List<Animal> AnimalList2 { set; get; }
        public int machuong { get; set; }
        public List<Animal> list { set; get; }
        public Chuong() {
            // AnimalList2 = new List<Animal>();
            list = new List<Animal>();
        }
        

        public void ThemConVat(Animal a) {
            // AnimalList2.Add(a);
            list.Add(a);
        }
        public void XoaConVat(String ten) {
            int kt = 0;
           for (int i = 0; i < list.Count; i++) {
                if (list[i].Ten == ten) {
                    list.RemoveAt(i);
                    kt = 1;
                }
            }
            if (kt == 1)
            {
                Console.WriteLine("Da xoa");
            }
            else {
                Console.WriteLine("khong co doi tuong de xoa");
            }
        }
        public void nhap() {
           Console.WriteLine("Nhap ma chuong can them");
            machuong = Int32.Parse(Console.ReadLine());
        }
        public void findall() {
            for (int i = 0; i < list.Count; i++) {
                list[i].Xemthongtin();
            }
        
        }

    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
    class Cat : Animal
    {
        public Cat() { 
        }
        public Cat(String Ten, String MoTa , int Tuoi) : base(Ten,MoTa,Tuoi) { }

        public override void Nhap()
        {
            base.Nhap();
        }
        public override void Tiengkeu()
        {
            Console.WriteLine("meo...meo...meo!!!!");
        }
        public override void Xemthongtin()
        {
            base.Xemthongtin();
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
    class Dog : Animal
    {
        public Dog()
        {
        }
        public Dog(String Ten, String MoTa, int Tuoi) : base(Ten, MoTa, Tuoi) { }

        public override void Nhap()
        {
            base.Nhap();
        }
        public override void Tiengkeu()
        {
            Console.WriteLine("Dog...Dog...Dog!!!!");
        }
        public override void Xemthongtin()
        {
            base.Xemthongtin();
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
    class Tiger : Animal
    {
        public Tiger()
        {
        }
        public Tiger(String Ten, String MoTa, int Tuoi) : base(Ten, MoTa, Tuoi) { }

        public override void Nhap()
        {
            base.Nhap();
        }
        public override void Tiengkeu()
        {
            Console.WriteLine("Tiger...Tiger...Tiger!!!!");
        }
        public override void Xemthongtin()
        {
            base.Xemthongtin();
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace qunlysothu
{
    class Animal
    {
        public String Ten { set; get; }
        public String MoTa { set; get; }
        public int Tuoi { set; get; }

        public Animal() { }
        public Animal(String Ten , String MoTa , int Tuoi) {
            this.Ten = Ten;
            this.MoTa = MoTa;
            this.Tuoi = Tuoi;
        }
        public virtual void Nhap()
        {
            Console.WriteLine("Nhap Ten :");
            Ten = Console.ReadLine();
            Console.WriteLine("Nhap Mo ta");
            MoTa = Console.ReadLine();
            Console.WriteLine("Nhap tuoi");
            Tuoi = Int32.Parse(Console.ReadLine());
        }
        public virtual void Xemthongtin() {
            Console.Write("Ten : " + Ten + " Mota : " + MoTa + " Tuoi: " + Tuoi + " . ");
        }

        public virtual void Tiengkeu()
        {

        }

    }
}



using System;

namespace qunlysothu
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Zoo zoo = new Zoo();
            int lc;
            do {
                menu();
                Console.WriteLine("Nhap su lua chon:");
                lc = Int32.Parse(Console.ReadLine());
                switch (lc) {
                    case 1:
                        Chuong chuong = new Chuong();
                        chuong.nhap();
                        zoo.ThemChuong(chuong);
                        break;
                    case 2:
                        Console.WriteLine("Nhap ma can xoa");
                        int ma = Int32.Parse(Console.ReadLine());
                        zoo.Xoachuong(ma);
                        break;
                    case 3:
                        Console.WriteLine(" 1 Tiger ");
                        Console.WriteLine(" 1 Dog ");
                        Console.WriteLine(" 1 Cat ");
                        int lccv = Int32.Parse(Console.ReadLine());
                        switch (lccv) { 
                        case 1:
                                Console.WriteLine("Nhap Ten :");
                                String Ten = Console.ReadLine();
                                Console.WriteLine("Nhap Mo ta");
                                String MoTa = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi");
                                int Tuoi = Int32.Parse(Console.ReadLine());
                                Animal tiger = new Tiger(Ten,MoTa,Tuoi);
                                Chuong c = new Chuong();
                                c.ThemConVat(tiger);
                                break;
                            case 2:
                                Console.WriteLine("Nhap Ten :");
                                String Ten1 = Console.ReadLine();
                                Console.WriteLine("Nhap Mo ta");
                                String MoTa1 = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi");
                                int Tuoi1 = Int32.Parse(Console.ReadLine());
                                Animal dog = new Dog(Ten1, MoTa1, Tuoi1);
                                Chuong cdog = new Chuong();
                                cdog.ThemConVat(dog);
                                break;
                            case 3:
                                Console.WriteLine("Nhap Ten :");
                                String Tenc = Console.ReadLine();
                                Console.WriteLine("Nhap Mo ta");
                                String MoTac = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi");
                                int Tuoic = Int32.Parse(Console.ReadLine());
                                Animal Cat = new Tiger(Tenc, MoTac, Tuoic);
                                Chuong ccat = new Chuong();
                                ccat.ThemConVat(Cat);
                                break;
                        }
                       
                        break;
                    case 4:
                        Console.WriteLine("Nhap ten con vat can xoa");
                      String name_A = Console.ReadLine();
                        Chuong chuongxoa = new Chuong();
                        chuongxoa.XoaConVat(name_A);
                        break;
                    case 5:
                        Chuong all = new Chuong();
                        all.findall();
                        
                        break;
                    case 6:
                        Console.WriteLine("Nhap lai");
                        break;
                        
                }
            } while (lc < 7);

        }

        public static void menu() {
            Console.WriteLine(" 1 Them chuong");
            Console.WriteLine(" 2 Xoa chuong");
            Console.WriteLine(" 3 Them con vat");
            Console.WriteLine(" 4 Xoa con vat");
            Console.WriteLine(" 5 Xem tat ca cac con vat ");
            Console.WriteLine(" 6 Thoat");
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:07:38



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Cat : Animal
    {
        public Cat() { }

        public Cat(string Name, int Age, string Description) : base(Name,Age,Description) { 
        }

        public override void Sound()
        {
            //base.Sound();
            Console.WriteLine("Meo keu Meo Meo.....");
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap tuoi:");
            Age = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Nhap mo ta:");
            Description = Console.ReadLine();
        }

    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:07:18



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Dog : Animal
    {
        public Dog() { }

        public Dog(string Name, int Age, string Description) : base(Name,Age,Description) { 
        }

        public override void Sound()
        {
            //base.Sound();
            Console.WriteLine("Cho keu Gau Gau....");
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap tuoi:");
            Age = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Nhap mo ta:");
            Description = Console.ReadLine();
        }

    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:07:03



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Tiger : Animal
    {
        public Tiger() { }

        public Tiger(string Name, int Age, string Description) : base(Name,Age,Description) { 
        }

        public override void Sound()
        {
            //base.Sound();
            Console.WriteLine("Ho keu Gzzzzzz Gzzzzzz.....");
        }

       public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap tuoi:");
            Age = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Nhap mo ta:");
            Description = Console.ReadLine();
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:07:03



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Tiger : Animal
    {
        public Tiger() { }

        public Tiger(string Name, int Age, string Description) : base(Name,Age,Description) { 
        }

        public override void Sound()
        {
            //base.Sound();
            Console.WriteLine("Ho keu Gzzzzzz Gzzzzzz.....");
        }

       public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap tuoi:");
            Age = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Nhap mo ta:");
            Description = Console.ReadLine();
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:06:49



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Animal
    {
       public string Name { get; set; }
        public int Age { get; set; }
        public string Description { get; set; }

        public Animal() { 
        }

        public Animal(string Name, int Age, string Descreption) {
            this.Name = Name;
            this.Age = Age;
            this.Description = Description;
        }
       

        public void Display() {
            Console.WriteLine("Name: {0}, Age: {1},Descreption: {2}", Name, Age, Description);
        }

        public virtual void  Sound()
        {
            Console.WriteLine("Mo phong tieng keu cac loai dong vat");
        }

    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:06:36



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Room
    {
        public int Roolno { get; set; }
        public List<Animal> RoomAnimalList { get; set; }

        public Room()
        {
            RoomAnimalList = new List<Animal>();
        }

        public void Input()
        {
            Console.WriteLine("Nhap ma chuong");
            Roolno = Int32.Parse(Console.ReadLine()); 
        }
        
        public void AddAnimal()
        {
            Console.WriteLine("Nhap so tuong ung loai dong vat ban muon them: 1-Tiger , 2-Dog, 3-Cat");
            int choose = Int32.Parse(Console.ReadLine());
            Animal animal = null;
            switch (choose)
            {
                case 1:
                    animal = new Tiger();
                    if(animal is Tiger)
                    {
                        ((Tiger)animal).Input();
                        RoomAnimalList.Add(animal);
                    }
                    break;

                case 2:
                    animal = new Dog();
                    if (animal is Dog)
                    {
                        ((Dog)animal).Input();
                        RoomAnimalList.Add(animal);
                    }
                    break;

                case 3:
                    animal = new Cat();
                    if (animal is Cat)
                    {
                        ((Cat)animal).Input();
                        RoomAnimalList.Add(animal);
                    }
                    break;

                case 4:
                    Console.WriteLine("Nhap so tuong ung loai dong vat sai...!!!");
                    break;
            }
        }

        public void DeleteAnimal()
        {
            Console.WriteLine("Nhap ten con vat muon xoa");
            string DeleteName = Console.ReadLine();

            foreach (Animal animal in RoomAnimalList)
            {
                if (animal.Name == DeleteName)
                {
                    RoomAnimalList.Remove(animal);
                    break;
                }
            }
        }

    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:06:22



using System;
using System.Collections.Generic;
using System.Text;

namespace ZooManager
{
    class Zoo
    {
       
        public List<Room> RoomList {get;set;}
      

            public Zoo (){
            RoomList = new List<Room>();
        }


        public void AddRoom()
        {
            Room room = new Room();
            room.Input();
            RoomList.Add(room);
        }
        public void DeleteRoom()
        {
            Console.WriteLine("Nhap ma chuong can xoa");
            int DeleteRoolno = Int32.Parse(Console.ReadLine()); 
            foreach (Room room in RoomList)
            {
                if (room.Roolno == DeleteRoolno)
                {
                    RoomList.Remove(room);
                    break;
                }
            }
          
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-19 10:06:04



using System;

namespace ZooManager
{
    class TestZoo
    {
        static Zoo zoo = new Zoo();
        static void Main(string[] args)
        {
            int choose = 0;

            do
            {
                Menu();
                choose = Int32.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        AddRoom();
                        break;

                    case 2:
                        DeleteRoom();
                        break;

                    case 3:
                        AddAnimal();
                        break;

                    case 4:
                        DeleteAnimal();
                        break;

                    case 5:
                        DisplayAllAnimal();
                        break;

                    case 6:
                        Console.WriteLine("Exit...!");
                        break;

                    default:
                        Console.WriteLine("Nhap lua chon choose sai...!");
                        break;
                }
            } while (choose != 6);
        }

        private static void DisplayAllAnimal()
        {
            foreach (Room room in zoo.RoomList)
            {
                foreach(Animal animal in room.RoomAnimalList)
                {
                    animal.Display();
                }
            }
        }

        private static void DeleteAnimal()
        {
            Console.WriteLine("nhap ma chuong can xoa dong vat");
            int RoolnoFind = Int32.Parse(Console.ReadLine());
            foreach (Room room in zoo.RoomList)
            {
                if (room.Roolno == RoolnoFind)
                {
                    room.DeleteAnimal();
                }
            }
        }

        private static void AddAnimal()
        {
            Console.WriteLine("nhap ma chuong can them dong vat");
            int RoolnoFind = Int32.Parse(Console.ReadLine());
            foreach(Room room in zoo.RoomList)
            {
                if (room.Roolno == RoolnoFind)
                {
                    room.AddAnimal();
                }
            }
        }

        private static void DeleteRoom()
        {
            zoo.DeleteRoom();
        }

        private static void AddRoom()
        {
            zoo.AddRoom();
        }

        static void Menu()
        {
            Console.WriteLine("1. Them chuong");
            Console.WriteLine("2. Xoa chuong");
            Console.WriteLine("3. Them con vat");
            Console.WriteLine("4. Xoa con vat");
            Console.WriteLine("5. Xem tat ca cac con vat");
            Console.WriteLine("Nhap lua chon: choose =  ");
        }
    }
}