• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

01 April 2018

C#: Entity Framework - Code First, no Database - (Console Application)

Program.cs
Example C# 2018
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new StudentContext())
            {
                var student = new Student() { Name = "Antonio" };
                var subject1 = new Subject() { Name = "Subject 1" };
                var subject2 = new Subject() { Name = "Subject 2" };

                student.SubjectsList.Add(subject1);
                student.SubjectsList.Add(subject2);

                db.Students.Add(student);
                db.SaveChanges();
            }
        }
    }

    public class Student
    {
        public Student()
        {
            this.SubjectsList = new List<Subject>();
        }

        public int StudentID { get; set; }
        public string Name { get; set; }
        public virtual List<Subject> SubjectsList { set; get; }

    }
    public class Subject
    {
        public int SubjectID { get; set; }
        public string Name { get; set; }
        public virtual Student Student { get; set; }
    }
    class StudentContext : DbContext
    {
        public DbSet<Student> Students { set; get; }
        public DbSet<Subject> Subjects { set; get; }
    }
}
App.config
Example C# 2018
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
      <add name="StudentContext" connectionString="Server=ANTONIO-PC\SQLEXPRESS; Database=StudentDatabase; User=sa; Password=1234567" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
Output
Thêm Birthday vào bảng Student
Tool > Nuget Package Manager > Package Manage Console
// Chạy lệnh này tại cửa sổ Console
Enable-Migrations
Add-Migration AddBirthday
Update-Database
Output
Xóa Birthday trên bảng Student
Add-Migration RemoveBirthday
Update-Database
Output

25 March 2018

C#: Collection Example

Collection C#
Example C# 2018
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            methodDisplay();
        }

        static string[] methodString()
        {
            string[] array = new string[3];
            array[0] = "Thank";
            array[1] = "To";
            array[2] = "Array String[]";
            return array;
        }
        static ArrayList methodArrayList()
        {
            ArrayList arrayList = new ArrayList();
            arrayList.Add("ArrayList1");
            arrayList.Add("ArrayList2");
            arrayList.Add("ArrayList3");
            arrayList.Add("ArrayList4");
            return arrayList;
        }
        static List<String> methodList()
        {
            List<String> list = new List<String>();
            list.Add("List1");
            list.Add("List2");
            list.Add("List3");
            list.Add("List4");
            return list;
        }
        static Hashtable methodHashtable()
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add("key1", "hash1");
            hashtable.Add("key2", "hash2");
            hashtable.Add("key3", "hash3");
            //string str = hashtable["key5"].ToString();
            return hashtable;
        }
        static Dictionary<string, string> methodDictionary()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("key1", "key11");
            dictionary.Add("key2", "key12");
            dictionary.Add("key3", "key13");
            //string str = dictionary["key5"].ToString();
            return dictionary;
        }
        static Stack<string> methodStack()
        {
            // Stack LIFO
            Stack<string> stack = new Stack<string>();
            stack.Push("stack1");
            stack.Push("stack2");
            stack.Push("stack3");
            //string str = stack.Pop().ToString()
            return stack;
        }
        static Queue<string> methodQueue()
        {
            // Queue FIFO
            Queue<string> queue = new Queue<string>();
            queue.Enqueue("queue1");
            queue.Enqueue("queue2");
            queue.Enqueue("queue3");
            //string str = queue.Dequeue().ToString();
            return queue;
        }
        static void methodDisplay()
        {
            // Show Array
            Console.WriteLine(string.Join("->", methodString()));

            // Show ArrayList
            Console.WriteLine(string.Join("->", methodArrayList().ToArray()));

            // Show List
            Console.WriteLine(string.Join("->", methodList()));

            // Show Hashtable
            Console.WriteLine(string.Join("->", methodHashtable()["key2"].ToString()));

            // Show Dictionary
            Console.WriteLine(string.Join("->", methodDictionary()));

            // Show Stack
            Console.WriteLine(string.Join("->", methodStack()));

            // Show Queue
            Console.WriteLine(string.Join("->", methodQueue()));

            // Endding and Reading
            Console.ReadLine();
        }
    }
}

13 June 2016

Đề thi AP C#1 Fpt Aptech Test Programming C# Fpt Aptech 2016 Accp i13

Đề thì trắc nghiệm môn AP C# 1 Aptech fpt















//========Hãy tự tìm và làm lại đáp án này chỉ đê tham khảo=======//
3-abc
4-a AJAX
5-a
6-b Convert
7-d XAML
8-a Store
9-c
10-bc
11-b LINQ
12-d
13-c
14-d
16-b
 ____________________________________
/ Tất cả các đáp án trên đủ để bạn vượt    \
\ qua! Chúc các bạn vận dụng thành công /
 -------------------------------------------------- 
        \   ^__^
         \  (oo)\___________
             (__)\                   )\/\
                  ||---------------w |
                  ||                   ||


Đề thi thực hành môn AP C# 1 Fpt Aptech 2016



class Program
Test C# 2016
using ContactManager;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContactManager
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();

            int choice;
            do
            {
                Console.WriteLine("\n================");
                Console.WriteLine("1.Add new contact");
                Console.WriteLine("2.Find a contact by name");
                Console.WriteLine("3.Display contact");
                Console.WriteLine("4.Exit");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                            new MethodContact().add(hash);
                            break;
                    case 2:
                            new MethodContact().search(hash);
                            break;
                    case 3:
                            new MethodContact().display(hash);
                            break;
                    case 4:
                            break;

                }
            } while (choice != 4);
        }
    }
}
class MethodContact
Test C# 2016
using System;
using System.Collections;

namespace ContactManager
{
    class MethodContact
    {
        public Hashtable add(Hashtable hash)
        {
            Contactmanager cm = new Contactmanager();

            Console.Write("contact name: ");
            cm.ContactName = Console.ReadLine();
            Console.Write("Phone number: ");
            cm.PhoneNumber = Console.ReadLine();

            if (cm.ContactName.Length != 0 && cm.PhoneNumber.Length != 0)
            {
                hash.Add(cm.ContactName, cm.PhoneNumber);

            }
            return hash;
        }

        public void display(Hashtable hash)
        {
            ICollection key = hash.Keys;
            Console.WriteLine("\tContactName\t\tPhone");
            foreach (string s in key)
            {
                Console.WriteLine("\t" + s + "\t\t\t" + hash[s]);
            }
        }

        public Hashtable search(Hashtable hash)
        {
            Console.Write("Enter name search: ");
            string name = Console.ReadLine();
            Console.WriteLine("\n\tContatName\t\tPhone");
            int flag = 0;
            ICollection key = hash.Keys;
            foreach (string s in key)
            {
                if (s.Equals(name))
                {
                    Console.WriteLine("\t" + s + "\t\t\t" + hash[s]);
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                Console.WriteLine("Name not found");
            }
            return hash;
        }
    }
}
class Contactmanager
Test C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContactManager
{
    class Contactmanager
    {
        public string ContactName { get; set; }
        public string PhoneNumber { get; set; }
        public Contactmanager()
        {

        }
        public Contactmanager(string contactname, string phone)
        {
            ContactName = contactname;
            PhoneNumber = phone;
        }
    }
}

10 June 2016

Abstract Class C#

Abstract Class
Abstract C# 2016
using System;
using static System.Environment;

namespace AbstractSample
{
    // Define an abstract class
    public abstract class PdaItem
    {
        public PdaItem(string name)
        {
            Name = name;
        }

        public virtual string Name { get; set; }
        public abstract string GetSummary();
    }
    public class Contact : PdaItem
    {
        public Contact(string name)
            : base(name)
        {
        }

        public override string Name
        {
            get
            {
                return $"{ FirstName } { LastName }";
            }

            set
            {
                string[] names = value.Split(' ');
                // Error handling not shown.
                FirstName = names[0];
                LastName = names[1];
            }
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }

        public override string GetSummary()
        {
            return $"FirstName: { FirstName + NewLine }"
            + $"LastName: { LastName + NewLine }"
            + $"Address: { Address + NewLine }";
        }

        // ...
    }

    public class Appointment : PdaItem
    {
        public Appointment(string name) :
            base(name)
        {
            Name = name;
        }

        public DateTime StartDateTime { get; set; }
        public DateTime EndDateTime { get; set; }
        public string Location { get; set; }

        // ...
        public override string GetSummary()
        {
            return $"Subject: { Name + NewLine }"
                + $"Start: { StartDateTime + NewLine }"
                + $"End: { EndDateTime + NewLine }"
                + $"Location: { Location }";
        }
    }

    public class Program
    {
        public static void Main()
        {
            PdaItem[] pda = new PdaItem[3];

            Contact contact = new Contact("Sherlock Holmes");
            contact.Address = "221B Baker Street, London, England";
            pda[0] = contact;

            Appointment appointment =
               new Appointment("Soccer tournament");
            appointment.StartDateTime = new DateTime(2008, 7, 18);
            appointment.EndDateTime = new DateTime(2008, 7, 19);
            appointment.Location = "Estádio da Machava";
            pda[1] = appointment;

            contact = new Contact("Anne Frank");
            contact.Address =
                "Apt 56B, Whitehaven Mansions, Sandhurst Sq, London";
            pda[2] = contact;

            List(pda);
        }

        public static void List(PdaItem[] items)
        {
            // Implemented using polymorphism. The derived
            // type knows the specifics of implementing 
            // GetSummary().
            foreach (PdaItem item in items)
            {
                Console.WriteLine("________");
                Console.WriteLine(item.GetSummary());
            }

            Console.ReadKey();
        }
    }
}

Đếm số lần xuất hiện trong list - Count List Lambda - c#: a method to count occurrences in a list

Count list lambda C#
Count list C# 2016
string words = "Hi, Hi, Hello, Hi, Hello";
var countList = words.Split(new[] { " " }, StringSplitOptions.None);
int count = countList.Where(s => s.Contains("Hi")).Count();
Console.WriteLine(count);
Output: 3
string occur = "Test1";
List<String> words = new List<string>() {"Test1","Test2","Test3","Test1"};
int count = words.Where(x => x.Equals(occur)).Count();
Console.WriteLine(count);
Output: 2
List<int> list = new List<int>();
list.AddRange(new int[] { 1, 2, 3, 2, 1, 4, 5, 1, 7, 1, 8, 1});
Console.WriteLine("1 occurs {0} times.", CountOccurenceOfValue(list, 1));
Console.WriteLine("1 occurs {0} times.", CountOccurenceOfValue2(list, 1));
}

static int CountOccurenceOfValue(List<int> list, int valueToFind)
{
    return ((from temp in list where temp.Equals(valueToFind) select temp).Count());
}

static int CountOccurenceOfValue2(List<int> list, int valueToFind)
{
int count = list.Where(temp => temp.Equals(valueToFind))
            .Select(temp => temp)
            .Count();
return count;
Output: 
1 occurs 5 times.
1 occurs 5 times.
List<Lichthi> list = new List<Lichthi>();

var ListName = from n in list
                   where n.Name == "Jonh"
                   select n;
int count = ListName.Count();
Console.WriteLine(count);

    // ___________________ 
    //< Toi La Mot Con Bo >
    // ------------------- 
    //        \   ^__^
    //         \  (oo)\_______
    //            (__)\       )\/\
    //                ||----w |
    //                ||     || 
Tính Count của list với điều kiện cùng tên C#
(Count đếm số lần xuất hiện của đối tượng trong list)
int tran1 = (from n2 in listlt where n2.Doib == d.Tendoi select n2).Count();
int tran2 = (from n1 in listlt where n1.Doia == d.Tendoi select n1).Count();
int tempTran = tran1 + tran2; //Trận

int thang1 = (from n2 in listthang where n2.Doib == d.Tendoi select n2).Count();
int thang2 = (from n1 in listthang where n1.Doia == d.Tendoi select n1).Count();
int tempThang = thang1 + thang2; //Thắng

int hoa1 = (from n2 in listhoa where n2.Doib == d.Tendoi select n2).Count();
int hoa2 = (from n1 in listhoa where n1.Doia == d.Tendoi select n1).Count();
int tempHoa = hoa1 + hoa2; //Hòa

int thua1 = (from n2 in listthua where n2.Doib == d.Tendoi select n2).Count();
int thua2 = (from n1 in listthua where n1.Doia == d.Tendoi select n1).Count();
int tempThua = thua1 + thua2; //Thua

Tính Sum của list với điều kiện cùng tên C#
(Sum tính tổng của đối tượng trong list)
int diem1 = (from n2 in listkq where n2.Doib == d.Tendoi select n2).Sum(item => int.Parse(item.kqB));
int diem2 = (from n1 in listkq where n1.Doia == d.Tendoi select n1).Sum(item => int.Parse(item.kqA));
int tempDiem = diem1 + diem2; //Diem
RUN C# Online: http://rextester.com

Assignment C# Fpt Aptech 2016 - Programming Assignment






Class Program
Assignment C# fpt aptech 2016 - Programming Assignment C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
        // _______________ 
        //< Duong Huu Dai >
        // ---------------
        //        \   ^__^
        //         \  (oo)\_______
        //            (__)\       )\/\
        //                ||--WWW |
        //                ||     ||
    class Program
    {
        //===========Khỏi tạo đối tượng list=========//
        List<Doi> listdoi = new List<Doi>();
        List<Lichthi> listlt = new List<Lichthi>();
        List<Ketqua> listkq = new List<Ketqua>();
        List<Ketqua> listthang = new List<Ketqua>();
        List<Ketqua> listthua = new List<Ketqua>();
        List<Ketqua> listhoa = new List<Ketqua>();

        //===============Quản lý danh sách===========//
        public void danhSach(List<Doi> list)
        {
            int n;
            do
            {
                Console.WriteLine("\n=================================");
                Console.WriteLine("- 1. Xem danh sach doi bong.");
                Console.WriteLine("- 2. Cap nhat danh sach doi bong.");
                Console.WriteLine("- 3. Them moi mot doi bong.");
                Console.WriteLine("- 0. Tro ve menu chinh.");

                n = int.Parse(Console.ReadLine());
                switch (n)
                {
                    case 0:
                        break;
                    case 1:
                        xemDanhSach(list);
                        break;
                    case 2:
                        capNhatDanhSach(list);
                        break;
                    case 3:
                        themMoi(list);
                        break;
                    default:
                        Console.WriteLine("Vui long chon lai!");
                        break;
                }
            } while (n.CompareTo(0) != 0);
        }          //-Menu danh sách
        public List<Doi> themMoi(List<Doi> list)
        {
            string answer = "";
            do
            {
                Console.WriteLine("======Them moi mot doi bong=====");
                Console.Write("- Nhap ma doi: ");
                string maDoi = Console.ReadLine();
                Console.Write("- Nhap ten doi: ");
                string tenDoi = Console.ReadLine();
                Console.Write("- Nhap ten huan luyen vien: ");
                string tenHLV = Console.ReadLine();
                list.Add(new Doi(maDoi, tenDoi, tenHLV));
                Console.WriteLine("- Ban co muon tiep tuc? [Y/N]");
                answer = Console.ReadLine();
                answer = answer.ToLower();
            } while (answer.CompareTo("y") == 0);
            return list;
        }      //-Thêm mới đội
        public void capNhatDanhSach(List<Doi> list)
        {
            string answer = "";
            do
            {
                Console.WriteLine("======Cap nhat thong tin doi bong=====");
                Console.Write("- Nhap ma doi: ");
                string maDoi = Console.ReadLine();
                Console.Write("- Sua ten doi: ");
                string tenDoi = Console.ReadLine();
                Console.Write("- Sua ten huan luyen vien: ");
                string tenHLV = Console.ReadLine();
                Console.WriteLine("- Ban co muon cap nhat thong tin? [Y/N]");
                string update = Console.ReadLine();
                update = update.ToLower();
                //Update một item trong list
                if (update == "y")
                {
                    try
                    {
                        //Lấy vị trí của item trong list
                        int index = list.FindIndex(delegate(Doi dsd1)
                        {
                            return dsd1.Madoi.Equals(maDoi);
                        });
                        //Xóa item ở vị trí vừa tìm được
                        list.RemoveAt(index);
                        //Thêm vào list một item mới
                        list.Add(new Doi(maDoi, tenDoi, tenHLV));
                        Console.WriteLine("- Thong tin da duoc cap nhat thanh cong!");

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("- Cap nhat khong thanh cong!");
                    }
                }

                Console.WriteLine("- Ban co muon tiep tuc? [Y/N]");
                answer = Console.ReadLine();
                answer = answer.ToLower();

            } while (answer.CompareTo("y") == 0);
            //Cap nhat xong hien thi Menu danh sach 2
            if (answer == "n")
            {
                danhSach2(list);
            }
        }   //-Cập nhật đội - hiển thị DS 2
        public void danhSach2(List<Doi> list)
        {
            int n;
            do
            {
                Console.WriteLine("\n=================================");
                Console.WriteLine("- 1. Xem danh sach doi bong.");
                Console.WriteLine("- 2. Cap nhat danh sach doi bong.");
                Console.WriteLine("- 3. Them moi mot doi bong.");
                Console.WriteLine("- 4. Xem danh sach theo thu tu ma doi."); //Sort theo mã
                Console.WriteLine("- 5. Xem danh sach doi bong theo ten doi."); //Sort theo tên
                Console.WriteLine("- 0. Tro ve menu chinh.");

                n = int.Parse(Console.ReadLine());
                switch (n)
                {
                    case 0:
                        break;
                    case 1:
                        xemDanhSach(list);
                        break;
                    case 2:
                        capNhatDanhSach(list);
                        break;
                    case 3:
                        themMoi(list);
                        break;
                    case 4:
                        xemTheoMa(list);
                        break;
                    case 5:
                        xemTheoTen(list);
                        break;
                    default:
                        Console.WriteLine("Vui long chon lai!");
                        break;
                }
            } while (n.CompareTo(0) != 0); //CompareTo return 0 and 1
        }         //-DS 2
        public void xemDanhSach(List<Doi> list)
        {
            Console.WriteLine("================================================");
            Console.WriteLine("|   Ma doi   |   Ten doi   |  Huan luyen vien  |");
            Console.WriteLine("================================================");
            foreach (Doi dsd in list)
            {
                Console.WriteLine("\n   {0}    \t {1}   \t {2} ", dsd.Madoi, dsd.Tendoi, dsd.HLV);
            }
            Console.WriteLine("\n================================================");

        }       //-Xem đội
        public void xemTheoMa(List<Doi> list)
        {
            Console.WriteLine("================================================");
            Console.WriteLine("|   Ma doi   |   Ten doi   |  Huan luyen vien  |");
            Console.WriteLine("================================================");
            list.Sort(new CompareMaDoi());
            foreach (Doi dsd in list)
            {
                Console.WriteLine("\n   {0}    \t {1}   \t {2} ", dsd.Madoi, dsd.Tendoi, dsd.HLV);
            }
            Console.WriteLine("\n================================================");
        }         //-Sort theo mã - DS2
        public void xemTheoTen(List<Doi> list)
        {
            Console.WriteLine("================================================");
            Console.WriteLine("|   Ma doi   |   Ten doi   |  Huan luyen vien  |");
            Console.WriteLine("================================================");
            list.Sort(new CompareTenDoi());
            foreach (Doi dsd in list)
            {
                Console.WriteLine("\n   {0}    \t {1}   \t {2} ", dsd.Madoi, dsd.Tendoi, dsd.HLV);
            }
            Console.WriteLine("\n================================================");
        }        //-Sort theo tên - DS2
        
        //=============Quản lý lịch thi đấu===========//
        public void lichThi(List<Lichthi> list)
        {
            int n;
            do
            {
                Console.WriteLine("\n=================================");
                Console.WriteLine("- 1. Xem lich thi dau.");
                Console.WriteLine("- 2. Cap nhat lich thi dau.");
                Console.WriteLine("- 3. Tao lich thi dau.");
                Console.WriteLine("- 0. Tro ve menu chinh.");

                n = int.Parse(Console.ReadLine());
                switch (n)
                {
                    case 0:
                        break;
                    case 1:
                        xemLich(list);
                        break;
                    case 2:
                        capNhat(list,listkq);
                        break;
                    case 3:
                        taoLich(list);
                        break;
                    default:
                        Console.WriteLine("Vui long chon lai!");
                        break;
                }
            } while (n.CompareTo(0) != 0); //CompareTo return 0 and 1
        }       //-Menu lịch thi
        public void taoLich(List<Lichthi> list)
        {
            int i = 1;
            String n;
            foreach (Lichthi dslt in list)
            {
                Console.WriteLine("- {0}. Tran: {1} vs {2}", i++, dslt.Doia, dslt.Doib);
            }
            do
            {
                string doia = "", doib="";

                Console.WriteLine("===============================");
                Console.Write("- Chon: ");
                int index = int.Parse(Console.ReadLine());
                index = index - 1;
                //Lấy ra vị trí trận đấu trong list lịch thi
                for (int j = 0; j < list.Count; j++)
                {
                    Lichthi values = list[j];
                    if (index == j)
                    {
                        list.RemoveAt(index); //Xóa item tại vị trí chọn
                        Console.WriteLine("- Tran: {0} vs {1}",values.Doia, values.Doib); //in ra màn hình tên trận đấu vừa chọn
                        doia = values.Doia;
                        doib = values.Doib;// Gán trận đấu trong list lịch thi vào biến tạm doia doib
                        break; // Bắt đầu xóa item nhưng đã gán vào biến tạm
                    }
                }

                Console.Write("- Ngay thi dau: ");
                string ngayThi = Console.ReadLine();
                Console.Write("- Gio thi dau: ");
                string gioThi = Console.ReadLine();
                Console.Write("- San thi dau: ");
                string sanThi = Console.ReadLine();
                //Lưu mới vào list khi đã đầy đủ thông tin
                list.Insert(index, new Lichthi() { Doia = doia, Doib = doib, Ngaythi = ngayThi, Giothi = gioThi, Santhi = sanThi });

                Console.WriteLine("Ban co muon tiep tuc? [Y/N]");
                n = Console.ReadLine();
                n = n.ToLower();
            } while (n.CompareTo("y") == 0);
        }       //-Thêm mứi lịch
        public void capNhat(List<Lichthi> listlt, List<Ketqua> listkq)
        {
            string n;
            int i = 1;
            do
            {
                Console.Write("- Tran {0}: ", i++);
                string doia = Console.ReadLine();
                Console.Write(" vs ");
                string doib = Console.ReadLine();

                Console.WriteLine("Ban co muon tiep tuc? [Y/N]");
                n = Console.ReadLine();
                n = n.ToLower();
                listlt.Add(new Lichthi() { Doia = doia, Doib = doib });
                listkq.Add(new Ketqua() { Doia=doia, Doib =doib});
            } while (n.CompareTo("y") == 0); //CompareTo return 0 and 1
        }       //-Cập nhật lịch
        public void xemLich(List<Lichthi> list)
        {
            Console.WriteLine("\n===========Lich Thi Dau==========");
            foreach (Lichthi dslt in list)
            {
                Console.WriteLine("\n| Tran: {0} vs {1}",dslt.Doia,dslt.Doib);
                Console.WriteLine("| Ngay thi dau: " + dslt.Ngaythi);
                Console.WriteLine("| gio thi: " + dslt.Giothi);
                Console.WriteLine("| San thi: " + dslt.Santhi);

            }
        }       //-Xem lịch
        
        //===========Quản lý kết quả thi đấu==========//
        public void ketQua(List<Ketqua> listkq)
        {
            int i = 1, exit;
            string answer, a="", b="";
            Console.WriteLine("==========Ket qua thi dau========");
            foreach (Ketqua kq in listkq)
            {
                Console.WriteLine("- {0}. {1} vs {2}",i++, kq.Doia, kq.Doib);
            }
            Console.WriteLine("- 0. Tro ve menu chinh");
            do{
                Console.WriteLine("Chon: ");
                int index = int.Parse(Console.ReadLine());
                     exit = index; //Nếu chọn 0 thì thoát vòng lặp
                     if (exit != 0) //Nếu chọn khác 0 thì nhập kết quả trận đấu
                     {
                         index = index - 1;
                         //Lấy ra vị trí trong list kết quả
                         for (int j = 0; j < listkq.Count; j++)
                         {
                             Ketqua values = listkq[j];
                             if (index == j)
                             {
                                 listkq.RemoveAt(index); //Xóa item tại vị trí chọn
                                 Console.WriteLine("- Tran: {0} vs {1}", values.Doia, values.Doib); //in ra màn hình tên trận đấu vừa chọn
                                 a = values.Doia;
                                 b = values.Doib;// Gán trận đấu vừa chọn vào biến tạm a b.
                                 break; // Bắt đầu xóa item nhưng đã lưu trận đấu
                             }
                         }

                         Console.Write("- Ket qua {0}: ", a);
                         String kq1 = Console.ReadLine();
                         Console.Write("- Ket qua {0}: ", b);
                         String kq2 = Console.ReadLine();
                         if (int.Parse(kq1) > int.Parse(kq2)) //Nếu 1 > 2 thì a thắng b thua
                         {
                             listthang.Add(new Ketqua() { Thang = 1, Doia = a });
                             listthua.Add(new Ketqua() { Thua = 1, Doib = b });
                         }
                         else if (int.Parse(kq1) < int.Parse(kq2)) // Nếu 1 < 2 thì a thua b thắng
                         {
                             listthang.Add(new Ketqua() { Thang = 1, Doia = b });
                             listthua.Add(new Ketqua() { Thua = 1, Doib = a });
                         }
                         else //  Nếu 1 = 2 thì a = b hòa
                         {
                             listhoa.Add(new Ketqua() { Hoa = 1, Doia = a, Doib = b });
                         }
                         listkq.Add(new Ketqua(a, b, kq1, kq2));
                         Console.WriteLine("Ban co muon tiep tuc? [Y/N]");
                         answer = Console.ReadLine();
                         answer = answer.ToLower();
                         if (answer != "y") //Nếu chọn y thì thóat vòng lặp
                         {
                             exit = 0;
                         }
                     }
            } while (exit.CompareTo(0) != 0);

        }
        public void thongKe(List<Doi> listdoi, List<Lichthi> listlt, List<Ketqua> listkq)
        {
            int diema=0, diemb=0, diemc=0;
            int  i=0, j=0;
                Console.WriteLine("=====================================================================");
                Console.WriteLine("| Ma doi | Ten doi           | Tran | Thang |  Hoa  |  Thua  | Diem |");
                Console.WriteLine("=====================================================================");
                
                foreach (Doi d in listdoi)
                {
                    i = i++;
                    if (listlt[i].Doia.Equals(d.Tendoi))
                    {

                        int tran1 = (from n2 in listlt where n2.Doib == d.Tendoi select n2).Count();
                        int tran2 = (from n1 in listlt where n1.Doia == d.Tendoi select n1).Count();
                        int tempTran = tran1 + tran2; //Trận
                        int thang1 = (from n2 in listthang where n2.Doib == d.Tendoi select n2).Count();
                        int thang2 = (from n1 in listthang where n1.Doia == d.Tendoi select n1).Count();
                        int tempThang = thang1 + thang2; //Thắng
                        int hoa1 = (from n2 in listhoa where n2.Doib == d.Tendoi select n2).Count();
                        int hoa2 = (from n1 in listhoa where n1.Doia == d.Tendoi select n1).Count();
                        int tempHoa = hoa1 + hoa2; //Hòa
                        int thua1 = (from n2 in listthua where n2.Doib == d.Tendoi select n2).Count();
                        int thua2 = (from n1 in listthua where n1.Doia == d.Tendoi select n1).Count();
                        int tempThua = thua1 + thua2; //Thua
                        int diem1 = (from n2 in listkq where n2.Doib == d.Tendoi select n2).Sum(item => int.Parse(item.kqB));
                        int diem2 = (from n1 in listkq where n1.Doia == d.Tendoi select n1).Sum(item => int.Parse(item.kqA));
                        int tempDiem = diem1 + diem2; //Diem
  
                        Console.WriteLine("  {0}\t  {1}\t\t{2}\t{3}\t{4}\t{5}\t{6}", d.Madoi, d.Tendoi, tempTran, tempThang, tempHoa, tempThua, tempDiem);
                    } 
                    else if (listlt[i].Doib.Equals(d.Tendoi))
                        {
                            int tran1 = (from n2 in listlt where n2.Doib == d.Tendoi select n2).Count();
                            int tran2 = (from n1 in listlt where n1.Doia == d.Tendoi select n1).Count();
                            int tempTran = tran1 + tran2; //Trận
                            int thang1 = (from n2 in listthang where n2.Doib == d.Tendoi select n2).Count();
                            int thang2 = (from n1 in listthang where n1.Doia == d.Tendoi select n1).Count();
                            int tempThang = thang1 + thang2; //Thắng
                            int hoa1 = (from n2 in listhoa where n2.Doib == d.Tendoi select n2).Count();
                            int hoa2 = (from n1 in listhoa where n1.Doia == d.Tendoi select n1).Count();
                            int tempHoa = hoa1 + hoa2; //Hòa
                            int thua1 = (from n2 in listthua where n2.Doib == d.Tendoi select n2).Count();
                            int thua2 = (from n1 in listthua where n1.Doia == d.Tendoi select n1).Count();
                            int tempThua = thua1 + thua2; //Thua
                            int diem1 = (from n2 in listkq where n2.Doib == d.Tendoi select n2).Sum(item => int.Parse(item.kqB));
                            int diem2 = (from n1 in listkq where n1.Doia == d.Tendoi select n1).Sum(item => int.Parse(item.kqA));
                            int tempDiem = diem1 + diem2; //Diem
                            Console.WriteLine("  {0}\t  {1}\t\t{2}\t{3}\t{4}\t{5}\t{6}", d.Madoi, d.Tendoi, tempTran, tempThang, tempHoa, tempThua, tempDiem);

                    }
                    else
                    {
                        int tran1 = (from n2 in listlt where n2.Doib == d.Tendoi select n2).Count();
                        int tran2 = (from n1 in listlt where n1.Doia == d.Tendoi select n1).Count();
                        int tempTran = tran1 + tran2; //Trận
                        int thang1 = (from n2 in listthang where n2.Doib == d.Tendoi select n2).Count();
                        int thang2 = (from n1 in listthang where n1.Doia == d.Tendoi select n1).Count();
                        int tempThang = thang1 + thang2; //Thắng
                        int hoa1 = (from n2 in listhoa where n2.Doib == d.Tendoi select n2).Count();
                        int hoa2 = (from n1 in listhoa where n1.Doia == d.Tendoi select n1).Count();
                        int tempHoa = hoa1 + hoa2; //Hòa
                        int thua1 = (from n2 in listthua where n2.Doib == d.Tendoi select n2).Count();
                        int thua2 = (from n1 in listthua where n1.Doia == d.Tendoi select n1).Count();
                        int tempThua = thua1 + thua2; //Thua
                        int diem1 = (from n2 in listkq where n2.Doib == d.Tendoi select n2).Sum(item => int.Parse(item.kqB));
                        int diem2 = (from n1 in listkq where n1.Doia == d.Tendoi select n1).Sum(item => int.Parse(item.kqA));
                        int tempDiem = diem1 + diem2; //Diem
                        Console.WriteLine("  {0}\t  {1}\t\t{2}\t{3}\t{4}\t{5}\t{6}", d.Madoi, d.Tendoi, tempTran, tempThang, tempHoa, tempThua, tempDiem);

                    }
                }
                Console.WriteLine("=====================================================================");
        }

        //==============Menu điều khiển chương trình và Main run====//
        public void menu()
        {
            int n;
            do
            {
                Console.WriteLine("\n--Chao mung den voi V-League 2016--");
                Console.WriteLine("- 1. Quan ly danh sach doi bong.");
                Console.WriteLine("- 2. Quan ly lich thi dau.");
                Console.WriteLine("- 3. Quan ly ket qua thi dau.");
                Console.WriteLine("- 4. Thong ke.");
                Console.WriteLine("- 0. Thoat.");

                n = int.Parse(Console.ReadLine());
                switch (n)
                {
                    case 0:
                        Console.WriteLine("Da thoat khoi chuong trinh!");
                        break;
                    case 1:
                        danhSach(listdoi);
                        break;
                    case 2:
                        lichThi(listlt);
                        break;
                    case 3:
                        ketQua(listkq);
                        break;
                    case 4:
                        thongKe(listdoi, listlt, listkq);
                        break;
                    default:
                        Console.WriteLine("Vui long chon lai");
                        break;
                }
            } while (n.Equals(0) != true); //Equals return true and false
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.menu();
            Console.Read();
        }
    }
}
Class Doi (set - get - constructor)
Class Doi C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
    class Doi : IComparable<Doi>
    {
        public string Madoi { set; get; }
        public string Tendoi { set; get; }
        public string HLV { set; get; }


        public Doi(string madoi, string tendoi, string hlv)
        {
            Madoi = madoi;
            Tendoi = tendoi;
            HLV = hlv;
        }

        public Doi() { }
        int IComparable<Doi>.CompareTo(Doi tother)
        {
            throw new NotImplementedException();
        }

    }
}
Class Lichthi (set - get - constructor)
Class Lichthi C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
    class Lichthi
    {
        public string Doia { set; get; }
        public string Doib { set; get; }
        public string Ngaythi { set; get; }
        public string Giothi { set; get; }
        public string Santhi { set; get; }

        public Lichthi(string doia, string doib, string ngaythi, string giothi, string santhi)
        {
            Doia = doia;
            Doib = doib;
            Ngaythi = ngaythi;
            Giothi = giothi;
            Santhi = santhi;
        }
        public Lichthi() { }
    }
}
Class Ketqua (set - get - constructor)
Class Ketqua C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
    class Ketqua
    {
        public string Doia { set; get; }
        public string Doib { set; get; }
        public string kqA { set; get; }
        public string kqB { set; get; }

        public int Thang { set; get; }
        public int Thua { set; get; }
        public int Hoa { set; get; }

        public Ketqua() { }
        public Ketqua(string doia, string doib, string kqa, string kqb)
        {
            Doia = doia;
            Doib = doib;
            kqA = kqa;
            kqB = kqb;
        }

    }
}
Class Sắp xếp theo tên đội list.Sort(new CompareTenDoi())
Class CompareTenDoi C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
    class CompareTenDoi : IComparer<Doi>
    {
        int IComparer<Doi>.Compare(Doi x, Doi y)
        {
            if (x == null || x.Tendoi == null)
            {
                return -1;
            }
            if (y == null || y.Tendoi == null)
            {
                return 1;
            }
            if (x.Tendoi == y.Tendoi)
            {
                return 0;
            }
            return x.Tendoi.CompareTo(y.Tendoi);
            throw new NotImplementedException();
        }
    }
}
Class Sắp xếp theo mã đội list.Sort(new CompareMaDoi())
Class CompareMaDoi C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment
{
    class CompareMaDoi : IComparer<Doi>
    {
        int IComparer<Doi>.Compare(Doi x, Doi y)
        {
            if (x == null || x.Madoi == null)
            {
                return -1;
            }
            if (y == null || y.Madoi == null)
            {
                return 1;
            }
            if (x.Madoi == y.Madoi)
            {
                return 0;
            }
            return x.Madoi.CompareTo(y.Madoi);
            throw new NotImplementedException();
        }
    }
}

 

BACK TO TOP

Xuống cuối trang