C# - C Sharp: News Management System


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

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

File INews.cs:

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

namespace Solution_Practical3_Exercise2
{
    interface INews
    {
        int ID { get; set; }
        string Title { get; set; }
        DateTime PublishDate { get; set; }
        string Author { get; set; }
        string Content { get; set; }
        float AverageRate { get; }
        void Display();
    }
}

File News.cs:

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

namespace Solution_Practical3_Exercise2
{
    class News:INews
    {
        int id;
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        string title;
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }
        DateTime publishdate;
        public DateTime PublishDate
        {
            get
            {
                return publishdate;
            }
            set
            {
                publishdate = value;
            }
        }
        string author;
        public string Author
        {
            get
            {
                return author;
            }
            set
            {
                author = value;
            }
        }
        string content;
        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }
        float averagerate;
        public float AverageRate
        {
            get { return averagerate; }
        }

        public void Display()
        {
            Console.WriteLine("Title:"+Title);
            Console.WriteLine("Publish date: "+PublishDate);
            Console.WriteLine("Author: "+Author);
            Console.WriteLine("Content: "+Content);
            Console.WriteLine("Averate rate: "+AverageRate);
        }
        int[] RateList = new int[3];
        public int this[int index] //Indexer
        {
            get
            {
                return RateList[index];
            }
            set
            {
                RateList[index] = value;
            }
        }
        public void Calculate(){
            averagerate= (float)(RateList[0]+RateList[1]+RateList[2])/3;
        }
    }
}

File Program.cs:

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

namespace Solution_Practical3_Exercise2
{
    class Program
    {
        static Hashtable NewsList = new Hashtable();
        static int count = 0;
        static void Menu()
        {
            Console.WriteLine("1. Insert news");
            Console.WriteLine("2. View list news");
            Console.WriteLine("3. Averate rate");
            Console.WriteLine("4. Exit");
        }
        static void InsertNews()
        {
            News news = new News();
            news.ID = ++count;
            Console.Write("Title: ");
            news.Title = Console.ReadLine();
            while (true)
            {
                try
                {
                    Console.Write("Publish date: ");
                    news.PublishDate = DateTime.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Publish date is not formatted!");
                }
            }
            Console.Write("Author: ");
            news.Author = Console.ReadLine();
            Console.Write("Content: ");
            news.Content = Console.ReadLine();
            for (int i = 0; i < 3; i++)
            {
                Console.Write("Rate "+(i+1)+": ");
                news[i] = int.Parse(Console.ReadLine());
            }
            NewsList.Add(news.ID, news);
        }
        static void ViewNews()
        {
            if (NewsList.Count > 0)
            {
                foreach (News news in NewsList.Values)
                {
                    news.Display();
                }
            }
            else
            {
                Console.WriteLine("News not found!");
            }
        }
        static void EverateRate()
        {
            if (NewsList.Count > 0)
            {
                foreach (News news in NewsList.Values)
                {
                    news.Calculate();
                    news.Display();
                }
            }
            else
            {
                Console.WriteLine("News not found!");
            }
        }
        static void Main(string[] args)
        {
            int choice;
            Menu();
            while (true)
            {
                Console.Write("Please select an item: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1: InsertNews(); break;
                    case 2: ViewNews(); break;
                    case 3: EverateRate(); break;
                    case 4: return;
                }
            }
        }
    }
}

» Tiếp: Students Management System
« Trước: Animal Management System
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!