By GokiSoft.com| 13:59 28/10/2021|
C Sharp

[Video] Bài 1: Chương trình quản lý sản phẩm - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP - AAHN-C2009G2

Link Video Bài Giảng

Chương trình quản lý sản phẩm - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP

#Utility.cs


using System;
namespace BT1434
{
    public class Utility
    {
        public static int ReadInt()
        {
            int value;

            while(true)
            {
                try
                {
                    value = int.Parse(Console.ReadLine());
                    return value;
                } catch(Exception ex)
                {
                    Console.WriteLine("Nhap lai: ");
                }
            }
        }

        public static float ReadFloat()
        {
            float value;

            while (true)
            {
                try
                {
                    value = float.Parse(Console.ReadLine());
                    return value;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Nhap lai: ");
                }
            }
        }
    }
}


#Program.cs


using System;

namespace BT1434
{
    class Program
    {
        static void Main(string[] args)
        {
            //Cau 1: Khai bao mang
            Console.WriteLine("Nhap So San Pham N = ");
            int N = Utility.ReadInt();

            Product[] productList = new Product[N];

            //Cau 2: Nhap thong tin san pham
            for(int i=0;i<N;i++)
            {
                Product p = new Product();
                p.Input();

                productList[i] = p;
            }

            //Cau 3: Tim SP co gia ban cao nhat
            Product productFind = null;
            for(int i=0;i<N;i++)
            {
                if(productFind == null || productFind.Gia1SP < productList[i].Gia1SP)
                {
                    productFind = productList[i];
                }
            }

            if(productFind != null)
            {
                productFind.Display();
            }
        }
    }
}


#Product.cs


using System;
namespace BT1434
{
    public class Product
    {
        public string MaHH { get; set; }
        public string TenHH { get; set; }
        public int SoLuong { get; set; }
        public float Gia1SP { get; set; }

        public Product()
        {
        }

        public Product(string maHH, string tenHH, int soLuong, float gia1SP)
        {
            MaHH = maHH;
            TenHH = tenHH;
            SoLuong = soLuong;
            Gia1SP = gia1SP;
        }

        public void Input()
        {
            Console.WriteLine("Nhap MaHH: ");
            MaHH = Console.ReadLine();

            Console.WriteLine("Nhap TenHH: ");
            TenHH = Console.ReadLine();

            Console.WriteLine("Nhap So Luong: ");
            SoLuong = Utility.ReadInt();

            Console.WriteLine("Nhap Gia Ban: ");
            Gia1SP = Utility.ReadFloat();
        }

        public void Display()
        {
            Console.WriteLine("MaHH: {0}, TenHH: {1}, So Luong: {2}, Gia: {3}",
                MaHH, TenHH, SoLuong, Gia1SP);
        }
    }
}




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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó