C# - C Sharp: Animal Management System

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

Xin xem bài tập tại ĐÂY.

Các Interface:

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

namespace Practical3_Exercise3
{
    interface IAnimal
    {
        int ID
        {
            get;
            set;
        }
        string Name
        {
            get;
            set;
        }
        int Age
        {
            get;
            set;
        }
    }
    interface ITerrestrialAnimal :IAnimal
    {
         void Move();
    }
    interface IMarineAnimal : IAnimal
    {
         void Move();
    }
}

Lớp Cat:

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

namespace Practical3_Exercise3
{
    class Cat:ITerrestrialAnimal
    {
        
        int id;
        string name;
        int age;
        public int ID
        {
            set
            {
                id = value;
            }
            get
            {
                return id;
            }
        }
        public string Name
        {
            set
            {
                name = value;
            }
            get
            {
                return name;
            }
        }

        public int Age
        {
            set
            {
                age = value;
            }
            get
            {
                return age;
            }
        }
        
        public void Move()
        {
            Console.WriteLine("Run");
        }
        public override string ToString()
        {                     
            Console.WriteLine(GetType());
            Console.WriteLine("Id:" + ID);
            Console.WriteLine("Name:" + Name);
            Console.WriteLine("Age:" + Age);
            return "";
        }
    }
}

Lớp Fish:

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

namespace Practical3_Exercise3
{
    class Fish :IMarineAnimal
    {
        int id;
        string name;
        int age;

        public int ID
        {
            set
            {
                id = value;
            }
            get
            {
                return id;
            }
        }
        public string Name
        { 
            set 
            {
                name = value;
            }
            get
            {
                return name;
            }
        }

        public int Age
        {
            set
            {
                age = value;
            }
            get
            {
                return age;
            }
        }
        public void Move()
        {
            Console.WriteLine("Swim");
        }
        public override string ToString()
        {
            Console.WriteLine(GetType());
            Console.WriteLine("Id: " + ID);
            Console.WriteLine("Name: " + Name);
            Console.WriteLine("Age: " + Age);
            return "";
        }
    }
}

Lớp Crocodile:

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

namespace Practical3_Exercise3
{
    class Crocodile :ITerrestrialAnimal,IMarineAnimal
    {
        int id;
        string name;
        int age;
        public int ID
        {
            set
            {
                id = value;
            }
            get
            {
                return id;
            }
        }

        public string Name
        {
            set
            {
                name = value;
            }
            get
            {
                return name;
            }
        }

        public int Age
        {
            set
            {
                age = value;
            }
            get
            {
                return age;
            }
        }

        void ITerrestrialAnimal.Move()
        {
            Console.WriteLine("Run");
        }

         void IMarineAnimal.Move()
        {
            Console.WriteLine("Swim");
        }

        public override string ToString()
        {
            Console.WriteLine(GetType());
            Console.WriteLine("Id:"+ID);
            Console.WriteLine("Name:" +Name);
            Console.WriteLine("Age:" +Age);
            return "";
        }
    }
}

Lớp Test:

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

namespace Practical3_Exercise3
{
    class Test
    {
        
        int count=0;
        Hashtable ListAnimal = new Hashtable();

        public void AddCat()
        {
            Cat obj = new Cat();
            obj.ID = count;            
            Console.Write("Name of cat:");
            obj.Name = Console.ReadLine();
            Console.Write("Age of cat:");
            obj.Age=int.Parse(Console.ReadLine());
            ListAnimal.Add(count, obj);
            count++;
        }

        public void AddFish()
        {
            Fish obj = new Fish();
            obj.ID = count;         
            Console.Write("Name of fish:");
            obj.Name = Console.ReadLine();
            Console.Write("Age of fish:");
            obj.Age = int.Parse(Console.ReadLine());
            ListAnimal.Add(count, obj);
            count++;
        }

        public void AddCrocodile()
        {
            Crocodile obj = new Crocodile();
            obj.ID = count;            
            Console.Write("Name of crocodile: ");
            obj.Name = Console.ReadLine();
            Console.Write("Age of crocodile: ");
            obj.Age = int.Parse(Console.ReadLine());            
            ListAnimal.Add(count, obj);
            count++;
        }

        public void ViewTerrestrialAnimals()
        {
            Cat c = new Cat();
            for (int i = 0; i < ListAnimal.Count; i++)
                if (ListAnimal[i].GetType().Equals(c.GetType()))
                {
                    ListAnimal[i].ToString();
                    c.Move();
                }
        }

        public void ViewMarineAnimals()
        {
            Fish f = new Fish();
            for (int i = 0; i < ListAnimal.Count; i++)
                if (ListAnimal[i].GetType().Equals(f.GetType()))
                {
                    ListAnimal[i].ToString();
                    f.Move();
                }
        }

        public void ViewAllAnimals()
        {
            foreach(DictionaryEntry de in ListAnimal.Values)
                Console.WriteLine(de);
        }

        public void DeleteAnimals()
        {
            int n;
            Console.Write("Please select a key: ");
            int.TryParse(Console.ReadLine(), out n);
            foreach(DictionaryEntry de in ListAnimal)
                if (de.Key.Equals(n))
                {
                    ListAnimal.Remove(n);
                    break;
                }
            Console.WriteLine(ListAnimal.Count);
        }

        public static void Main()
        {
            Test t = new Test();
            Cat c = new Cat();     
            int choice;
            Console.WriteLine("1.Create a Crocodile");
            Console.WriteLine("2.Create a Cat");
            Console.WriteLine("3.Create a Fish");
            Console.WriteLine("4.View Terrestrial Animals");
            Console.WriteLine("5.View Marine Animals");
            Console.WriteLine("6.View All Animals");
            Console.WriteLine("7.Delete Animal");
            Console.WriteLine("8.Exit");
            do
            {
                Console.Write("\nPlease select an item: ");
                int.TryParse(Console.ReadLine(), out choice);
                switch (choice)
                {
                    case 1: t.AddCrocodile();
                        break;
                    case 2: t.AddCat();
                        break;
                    case 3: t.AddFish();
                        break;
                    case 4: t.ViewTerrestrialAnimals();
                        break;
                    case 5: t.ViewMarineAnimals();
                        break;
                    case 6: t.ViewAllAnimals();
                        break;
                    case 7: t.DeleteAnimals();
                        break;
                    case 8: return;
                }
            } while (choice != 8);
        }
    }
}

» Tiếp: News Management System
« Trước: Products Management System
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!