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

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

Tạo class Product gồm các thuộc tính:

-       Tên hàng hóa

-       Nhà sản xuất

-       Giá bán

+ Tạo 2 constructor cho lớp này.

+ Cài đặt hàm nhập và hiển thị.

 

Tạo class ProductMenu, khai báo hàm main và tạo menu sau:

1.    Nhập thông tin cho n sản phẩm

2.    Hiển thị thông tin vừa nhập

3.    Sắp xếp thông tin giảm dần theo giá và hiển thị

4.    Thoát.

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

5

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

Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2021-05-19 10:21:45



using System;
using System.Collections.Generic;
using System.Text;
namespace Product
{
    class Program
    {
        static int c;
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            Console.OutputEncoding = Encoding.UTF8;
            List<Product> Products = new List<Product>();
            do
            {
                menu();
               

                isChoose();
                switch (c)
                {
                    case 1:
                        input(Products);
                        break;
                    case 2:
                        display(Products);
                        break;
                    case 3:
                        sort(Products);
                        display(Products);
                        break;
                    case 4:
                        Console.WriteLine("Ket thuc !!");
                        break;
                    default:
                        Console.WriteLine("nhap sai !!");
                        break;

                }

            } while (c != 4);
        }
        static void menu()
        {
            Console.WriteLine("1.    Nhập thông tin cho n sản phẩm");
            Console.WriteLine("2.    Hiển thị thông tin vừa nhập");
            Console.WriteLine("3.    Sắp xếp thông tin giảm dần theo giá và hiển thị");
            Console.WriteLine("4.    Thoát.");
           
        }
        static void input(List<Product> Products)
        {
            for (; ; )
            {
                Product product = new Product();
                product.input();
                Products.Add(product);
                Console.WriteLine("nhap them product : (Y/N)");
                string option = Console.ReadLine();
                if (option.Equals("N"))
                {
                    break;
                }
            }
        }
        static void display(List<Product> Products)
        {
            foreach (var item in Products)
            {
                item.display();
            }
        }
        static void sort(List<Product> Products)
        {
            Products.Sort((x, y) => y.price.CompareTo(x.price));
        }
        static void isChoose()
        {
            bool isName = false;
            while (!isName)
            {

                Console.WriteLine("Chon : ");
                string choose = Console.ReadLine();


                if (!string.IsNullOrEmpty(choose))
                {

                    if (checkc(choose))
                    {
                        c = int.Parse(choose);

                        isName = true;
                    }
                    else
                    {
                        Console.WriteLine("errol--> ko dc nhap chu!");
                    }
                    
                   
                }
                else
                {
                    Console.WriteLine("errol--> Null!");
                }

            }
        }
        static bool checkc(string s)
        {
            bool check = true;
            foreach (char item in s)
            {
                if (item < 48 || item > 57)
                {
                    check = false;
                    break;
                }
                
            }
            return check;
        }


    }
}



Bùi Văn Mạnh [T2008A]
Bùi Văn Mạnh

2021-05-19 09:59:50



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

namespace product
{
class ProductMenu
    {
        static void Main(string[] args)
        {
            List<Product> ListProduct = new List<Product>();
            int choice = 0;
            do
            {
                Menu();
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Nhap vao so N");
                        int n = int.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++)
                        {
                            Console.WriteLine("Nhap vao thong tin Product thu" + (i + 1));
                            Product nProduct = new Product();
                            nProduct.Input();
                            ListProduct.Add(nProduct);
                        }
                        break;
                    case 2:
                        foreach (Product prd in ListProduct)
                        {
                            prd.Output();
                        }
                        break;
                    case 3:
                        for (int i = 0; i < ListProduct.Count - 1; i++)
                        {
                            for (int j = i + 1; j < ListProduct.Count; j++)
                            {
                                if (ListProduct[i].Price < ListProduct[j].Price)
                                {
                                    Product temp = ListProduct[i];
                                    ListProduct[i] = ListProduct[j];
                                    ListProduct[j] = temp;
                                }
                            }
                        }
                        foreach (Product prd in ListProduct)
                        {
                            prd.Output();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Bye Bye...");
                        return;
                    default:
                        Console.WriteLine("Invalid input");
                           break;
                }
            }
            while (choice != 4);
            {
                static void Menu()
                {
                    Console.WriteLine("Nhap thong tin cho n san pham");
                    Console.WriteLine("Hien thi thong tin vua nhap");
                    Console.WriteLine("Sap xep thong tin giam dan theo gia va hien thi");
                    Console.WriteLine("Thoat");
                }
            }

        }
    }
}



Bùi Văn Mạnh [T2008A]
Bùi Văn Mạnh

2021-05-19 09:58:31



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

namespace product
{
    class Product
    {
        public string Name { get; set; }
        public string Producer { get; set; }
        public float Price { get; set; }
        public Product() { }
        public Product(string Name , string Producer , float Price)
        {
            Name = Name;
            Producer = Producer;
            Price = Price;
        }
        public void Input()
        {
            Console.WriteLine("Input product Name");
            Name = Console.ReadLine();
            Console.WriteLine("Input Producer");
            Producer = Console.ReadLine();
            Console.WriteLine("Input Price");
            Price = int.Parse(Console.ReadLine());
        }
        public void Output()
        {
            Console.WriteLine("product Name:" + Name);
            Console.WriteLine("Producer:" + Producer);
            Console.WriteLine("Price:" + Price);
        }
    }
}






nguyễn Sử [T2008A]
nguyễn Sử

2021-05-19 09:47:49



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

namespace quanlisp
{
    class product
    {
        public string tenhanghoa { get; set; }
        public string nhasanxuat { get; set; }
        public string giaban { get; set; }

        public product()
        {

        }
        public product(string tenhanghoa, string nhasanxuat, string giaban)
        {
            this.tenhanghoa = tenhanghoa;
            this.nhasanxuat = nhasanxuat;
            this.giaban = giaban;
        }
        public void Input()
        {
            Console.WriteLine("ten hang hoa: ");
            string tenhanghoa = Console.ReadLine();
            Console.WriteLine("nha san xuat: ");
            string nhasanxuat = Console.ReadLine();
            Console.WriteLine("gia ban: ");
            string giaban = Console.ReadLine();
        }
        public void Display()
        {
            Console.WriteLine("ten hang hoa :{0}, nha san xuat :{1}, gia ban :{2}", tenhanghoa, nhasanxuat, giaban);
        }
    }
}



nguyễn Sử [T2008A]
nguyễn Sử

2021-05-19 09:47:33



using System;
using System.Collections.Generic;

namespace quanlisp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            product product = new product();
            List<product> productList = new List<product>();

            int chon;
            int n;
            do
            {
                Menu();
                chon = Convert.ToInt32(Console.ReadLine());
                switch (chon)
                {
                    case 1:
                        Console.WriteLine("nhap ten san pham: ");
                        product.Input();
                        productList.Add(product);
                        break;
                    case 2:
                        Console.WriteLine("hien thi thong tin san pham: ");
                        foreach( product product1 in productList)
                        {
                            product.Display();
                        }
                        break;
                    case 3:
                        Console.WriteLine("sap xep va hien thi");
                        for (int i = 0; i < productList.Count - 1; i++)
                        {
                            for (int j = i + 1; j < productList.Count; j++)
                            {
                                if (productList[i].giaban < productList[j].giaban)
                                {
                                    product product1 = productList[i];
                                    productList[i] = productList[j];
                                    productList[j] = product1;
                                }
                            }
                        }
                        product.Display();
                        break;
                    case 4:
                        Console.WriteLine("thoat: ");
                        break;
                    default:
                        Console.WriteLine("nhap lai !!!!!!!!");
                        break;
                }

            } while (chon !=5);

            static void Menu()
            {
                Console.WriteLine("-------- Menu --------");
                Console.WriteLine("1: nhap thong tin san pham: ");
                Console.WriteLine("2: hien thi thong tin vua nhap: ");
                Console.WriteLine("3: sap xep thong tin giam dan theo gia va hien thi: ");
                Console.WriteLine("4: thoat: ");
                Console.WriteLine("chon: ");
            }
        }
    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-05-19 09:29:34


#Product.cs


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

namespace QuanLySanPham.product
{
    class Product
    {
        public string name { get; set; }
        public string nsx { get; set; }
        public float price { get; set; }
        public Product()
        {

        }
        public Product(string name, string nsx,float price)
        {
            this.name = name;
            this.nsx = nsx;
            this.price = price;
        }
        public void input()
        {
            Console.WriteLine("Ten san pham:");
            name = Console.ReadLine();
            Console.WriteLine("Nha san xuat:");
            nsx = Console.ReadLine();
            Console.WriteLine("Nhap gia ban:");
            price = float.Parse(Console.ReadLine());
        }
        public void display()
        {
            Console.WriteLine("Ten san pham: {0}, Nha san xuat: {1}, Gia ban: {2}", name, nsx,price);
        }
    }
}


#ProductMenu.cs


using System;
using System.Collections.Generic;
using QuanLySanPham.product;

namespace QuanLySanPham.abc
{
    class ProductMenu
    {
        static void Main(string[] args)
        {
            int choose=0;
            List<Product> productList = new List<Product>();
            do
            {
                Menu();
                choose = int.Parse(Console.ReadLine());

                switch (choose)
                {
                    case 1:
                        Console.WriteLine("So san pham muon them:");
                        int N = int.Parse(Console.ReadLine());
                        for(int i = 0; i < N; i++)
                        {
                            Console.WriteLine("Nhap san pham thu {0}:", productList.Count + 1);
                            Product product = new Product();
                            product.input();
                            productList.Add(product);
                        }
                        break;
                    case 2:
                        for (int i = 0; i < productList.Count; i++)
                        {
                            Console.WriteLine("Thong tin san pham thu {0}:", i + 1);
                            productList[i].display();
                        }
                        break;
                    case 3:
                        for(int i = 0; i < productList.Count - 1; i++)
                        {
                            for(int j = i+1; j < productList.Count; j++)
                            {
                                if(productList[i].price < productList[j].price)
                                {
                                    Product tmp = productList[i];
                                    productList[i] = productList[j];
                                    productList[j] = tmp;
                                }
                            }
                        }
                        Console.WriteLine("Thong tin san pham sau khi da sap xep giam dan theo gia ban:");
                        for (int i = 0; i < productList.Count; i++)
                        {
                            Console.WriteLine("Thong tin san pham thu {0}:", i + 1);
                            productList[i].display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Tam biet!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!");
                        break;
                }
            } while (choose != 4);
        }
        static void Menu()
        {
            Console.WriteLine("Lua chon chuong trinh:");
            Console.WriteLine("1.Nhap thong tin cho N san pham");
            Console.WriteLine("2.Hien thi thong tin vua nhap");
            Console.WriteLine("3.Sap xep thong tin giam dan theo gia va hien thi");
            Console.WriteLine("4.Thoat");
        }
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-05-19 09:18:15


#Product.cs


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

namespace les3bt1
{
    class Product
    {
        public string Name { get; set; }
        public string Menufacture { get; set; }
        public float Price { get; set; }

        public Product()
        {

        }
        public Product(string Name, string Menufacture, float Price) {
            this.Name = Name;
            this.Menufacture = Menufacture;
            this.Price = Price;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten Hang Hoa:");
            Name = Console.ReadLine();
            Console.WriteLine("Nhap nsx:");
            Menufacture = Console.ReadLine();
            Console.WriteLine("nhap gia:");
            Price = float.Parse(Console.ReadLine());
        }
        public void Display()
        {
            Console.WriteLine("Name : {0}, Menufacture : {1}; Price: {2}", Name, Menufacture, Price);
        }
    }
}


#Program.cs


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

namespace les3bt1
{
    class ProductMenu
    {   
        static List<Product> productList = new List<Product>();
        static void Main(string[] args)
        {
            int choose;
            do
            {
                showMenu();
                Console.WriteLine("Lua chon chuong trinh:");
                choose = Convert.ToInt32(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        InputInfor();
                        break;
                    case 2:
                        ShowInfor();
                        break;
                    case 3:
                        SortInfor();
                        break;
                    case 4:
                        Console.WriteLine("Thoat!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;

                }
            } while (choose != 4);


        }

        private static void SortInfor()
        {
            for(int i = 0; i < productList.Count -1; i++)
            {
               for(int j = i+1; j < productList.Count; j++)
                {
                    if (productList[i].Price < productList[j].Price)
                    {
                        Product temp = productList[i];
                        productList[i] = productList[j];
                        productList[j] = temp;
                    }
                }
            }
            foreach (Product prs in productList)
            {
                prs.Display();
            }
        }

      

        private static void InputInfor()
        {
            Console.WriteLine("Nhap n san pham:");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++ )
            {
                Product pr = new Product();
                pr.Input();
                productList.Add(pr);
            }
        }
        private static void ShowInfor()
        {
            foreach (Product pr in productList)
            {
                pr.Display();
            }
        }
        static void showMenu()
        {
            Console.WriteLine("1.Nhap thong tin san pham:");
            Console.WriteLine("2.Hien thi thong tin san pham:");
            Console.WriteLine("3.Sap xep thong tin theo gia giam dan ");
            Console.WriteLine("4.Thoat!!!");
        }
    }
}



hainguyen [T2008A]
hainguyen

2021-05-19 09:07:43



using System;
using System.Collections.Generic;

namespace lesson03
{
    class Program
    {
        static List<Product> listProduct { get; set; }
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.Unicode;
            listProduct = new List<Product>();
            int choose;
            do
            {
                showMenu();
                choose = int.Parse(Console.ReadLine());
                switch(choose)
                {
                    case 1:
                        Nhap();
                        break;
                    case 2:
                        HienThi();
                        break;
                    case 3:
                        SapXep();
                        break;
                    case 4:
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Fail!");
                        break;
                }
            } while (choose != 4);

            static void showMenu()
            {
                Console.WriteLine("1. Nhap");
                Console.WriteLine("2. Hien thi");
                Console.WriteLine("3. Sap xep");
                Console.WriteLine("4. Thoat");
                Console.WriteLine("Chon: ");
            }

            static void Nhap()
            {
                Console.WriteLine("Nhap product: ");
                int n = int.Parse(Console.ReadLine());
                for (int i = 0; i < n; i++)
                {
                    Product product = new Product();
                    product.input();
                    listProduct.Add(product);
                }
            }

            static void HienThi()
            {
                foreach (Product p in listProduct)
                {
                    p.display();
                }
            }

            static void SapXep()
            {
                for (int i = 0; i < listProduct.Count-1; i++)
                {
                    for (int j = i+1; j < listProduct.Count; j++)
                    {
                        if (listProduct[i].price < listProduct[j].price)
                        {
                            Product tmp = listProduct[i];
                            listProduct[i] = listProduct[j];
                            listProduct[j] = tmp;
                        }
                    }
                }
                HienThi();
            }
        }
    }
}



hainguyen [T2008A]
hainguyen

2021-05-19 09:07:23



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

namespace lesson03
{
    class Product
    {
        public string name { get; set; }
        public string nsx { get; set; }
        public float price { get; set; }

        public Product () { }

        public Product (string name, string nsx, float price)
        {
            this.name = name;
            this.nsx = nsx;
            this.price = price;
        }

        public void input()
        {
            Console.WriteLine("Nhap name: ");
            name = Console.ReadLine();

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

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

        public void display()
        {
            Console.WriteLine("name: = {0}, nsx: = {1}, price: = {2}", name, nsx, price);
        }
    }
}



Nguyễn đình quân [T2008A]
Nguyễn đình quân

2021-05-19 09:03:08




using System;
using System.Collections.Generic;

namespace QLSanPham
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> productList = new List<Product>();
            int choose;
            do
            {             
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch (choose)
                {
                    case 1:
                        Console.WriteLine("N san pham muon them ");
                        int n = int.Parse(Console.ReadLine());
                        Product pro = new Product();
                        for (int i=0; i <n; i++)
                        {
                            pro = new Product();
                            pro.Input();
                            productList.Add(pro);
                        }
                        break;
                    case 2:
                        foreach(Product propro in productList)
                        {
                            propro.Display();
                        }
                        break;
                    case 3 :
                        productList.Sort((a, b) => a.Price.CompareTo(b.Price));
                        foreach (Product propro in productList)
                        {
                            propro.Display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat");
                        break;
                    default:
                        Console.WriteLine("Nhap khong chinh xac ( chi nhap tu 1-4)");
                        break;
                        
                }
            } while (choose != 5);
        }
            static void ShowMenu()
            {
                Console.WriteLine("1. So san pham muon them : ");
                Console.WriteLine("2. Thong tin vua nhap : ");
                Console.WriteLine("3. Sap xep thong tin giam dan theo gia va hien thi : ");
                Console.WriteLine("4. Thoat : ");
            }
    }
}



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

namespace QLSanPham
{
    class Product
    {
        public string NameProduct { get; set; }
        public string Producer { get; set; }
        public float Price { get; set; }
        public Product()
        {

        }
        public Product(string NameProduct,string Producer,float Price)
        {
            this.Producer = Producer;
            this.NameProduct = NameProduct;
            this.Price = Price;
        }
        public void Input()
        {
            Console.WriteLine("Nhap ten san pham :");
            NameProduct = Console.ReadLine();
            Console.WriteLine("Nhap nha san xuat :");
            Producer = Console.ReadLine();
            Console.WriteLine("Nhap gia tien :");
            Price = float.Parse(Console.ReadLine());
        }
        public void Display()
        {
            Console.WriteLine("Ten san pham : {0},Nha san xuat : {1}, Gia ban : {2}",NameProduct,Producer,Price);
        }
    }
}