By GokiSoft.com| 08:29 06/10/2021|
C Sharp

[Video] Hướng dẫn chữa bài tập quản lý sở thú - Zoo Project C# - Lập trình C# - C2010G

Link Video Bài Giảng


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

Kết quả json ý cuối:

{"DanhSachChuong":[{"MaChuong":1,"AnimalList":[{"Ten":"Tiger01","Tuoi":1,"MoTa":"1"},{"Ten":"Dog01","Tuoi":2,"MoTa":"2"}]},{"MaChuong":2,"AnimalList":[{"Ten":"Cat01","Tuoi":5,"MoTa":"5"}]}]}


Cấu trúc dự án:

#Program.cs


using System;
using BT1734.Models;
using BT1734.Utils;

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

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();

                switch(choose)
                {
                    case 1:
                        zoo.ThemChuong();
                        break;
                    case 2:
                        zoo.XoaChuong();
                        break;
                    case 3:
                        zoo.ThemConVat();
                        break;
                    case 4:
                        zoo.XoaConVat();
                        break;
                    case 5:
                        zoo.XemThongTin();
                        break;
                    case 6:
                        //Luu thong tin xuong file zoo.json
                        //Convert object class -> json string
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(zoo);
                        System.IO.File.WriteAllText(@"zoo.json", json);
                        break;
                    case 7:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 7);
        }

        static void ShowMenu()
        {
            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 thong tin");
            Console.WriteLine("6. Luu zoo.json");
            Console.WriteLine("7. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Utils/Utility.cs


using System;
namespace BT1734.Utils
{
    public class Utility
    {
        public static int ReadInt()
        {
            int value;
            while(true)
            {
                try
                {
                    value = int.Parse(Console.ReadLine());
                    return value;
                } catch(Exception e)
                {
                    Console.WriteLine("Nhap lai!!!");
                }
            }
        }
    }
}


#Models/Zoo.cs


using System;
using System.Collections.Generic;
using BT1734.Utils;

namespace BT1734.Models
{
    public class Zoo
    {
        public List<Chuong> DanhSachChuong { get; set; }

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

        public void ThemChuong()
        {
            Console.WriteLine("Nhap so chuong can them: ");
            int N = Utility.ReadInt();

            for(int i=0;i<N;i++)
            {
                Chuong c = new Chuong();
                c.Input();

                DanhSachChuong.Add(c);
            }
        }

        public void XoaChuong()
        {
            Console.WriteLine("Nhap ma chuong can xoa: ");
            int machuong = Utility.ReadInt();

            for(int i=0;i<DanhSachChuong.Count;i++)
            {
                if(DanhSachChuong[i].MaChuong == machuong)
                {
                    DanhSachChuong.RemoveAt(i);
                    break;
                }
            }
        }

        public void ThemConVat()
        {
            Console.WriteLine("Nhap ma chuong can them con vat: ");
            int machuong = Utility.ReadInt();

            foreach(Chuong chuong in DanhSachChuong)
            {
                if(chuong.MaChuong == machuong)
                {
                    chuong.ThemConVat();
                    break;
                }
            }
        }

        public void XoaConVat()
        {
            Console.WriteLine("Nhap ma chuong can xoa con vat: ");
            int machuong = Utility.ReadInt();
            bool isFind = false;

            foreach (Chuong chuong in DanhSachChuong)
            {
                if (chuong.MaChuong == machuong)
                {
                    isFind = true;
                    Console.WriteLine("Nhap ten con vat can xoa: ");
                    string ten = Console.ReadLine();

                    chuong.XoaConVat(ten);
                    break;
                }
            }

            if(!isFind)
            {
                Console.WriteLine("Khong tim thay ma chuong tren");
            }
        }

        public void XemThongTin()
        {
            Console.WriteLine("Thong tin chuong: ");

            foreach(Chuong chuong in DanhSachChuong)
            {
                chuong.Display();
            }
        }
    }
}


#Models/Tiger.cs


using System;
namespace BT1734.Models
{
    public class Tiger : Animal
    {
        public Tiger()
        {
        }

        public override void TiengKeu()
        {
            Console.WriteLine("Tieng keu tiger");
        }
    }
}


#Models/Dog.cs


using System;
namespace BT1734.Models
{
    public class Dog : Animal
    {
        public Dog()
        {
        }

        public override void TiengKeu()
        {
            Console.WriteLine("Tieng keu dog");
        }
    }
}


#Models/Chuong.cs


using System;
using System.Collections.Generic;
using BT1734.Utils;

namespace BT1734.Models
{
    public class Chuong
    {
        public int MaChuong { get; set; }
        public List<Animal> AnimalList { get; set; }

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

        public void ThemConVat()
        {
            Animal animal;
            Console.WriteLine("Chon dong vat can them: ");
            Console.WriteLine("1. Them Tiger");
            Console.WriteLine("2. Them Dog");
            Console.WriteLine("3. Them Cat");
            Console.WriteLine("Chon: ");
            int choose = Utility.ReadInt();

            switch(choose)
            {
                case 1:
                    animal = new Tiger();
                    break;
                case 2:
                    animal = new Dog();
                    break;
                default:
                    animal = new Cat();
                    break;
            }
            animal.Input();

            AnimalList.Add(animal);
        }

        public void XoaConVat(string ten)
        {
            //AnimalList -> {A, B, C, C, D, K}
            //ten -> C
            for(int i=0;i<AnimalList.Count;i++)
            {
                if(AnimalList[i].Ten == ten)
                {
                    AnimalList.RemoveAt(i);
                    i--;
                }
            }
        }

        public void Input()
        {
            Console.WriteLine("Nhap ma chuong: ");
            MaChuong = Utility.ReadInt();

            Console.WriteLine("Nhap so dong vat can them: ");
            int N = Utility.ReadInt();

            for(int i=0;i<N;i++)
            {
                ThemConVat();
            }
        }

        public void Display()
        {
            Console.WriteLine("Thong tin dong vat chuong: {0}", MaChuong);

            foreach(Animal animal in AnimalList)
            {
                animal.XemThongTin();
            }
        }
    }
}


#Models/Cat.cs


using System;
namespace BT1734.Models
{
    public class Cat : Animal
    {
        public Cat()
        {
        }

        public override void TiengKeu()
        {
            Console.WriteLine("Tieng keu cat");
        }
    }
}


#Models/Animal.cs


using System;
using BT1734.Utils;

namespace BT1734.Models
{
    public abstract class Animal
    {
        public string Ten { get; set; }
        public int Tuoi { get; set; }
        public string MoTa { get; set; }

        public Animal()
        {
        }

        public Animal(string ten, int tuoi, string mota)
        {
            Ten = ten;
            Tuoi = tuoi;
            MoTa = mota;
        }

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

            Console.WriteLine("Nhap tuoi: ");
            Tuoi = Utility.ReadInt();

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

        public void XemThongTin()
        {
            Console.WriteLine("Ten: {0}, Tuoi: {1}, MoTa: {2}", Ten, Tuoi, MoTa);
        }

        public abstract void TiengKeu();
    }
}




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

5

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

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

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