By GokiSoft.com| 14:40 25/09/2021|
Java Advanced

[Share Code] Chương trình quản lý sản phẩm - Product - Lập Trình C# - Lập Trình C Sharp - AAHN - C2009G

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


#Product.cs


using System;
namespace BT1495
{
    public class Product
    {
        public string Name { get; set; }
        public string ManufacturerName { get; set; }
        public float Price { get; set; }

        public Product()
        {
        }

        public Product(string name, string manufacturerName, float price)
        {
            Name = name;
            ManufacturerName = manufacturerName;
            Price = price;
        }

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

            Console.WriteLine("Nhap nha san xuat: ");
            ManufacturerName = Console.ReadLine();

            Console.WriteLine("Nhap gia: ");
            Price = float.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine("Ten: {0}, nha san xuat: {1}, gia: {2}", Name, ManufacturerName, Price);
        }
    }
}


#Program.cs


using System;

namespace BT1495
{
    class ProductMenu
    {
        static Product[] products;

        static void Main_Comment(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch(choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        SortByPrice();
                        Display();
                        break;
                    case 4:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 4);
        }

        static void Input()
        {
            Console.WriteLine("Nhap so san pham can them: ");
            int N = int.Parse(Console.ReadLine());

            products = new Product[N];

            for(int i=0;i<N;i++)
            {
                Product p = new Product();
                p.Input();
                products[i] = p;
            }
        }

        static void Display()
        {
            if (products == null) return;

            Console.WriteLine("Thong tin san pham");
            for (int i = 0; i < products.Length; i++)
            {
                products[i].Display();
            }
        }

        static void SortByPrice()
        {
            if (products == null) return;

            Array.Sort<Product>(products, new Comparison<Product>(
                (p1, p2) => (p1.Price < p2.Price)?1:-1
                ));
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap N san pham");
            Console.WriteLine("2. Hien thi thong tin san pham");
            Console.WriteLine("3. Sap xep theo gia giam dan");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Test.cs


using System;
using System.Collections.Generic;

namespace BT1495
{
    class ProductTest
    {
        static List<Product> products;

        static void Main(string[] args)
        {
            products = new List<Product>();

            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch (choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        SortByPrice();
                        Display();
                        break;
                    case 4:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 4);
        }

        static void Input()
        {
            Console.WriteLine("Nhap so san pham can them: ");
            int N = int.Parse(Console.ReadLine());

            for (int i = 0; i < N; i++)
            {
                Product p = new Product();
                p.Input();

                products.Add(p);
            }
        }

        static void Display()
        {
            if (products == null) return;

            Console.WriteLine("Thong tin san pham");
            for (int i = 0; i < products.Count; i++)
            {
                products[i].Display();
            }
        }

        static void SortByPrice()
        {
            if (products == null) return;

            products.Sort(
                    (p1, p2) => (p1.Price < p2.Price) ? 1 : -1
                );
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap N san pham");
            Console.WriteLine("2. Hien thi thong tin san pham");
            Console.WriteLine("3. Sap xep theo gia giam dan");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


Tags:

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

5

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