By GokiSoft.com| 14:45 17/05/2021|
C Sharp

[Share Code] Tìm hiểu về mảng trong C# - Array in C# - Class Object in C# - Lớp đối tượng trong C# - Lập trình C#

[Share Code] Tìm hiểu về mảng trong C# - Array in C# - Class Object in C# - Lớp đối tượng trong C# - Lập trình C#

Viết chương trình quản thư viên
- Book:
	Thuộc tính
		- BookCode
		- Title
		- AuthorName
		- Price
		- Nxb
	Hành động (Method)
		- Hàm nhập & hiển thị
- Account
	Thuộc tính
		- UserName
		- Email
		- Password
	Hành động (Method)
		- Hàm nhập & hiển thị




#Program.cs


using System;
using Lesson02.Modals;
using System.Collections.Generic;

namespace Lesson02
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //learnArray();
            Book book = new Book();
            //book.BookCode = "Xin Chao";
            book.SetBookCode("OKOKOK");
            book.Title = "Xin chao";
            book.Price = -1000;
            book.Display();

            Book book2 = new Book("B01", "Lap Trinh C", "ABC", 100000, "Aptech");
            book2.Display();
        }

        static void learnArray()
        {
            Console.WriteLine("Learn Array in C#!");
            // Phan 1: Tim hieu mang index datatype[] vars -> fix length cua array.
            // Khai bao mang rong -> gom 5 so nguyen trong C#
            int[] t = new int[5];
            float[] f = new float[5];
            string[] s = new string[5];
            String[] ss = new String[5];

            // Them gia tri vao trong mang
            t[0] = 5;
            Console.WriteLine("Nhap gia tri t[1] = ");
            t[1] = Convert.ToInt32(Console.ReadLine());
            //t[1] = Int32.Parse(Console.ReadLine())
            for (int i = 2; i < 5; i++)
            {
                Console.WriteLine("Nhap gia tri t[{0}] = ", i);
                t[i] = Convert.ToInt32(Console.ReadLine());
            }

            //Hien thi du lieu ra
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Nhap gia tri t[{0}] = {1}", i, t[i]);
            }
            // Phan 2: Khai bao mang gom cac phan tu co san
            int[] k = { 3, 5, 9, 10, 2 };
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Nhap gia tri k[{0}] = {1}", i, k[i]);
            }

            // Phan 3: Khai bang mang dynamic trong C#
            //int -> float, double, string, String, Class Object, ...
            List<int> t1 = new List<int>();
            //length = t1.Count
            //Them phan tu vao trong mang
            t1.Add(32);
            t1.Add(7);
            t1.Add(7);
            t1.Add(7);
            t1.Add(88);
            //Lay cac phan tu trong mang
            for (int i = 0; i < t1.Count; i++)
            {
                Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
            }

            foreach (int v in t1)
            {
                Console.WriteLine("Nhap gia tri: {0}", v);
            }

            //Sua du lieu
            t1[0] = 111;

            for (int i = 0; i < t1.Count; i++)
            {
                Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
            }

            //Xoa 1 phan tu trong mang
            //t1.RemoveAt(0);
            t1.Remove(7);

            for (int i = 0; i < t1.Count; i++)
            {
                Console.WriteLine("Nhap gia tri t1[{0}] = {1}", i, t1[i]);
            }
        }
    }
}


#Book.cs


using System;

namespace Lesson02.Modals
{
    public class Book
    {
        //private, protected, public, frienly
        //Phan 1: Tim hieu getter/setter
        string BookCode;
        //Viet theo style cua java -> trong C# ko su dung cach nay.
        public void SetBookCode(string BookCode)
        {
            this.BookCode = BookCode;
        }

        public string GetBookCode()
        {
            return this.BookCode;
        }

        //Viet tuong minh
        private string __Title2;
        public string Title2
        {
            get
            {
                return __Title2;
            }

            set
            {
                __Title2 = value;
            }
        }

        //Rut gon -> get/set -> public
        public string Title { get; set; }

        //get -> public, set -> private
        public string AuthorName { get; private set; }

        //get -> private, set -> public
        public string Nxb { private get; set; }

        //price > 0 -> khong co gia am.
        private float _Price;
        public float Price {
            //private get
            get
            {
                return _Price;
            }

            //private set
            set
            {
                if(value <= 0)
                {
                    Console.WriteLine("Price khong duoc phep am!!!");
                } else
                {
                    _Price = value;
                }
            }
        }

        public Book()
        {
        }

        public Book(string BookCode, string Title, string AuthorName, float Price, string Nxb)
        {
            this.BookCode = BookCode;
            this.AuthorName = AuthorName;
            this.Title = Title;
            this.Price = Price;
            this.Nxb = Nxb;
        }

        public void Input()
        {
            Console.WriteLine("Input...");
        }

        public void Display()
        {
            Console.WriteLine("Display... {0} - {1} - Price: {2}", Title, BookCode, Price);
        }
    }
}


#Account.cs


using System;
namespace Lesson02.Modals
{
    public class Account
    {
        public Account()
        {
        }
    }
}


Tags:

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

5

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

Do Trung Duc [T2008A]
Do Trung Duc

2021-05-18 04:43:14



using System;
using System.Collections.Generic;

namespace Lesson2.StudentMark
{
    class Student
    {
        public string Roolno { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }

        public string Subject { get; set; }
       private int _Mark;
        public int Mark
        {
            get
            {
                return _Mark;
            }
            set
            {
                if(value<0 || value > 10)
                {
                    Console.WriteLine("Nhap diem thi sai");
                }
                else
                {
                    _Mark = value;
                }
            }
        }

        public void Input()
        {
            Console.WriteLine("Nhap ma hoc sinh:");
            Roolno = Console.ReadLine();
            Console.WriteLine("Nhap ten hoc sinh:");
            Name = Console.ReadLine();
            Console.WriteLine("Nhap lop hoc sinh:");
            Class = Console.ReadLine();
            Console.WriteLine("Nhap mon hoc:");
            Subject = Console.ReadLine();
            Console.WriteLine("Nhap diem mon hoc:");
            Mark = Int32.Parse(Console.ReadLine());

        }
        public void Display()
        {
            Console.WriteLine("Ma: {0}, Ten: {1}, Lop: {2}, Mon hoc: {3}, Diem: {4} ", Roolno, Name, Class, Subject, Mark);
        }

        static void Main(string[] args)
        {
            List<Student> StudenList = new List<Student>();
            Console.WriteLine("Nhap so luong hoc sinh can them");
            int N = Int32.Parse(Console.ReadLine());

            int MaxIndex = 0;
            int MaxMark = 0;
            for(int i = 0; i < N; i++)
            {
                Student student = new Student();
                student.Input();
                StudenList.Add(student);

                if(StudenList[i].Mark >= MaxMark)
                {
                    MaxIndex = i;
                }
            }

            Console.WriteLine("Thong tin hoc sinh co diem cao nhat nhu sau:");
            StudenList[MaxIndex].Display();
        }
    }
}