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

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.

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

15

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

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

2021-05-19 09:00:14


#TestZoo.cs


using System;
using QuanLySoThu.Modals;

namespace QuanLySoThu.Test
{
    class TestZoo
    {
        static void Main(string[] args)
        {
            Zoo zoo = new Zoo();
            
            int choose = 0;
            do
            {
                Menu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Chuong chuong = new Chuong();
                        Console.WriteLine("Nhap ma chuong muon them:");
                        chuong.maChuong = int.Parse(Console.ReadLine());
                        zoo.ThemChuong(chuong);
                        break;
                    case 2:
                        Console.WriteLine("Nhap ma chuong muon xoa:");
                        int maChuong = int.Parse(Console.ReadLine());
                        zoo.XoaChuong(maChuong);
                        break;
                    case 3:
                        Console.WriteLine("Chon chuong muon them con vat:");
                        int selectChuong = int.Parse(Console.ReadLine());
                        int check = 0;
                        foreach (Chuong chuong1 in zoo.DanhSachChuong)
                        {
                            if (chuong1.maChuong == selectChuong)
                            {
                                check++;
                                animalMenu();
                                int select = int.Parse(Console.ReadLine());
                                switch (select)
                                {
                                    case 1:
                                        Tiger tiger = new Tiger();
                                        tiger.Input();
                                        chuong1.ThemConVat(tiger);
                                        break;
                                    case 2:
                                        Dog dog = new Dog();
                                        dog.Input();
                                        chuong1.ThemConVat(dog);
                                        break;
                                    case 3:
                                        Cat cat = new Cat();
                                        cat.Input();
                                        chuong1.ThemConVat(cat);
                                        break;
                                    default:
                                        Console.WriteLine("Nhap sai!!");
                                        break;
                                }
                            }
                        }
                        if (check == 0) Console.WriteLine("Khong co ma chuong tuong ung!!");
                        break;
                    case 4:
                        Console.WriteLine("Chon chuong muon xoa con vat:");
                        int selectChuong1 = int.Parse(Console.ReadLine());
                        int check1 = 0;
                        foreach (Chuong chuong1 in zoo.DanhSachChuong)
                        {
                            if (chuong1.maChuong == selectChuong1)
                            {
                                check1++;
                                Console.WriteLine("Nhap ten con vat muon xoa:");
                                string name = Console.ReadLine();
                                chuong1.XoaConVat(name);
                            }
                        }
                        if (check1 == 0) Console.WriteLine("Khong co ma chuong tuong ung!!");
                        break;
                    case 5:
                        foreach(Chuong chuong2 in zoo.DanhSachChuong)
                        {
                            Console.WriteLine("Chuong {0}:", chuong2.maChuong);
                            foreach(Animal animal1 in chuong2.AnimalList)
                            {
                                animal1.XemThongTin();
                                animal1.TiengKeu();
                            }
                        }
                        break;
                    case 6:
                        Console.WriteLine("Tam biet!!");
                        break;
                    default:
                        Console.WriteLine("Loi nhap!!");
                        break;

                }
            } while (choose != 6);
        }
        static void Menu()
        {
            Console.WriteLine("Lua chon chuong trinh:");
            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 con vat");
            Console.WriteLine("6.Thoat");
        }
        static void animalMenu()
        {
            Console.WriteLine("Lua chon con vat de them:");
            Console.WriteLine("1.Ho");
            Console.WriteLine("2.Cho");
            Console.WriteLine("3.Meo");
        }
    }
}


#Animal.cs


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

namespace QuanLySoThu.Modals
{
    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)
        {
            this.Ten = Ten;
            this.Tuoi = Tuoi;
            this.MoTa = MoTa;
        }
        public void Input()
        {
            Console.WriteLine("Nhap ten:");
            Ten = Console.ReadLine();
            Console.WriteLine("Nhap tuoi:");
            Tuoi = int.Parse(Console.ReadLine());
            Console.WriteLine("Mo ta:");
            MoTa = Console.ReadLine();
        }
        public void XemThongTin()
        {
            Console.WriteLine("Ten: {0}, Tuoi: {1}, Mo ta: {2}", Ten, Tuoi, MoTa);
        }
        public abstract void TiengKeu();
    }
}


#Cat.cs


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

namespace QuanLySoThu.Modals
{
    class Cat : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Meo meo....");
        }
    }
}


#Chuong.cs


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

namespace QuanLySoThu.Modals
{
    class Chuong
    {
        public int maChuong { get; set; }
        public List<Animal> AnimalList = new List<Animal>();
        public void ThemConVat(Animal a)
        {
            AnimalList.Add(a);
        }
        public void XoaConVat(string ten)
        {
            foreach(Animal animal in AnimalList)
            {
                if (animal.Ten == ten)
                {
                    AnimalList.Remove(animal);
                    break;
                }
            }
        }
    }
}


#Dog.cs


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

namespace QuanLySoThu.Modals
{
    class Dog : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Gau gau....");
        }
    }
}


#Tiger.cs


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

namespace QuanLySoThu.Modals
{
    class Tiger : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Gruhhhh.....");
        }
    }
}


#Zoo.cs


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

namespace QuanLySoThu.Modals
{
    class Zoo
    {
        public List<Chuong> DanhSachChuong = new List<Chuong>();
        public void ThemChuong(Chuong c)
        {
            DanhSachChuong.Add(c);
        }
        public void XoaChuong(int machuong)
        {

            foreach(Chuong chuong in DanhSachChuong)
            {
                if (chuong.maChuong == machuong)
                {
                    DanhSachChuong.Remove(chuong);
                    break;
                }
            }

        }
    }
}



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

2020-07-10 08:55:48



using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AnimalManager
{
    class TestZoo
    {
        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.Luu file");
            Console.WriteLine("7.Exit");
        }
        static void AddChuong(Zoo zoo) {
            Chuong chuong = new Chuong();
            Console.WriteLine("Nhap ma chuong : ");
            chuong.MaChuong = int.Parse(Console.ReadLine());
            zoo.DanhSachChuong.Add(chuong);
            Console.WriteLine("Continue Y/N?");
            string choice = Console.ReadLine();
            if ((choice.ToLower()).Equals("n"))
            {             
            }
            else {
                AddChuong(zoo);
            }
        }
        static void DelChuong(Zoo zoo)
        {
            Console.WriteLine("Nhap ma chuong : ");
            int maChuong = int.Parse(Console.ReadLine());
            zoo.XoaChuong(maChuong);
        }

        static void AddAnimal(Zoo zoo) {
            Console.WriteLine("Nhap ma chuong : ");
            int maChuong = int.Parse(Console.ReadLine());
            for (int i = 0; i < zoo.DanhSachChuong.Count; i++) {
                if ( zoo.DanhSachChuong[i].MaChuong == maChuong) {
                    Boolean exit = true;
                    while (exit) {
                        Console.WriteLine("Chon con vat.");
                        Console.WriteLine("1.Tiger");
                        Console.WriteLine("2.Cat");
                        Console.WriteLine("3.Dog");
                        Console.WriteLine("4.Exit");
                        int choice = int.Parse(Console.ReadLine());
                        switch (choice) {
                            case 1:
                                Tiger tiger = new Tiger();
                                Console.WriteLine("Nhap ten : ");
                                tiger.Ten = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi : ");
                                tiger.Tuoi = int.Parse(Console.ReadLine());
                                Console.WriteLine("Nhap mo ta : ");
                                tiger.MoTa = Console.ReadLine();
                                zoo.DanhSachChuong[i].ThemConVat(tiger);
                                break;
                            case 2:
                                Cat cat = new Cat();
                                Console.WriteLine("Nhap ten : ");
                                cat.Ten = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi : ");
                                cat.Tuoi = int.Parse(Console.ReadLine());
                                Console.WriteLine("Nhap mo ta : ");
                                cat.MoTa = Console.ReadLine();
                                zoo.DanhSachChuong[i].ThemConVat(cat);
                                break;
                            case 3:
                                Dog dog = new Dog();
                                Console.WriteLine("Nhap ten : ");
                                dog.Ten = Console.ReadLine();
                                Console.WriteLine("Nhap tuoi : ");
                                dog.Tuoi = int.Parse(Console.ReadLine());
                                Console.WriteLine("Nhap mo ta : ");
                                dog.MoTa = Console.ReadLine();
                                zoo.DanhSachChuong[i].ThemConVat(dog);
                                break;
                            case 4:
                                exit = false;
                                break;
                        }
                    }
                }
            }
        }
        static void DelAnimal(Zoo zoo)  
        {
            Console.WriteLine("Nhap ma chuong : ");
            int maChuong = int.Parse(Console.ReadLine());
            
          
            for (int i = 0; i < zoo.DanhSachChuong.Count; i++) {
                for (int j = 0; j < zoo.DanhSachChuong[i].AnimalList.Count; j++) {
                    Console.WriteLine(zoo.DanhSachChuong[i].AnimalList[j].Ten);
                }
                
                if (zoo.DanhSachChuong[i].MaChuong == maChuong) {
                    Console.WriteLine("Nhap ten con vat : ");
                    string ten = Console.ReadLine();
                    for (int j = 0; j < zoo.DanhSachChuong[i].AnimalList.Count; j++)
                    {
                        if (zoo.DanhSachChuong[i].AnimalList[j].Ten.Equals(ten)) {
                            zoo.DanhSachChuong[i].AnimalList.RemoveAt(j);
                        }
                    }
                }
            }
        }
        static void ShowAllAnimal(Zoo zoo) {
            foreach(Chuong chuong in zoo.DanhSachChuong){
                Console.WriteLine("Chuong {0}.",(chuong.MaChuong));
                for (int i = 0; i < chuong.AnimalList.Count; i++) {
                    chuong.AnimalList[i].XemThongTin();
                }
            }
        }


            public static void Main(string[] args) {
            Zoo zoo = new Zoo();
            while (true) {
                Menu();
                int choice = int.Parse(Console.ReadLine());
                switch (choice) {
                    case 1:
                        AddChuong( zoo);
                        break;
                    case 2:
                        DelChuong(zoo);
                        break;
                    case 3:
                        AddAnimal( zoo);
                        break;
                    case 4:
                        DelAnimal( zoo);
                        break;
                    case 5:
                        ShowAllAnimal(zoo);
                        break;
                    case 6:
                        string json = JsonConvert.SerializeObject(zoo);
                        FileStream file = File.Create("zoo.json");
                        file.Close();
                        System.IO.File.WriteAllText("zoo.json",json);
                        break;
                    case 7:
                        return;

                }
            }
        }
    }
}



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

namespace AnimalManager
{
    class Zoo
    {
        public List<Chuong> DanhSachChuong { get; set; }
        public Zoo() {
            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 AnimalManager
{
    class Chuong
    {
        public int MaChuong { get; set; }
        public List<Animal> AnimalList {get;set;}

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

        public void ThemConVat(Animal a) {
            AnimalList.Add(a);
        }
        public void XoaConVat(String Ten) {
            for (int i = 0; i < AnimalList.Count;i++) {
                if (AnimalList[i].Ten.Equals(Ten)) {
                    AnimalList.RemoveAt(i);
                }
            }
        }
    }
}



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

namespace AnimalManager
{
    class Cat : Animal
    {

        public override void TiengKeu()
        {
            Console.WriteLine("Meo meo...");
        }
        public Cat() : base()
        {

        }
        public override void XemThongTin()
        {
            base.XemThongTin();
        }
        public Cat(string Ten, int Tuoi, string MoTa) : base(Ten, Tuoi, MoTa)
        {
        }
    }
}



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

namespace AnimalManager
{
    class Dog : Animal
    {

        public override void TiengKeu()
        {
            Console.WriteLine("Gau Gau...");
        }
        public Dog() : base()
        {

        }
        public override void XemThongTin()
        {
            base.XemThongTin();
        }
        public Dog(string Ten, int Tuoi, string MoTa) : base(Ten, Tuoi, MoTa)
        {
        }
    }
}



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

namespace AnimalManager
{
    class Tiger : Animal
    {

        public override void TiengKeu() {
            Console.WriteLine("Gam Gu...");
        }
        public Tiger() : base()  {
            
        }
        public override void  XemThongTin()   {
            base.XemThongTin();
        }
        public Tiger(string Ten , int Tuoi , string MoTa) : base(Ten, Tuoi,MoTa)
        {
        }
    }
}



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

namespace AnimalManager
{
    abstract class Animal
    {
        public string Ten { get; set; }
        public int Tuoi { get; set; }
        public string MoTa { get; set; }
        public virtual void XemThongTin() {
            Console.WriteLine("Ten dong vat : {0} , Tuoi : {1} , MoTa : {2}",Ten,Tuoi,MoTa);
        }
        public abstract void TiengKeu();

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

        public Animal()
        {
        }
    }
}



Hoàng Quang Huy [C1907L]
Hoàng Quang Huy [C1907L]

2020-07-10 08:15:48



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

namespace Project6
{
    class Cat : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Mèo đây");
        }
    }
}
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Project6
{
    class Dog : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Chó đây");
        }
    }
}
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Project6
{
    class Tiger : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Hổ đây");
        }
    }
}
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Project6
{
    public abstract class Animal
    {
        public abstract void TiengKeu();
        public string Ten { get; set; }
        public int Tuoi { get; set; }
        public string MoTa { get; set; }

        public void XemThongTin()
        {
            Console.WriteLine("Tên: " + Ten);
            Console.WriteLine("Tuổi: " + Tuoi);
            Console.WriteLine("Mô tả: " + MoTa);
        }

        public void NhapThongTin()
        {
            Console.WriteLine("Nhập tên: ");
            this.Ten = Console.ReadLine();
            Console.WriteLine("Nhập tuổi: ");
            this.Tuoi = int.Parse(Console.ReadLine());
            Console.WriteLine("Thêm mô tả: ");
            this.MoTa = Console.ReadLine();
        }
        protected Animal(string ten, int tuoi, string moTa)
        {
            Ten = ten;
            Tuoi = tuoi;
            MoTa = moTa;
        }
        protected Animal()
        {
        }
    }
}
-------------------------------------------------------------------------------------using System;
using System.Collections.Generic;
using System.Text;

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

        public Chuong(int maChuong, List<Animal> animalList)
        {
            MaChuong = maChuong;
            AnimalList = animalList;
        }

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

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

        public void XoaConVat(String ten)
        {
            for (int i = 0; i < AnimalList.Count; i++)
            {
                if (ten.Equals(AnimalList[i].Ten))
                {
                    AnimalList.RemoveAt(i);
                }
            }
        }

        public void Input()
        {
            Console.WriteLine("Nhập mã chuồng: ");
            this.MaChuong = int.Parse(Console.ReadLine());
        }
    }
}
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Project6
{
    public class Zoo
    {
        public List<Chuong> DanhSachChuong { get; set; }
        
        public Zoo(List<Chuong> danhSachChuong)
        {
            DanhSachChuong = danhSachChuong;
        }

        public Zoo()
        {
            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 Newtonsoft.Json;
namespace Project6
{
    class TestZoo
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.Unicode;
            int choose;
            int n;
            Zoo zoo = new Zoo();
            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Console.WriteLine("Nhập số lượng chuồng cần thêm: ");
                        n = int.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++)
                        {
                            Chuong chuong = new Chuong();
                            chuong.Input();
                            zoo.ThemChuong(chuong);
                        }
                        break;
                    case 2:
                        Console.WriteLine("Nhập mã chuồng cần xóa: ");
                        int del = int.Parse(Console.ReadLine());
                        zoo.XoaChuong(del);
                        break;
                    case 3:
                        int num = 0 ;
                        for (int i = 0; i < zoo.DanhSachChuong.Count; i++)
                        {
                            Console.WriteLine("Nhập số lượng con vật cho chuồng " + zoo.DanhSachChuong[i].MaChuong);
                                num = int.Parse(Console.ReadLine());
            
                            for (int j = 0; j < num; j++)
                            {
                                Console.WriteLine("Chọn con vật cần thêm:\n1. Tiger.\n2. Dog\n3. Cat");
                                int c = int.Parse(Console.ReadLine());
                                Animal animal;
                                switch (c)
                                {
                                    case 1:
                                        animal = new Tiger();
                                        animal.NhapThongTin();
                                        break;
                                    case 2:
                                        animal = new Dog();
                                        animal.NhapThongTin();
                                        break;
                                    case 3:
                                        animal = new Cat();
                                        animal.NhapThongTin();
                                        break;
                                    default:
                                        animal = new Tiger();
                                        break;
                                }
                                zoo.DanhSachChuong[i].ThemConVat(animal);
                            }
                        }

                        break;
                    case 4:
                        Console.WriteLine("Nhập mã chuồng cần xóa con vật: ");
                        int ma = int.Parse(Console.ReadLine());
                        for (int i = 0; i < zoo.DanhSachChuong.Count; i++)
                        {
                            if (ma.Equals(zoo.DanhSachChuong[i].MaChuong))
                            {
                                Console.WriteLine("Nhập tên con vật cần xóa: ");
                                string name = Console.ReadLine();
                                zoo.DanhSachChuong[i].XoaConVat(name);
                            }
                        }
                        break;
                    case 5:
                        for (int i = 0; i < zoo.DanhSachChuong.Count; i++)
                        {
                            for (int j = 0; j < zoo.DanhSachChuong[i].AnimalList.Count; j++)
                            {
                                zoo.DanhSachChuong[i].AnimalList[j].XemThongTin();
                                zoo.DanhSachChuong[i].AnimalList[j].TiengKeu();
                            }
                        }
                        break;
                    case 6:
                        string output = JsonConvert.SerializeObject(zoo);
                        System.IO.File.WriteAllText(@"data.json", output);
                        break;
                    case 7:
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Lựa chọn không hợp lệ");
                        break;
                }
            } while (choose != 7);
        }


        static void ShowMenu()
        {
            Console.WriteLine("1. Thêm chuồng");
            Console.WriteLine("2. Xóa chuồng");
            Console.WriteLine("3. Thêm con vật");
            Console.WriteLine("4. Xóa con vật");
            Console.WriteLine("5. Xem tất cả các con vật");
            Console.WriteLine("6. Lưu File");
            Console.WriteLine("7. Thoát");
            Console.WriteLine("Lựa chọn của bạn: ");
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy [C1907L]

2020-07-08 14:10:04



using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Program
    {
        static void Main(string[] args)
        {
            Zoo zoo = new Zoo();
            int choose;
            do
            {
                Menu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Chuong chg = new Chuong();
                        chg.input();
                        zoo.Add(chg);
                        break;
                    case 2:
                        Console.WriteLine("Nhap Ma chuong: ");
                        int maChuong = int.Parse(Console.ReadLine());
                        zoo.Drop(maChuong);
                        break;
                    case 3:
                        int Cage = nhapmachuong();
                        Console.WriteLine("Con nao?");
                        Console.WriteLine("1.Tiger\n2.Dog\n3.Cat");
                        int chon = int.Parse(Console.ReadLine());
                        switch (chon)
                        {
                            case 1:
                                Tiger tiger = new Tiger();
                                tiger.input();
                                for (int i=0;i< zoo.DSChuong.Count;i++)
                                {
                                    if(zoo.DSChuong[i].maChuong== Cage)
                                    {
                                        zoo.DSChuong[i].Add(tiger);
                                    }
                                }
                                break;
                            case 2:
                                Dog dog = new Dog();
                                dog.input();
                                for (int i = 0; i < zoo.DSChuong.Count; i++)
                                {
                                    if (zoo.DSChuong[i].maChuong == Cage)
                                    {
                                        zoo.DSChuong[i].Add(dog);
                                    }
                                }
                                break;
                            case 3:
                                Cat cat = new Cat();
                                cat.input();
                                for (int i = 0; i < zoo.DSChuong.Count; i++)
                                {
                                    if (zoo.DSChuong[i].maChuong == Cage)
                                    {
                                        zoo.DSChuong[i].Add(cat);
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        break;
                    case 4:
                        Console.WriteLine("Nhap ten con can xoa: ");
                        string delName = Console.ReadLine();
                        foreach (Chuong chog in zoo.DSChuong)
                        {
                            chog.Drop(delName);
                        }
                        break;
                    case 5:
                        foreach (Chuong chog in zoo.DSChuong)
                        {
                            Console.WriteLine("\tMa chuong: "+chog.maChuong);
                            foreach (Animal dv in chog.AnimalList)
                            {
                                dv.XemThongTin();
                                dv.TiengKeu();
                            }
                        }
                        break;
                    case 6:
                        string output = JsonConvert.SerializeObject(zoo);
                        System.IO.File.WriteAllText(@"data.json",output);
                        break;
                    case 7:
                        break;
                    default:
                        Console.WriteLine("Error");
                        break;
                }
            } while (choose!=7);
        }

        static void Menu()
        {
            Console.WriteLine("1. Them Chuong");
            Console.WriteLine("2. Xoa Chuong");
            Console.WriteLine("3. Them Animal");
            Console.WriteLine("4. Xoa Animal");
            Console.WriteLine("5. Xem Animal");
            Console.WriteLine("6. Save");
            Console.WriteLine("7. Exit");
            Console.Write("Choose: ");
        }

        static int nhapmachuong()
        {
            Console.Write("Nhap Ma chuong muon them vao: ");
            int machuong = int.Parse(Console.ReadLine());
            return machuong;
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Zoo
    {
        public List<Chuong> DSChuong { get; set; }

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

        public Zoo(List<Chuong> dSChuong)
        {
            DSChuong = dSChuong;
        }

        public void Add(Chuong a)
        {
            DSChuong.Add(a);
        }

        public void Drop(int maChuong)
        {
            for (int i = 0; i < DSChuong.Count; i++)
            {
                if (DSChuong[i].maChuong==maChuong)
                {
                    DSChuong.RemoveAt(i);
                }
            }
        }

    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Chuong
    {
        public int maChuong { get; set; }
        public List<Animal> AnimalList { get; set; }

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

        public Chuong(int maChuong, List<Animal> animalList)
        {
            this.maChuong = maChuong;
            AnimalList = animalList;
        }

        public void Add(Animal a)
        {
            AnimalList.Add(a);
        }

        public void Drop(string a)
        {
            for (int i=0;i<AnimalList.Count;i++)
            {

                if (a.Equals(AnimalList[i].Name))
                {
                    AnimalList.RemoveAt(i);
                }
            }
        }

        public void input()
        {
            Console.WriteLine("Nhap Ma chuong: ");
            maChuong = int.Parse(Console.ReadLine());
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Cat : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Meow...");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Dog : Animal
    {
        public override void TiengKeu()
        {
            Console.WriteLine("Woof...");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    class Tiger : Animal
    {

        public override void TiengKeu()
        {
            Console.WriteLine("Roah...");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiZoo
{
    abstract 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 description)
        {
            Name = name;
            Age = age;
            Description = description;
        }

        public void input()
        {
            Console.WriteLine("Nhap Ten: ");
            Name = Console.ReadLine();
            Console.WriteLine("Nhap Tuoi: ");
            Age = int.Parse(Console.ReadLine());
            Console.WriteLine("Nhap Mota: ");
            Description = Console.ReadLine();
        }

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

        public abstract void TiengKeu();
    }
}



trung [C1907L]
trung [C1907L]

2020-07-08 14:09:02



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    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 XemThongTin()
        {
            Console.WriteLine("Ten : {0} | Tuoi : {1} | Mo ta : {2}", Ten, Tuoi, MoTa);
        }

        public abstract void TiengKeu();

    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Tiger : Animal
    {

        public Tiger(string ten, int tuoi, string moTa) : base(ten, tuoi, moTa)
        {

        }

        public override void TiengKeu()
        {
            Console.WriteLine("Ga`o Ga`o");    
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Dog : Animal
    {
        

        public Dog(string ten, int tuoi, string moTa) : base(ten, tuoi, moTa)
        {

        }

        public override void TiengKeu()
        {
            Console.WriteLine("Gau Gau");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Cat : Animal
    {
        public Cat(string ten, int tuoi, string moTa) : base(ten, tuoi, moTa)
        {

        }

        public override void TiengKeu()
        {
            Console.WriteLine("Meo meo");
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Chuong
    {
        public int MaChuong { get; set; }
        public List<Animal> AnimalList {get;set;}
        
        public Chuong()
        {
            AnimalList = new List<Animal>();
        }

        public void Input()
        {
            Console.WriteLine("Nhap ma chuong");
            MaChuong = int.Parse(Console.ReadLine());
        }

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

        public void XoaConVat(string Ten)
        {
            for (int i = 0; i < AnimalList.Count; i++)
            {
                if (AnimalList[i].Ten.Equals(Ten))
                {
                    AnimalList.RemoveAt(i);
                    break;
                }
            }
        }


    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Zoo
    {
        public List<Chuong> DanhSachChuong { get; set; }

        public Zoo()
        {
            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);
                    break;
                }
            }
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class TestZoo
    {
        static void Main(string[] args)
        {
            Zoo ZooMgr = new Zoo();
            int choice = 0;
            do
            {
                ShowMenu();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Nhap so luong chuong :");
                        int n = int.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++)
                        {
                            Chuong newChuong = new Chuong();
                            newChuong.Input();
                            ZooMgr.ThemChuong(newChuong);
                            /*
                            Console.WriteLine("Nhap so luong thu cho chuong thu {0}", i + 1);
                            int m = int.Parse(Console.ReadLine());
                            for (int j = 0; j < m; j++)
                            {

                            }
                            */
                        }
                        break;
                    case 2:
                        Console.WriteLine("Nhap ma chuong muon xoa");
                        int maChuong = int.Parse(Console.ReadLine());
                        ZooMgr.XoaChuong(maChuong);
                        break;
                    case 3:
                        for (int i = 0; i < ZooMgr.DanhSachChuong.Count; i++)
                        {
                            Console.WriteLine("Nhap so luong thu cho chuong co ma {0}", ZooMgr.DanhSachChuong[i].MaChuong);
                            int soVat = int.Parse(Console.ReadLine());
                            for (int j = 0; j < soVat; j++)
                            {
                                Console.WriteLine("Input 1->Tiger, 2->Dog, 3->Cat");
                                int animalType = int.Parse(Console.ReadLine());
                                Animal newAnimal;
                                Console.WriteLine("Nhap vao ten con thu");
                                string ten = Console.ReadLine();
                                Console.WriteLine("Nhap vao tuoi con thu");
                                int tuoi = int.Parse(Console.ReadLine());
                                Console.WriteLine("Nhap vao mo ta");
                                string moTa = Console.ReadLine();
                                switch (animalType)
                                {
                                    case 1:
                                        newAnimal = new Tiger(ten, tuoi,moTa);
                                        break;
                                    case 2:
                                        newAnimal = new Dog(ten, tuoi, moTa);
                                        break;
                                    case 3:
                                        newAnimal = new Cat(ten, tuoi, moTa);
                                        break;
                                    default:
                                        newAnimal = new Tiger(ten, tuoi, moTa);
                                        break;
                                }
                                ZooMgr.DanhSachChuong[i].ThemConVat(newAnimal);
                             }
                        }
                        break;
                    case 4:
                        Console.WriteLine("Nhap vao ma chuong muon xoa con vat");
                        int MaChuong = int.Parse(Console.ReadLine());
                        foreach (Chuong c in ZooMgr.DanhSachChuong)
                        {
                            if (c.MaChuong == MaChuong)
                            {
                                Console.WriteLine("Nhap vao ten con thu vat");
                                string tenVat = Console.ReadLine();
                                c.XoaConVat(tenVat);
                                break;
                            }
                        }
                        break;
                    case 5:
                        foreach (Chuong c in ZooMgr.DanhSachChuong)
                        {
                            Console.WriteLine("Danh sach con vat cho chuong co ma chuong {0}", c.MaChuong);
                            foreach (Animal anm in c.AnimalList)
                            {
                                anm.TiengKeu();
                                anm.XemThongTin();
                            }
                        }
                        break;
                    case 6:
                        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(ZooMgr);
                        System.IO.File.WriteAllText(@"data.json", jsonString);
                        break;
                    case 7:
                        Console.WriteLine("Bye bye ...");
                        break;
                    default:
                        Console.WriteLine("Invalid input ...");
                        break;

                }
            } while (choice != 7);
        }

        public static void ShowMenu()
        {
            Console.WriteLine("1. Thêm chuồng");
            Console.WriteLine("2. Xóa chuồng");
            Console.WriteLine("3. Thêm con vật");
            Console.WriteLine("4. Xóa con vật");
            Console.WriteLine("5. Xem tất cả các con vật");
            Console.WriteLine("6. Lưu File");
            Console.WriteLine("7. Exit");
            Console.WriteLine("Your choice: ?");
        }
    }
}