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)

Minh Nghia [T1907A]
Minh Nghia

2020-05-26 08:40:24



using System;
using System.Collections.Generic;
using System.Data;

namespace BookProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int choose, n;
            List<AptechBook> listBook = new List<AptechBook>();

            do
            {
                showMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Console.WriteLine("\nNhap thong tin N sach :");
                        n = int.Parse(Console.ReadLine());

                        for (int i = 0; i < n; i++)
                        {
                            AptechBook book = new AptechBook();
                            book.Input();
                            listBook.Add(book);
                        }
                        break;
                    case 2:
                        Console.WriteLine("\nHien thi thong sach :");
                        foreach (AptechBook item in listBook)
                        {
                            item.Display();
                        }
                        break;
                    case 3:
                        foreach(AptechBook book in listBook)
                        {
                            foreach(AptechBook book1 in listBook)
                            {
                                if(book._YearPublishing > book1._YearPublishing)
                                {
                                    int tmp = book._YearPublishing;
                                    book._YearPublishing = book1._YearPublishing;
                                    book1._YearPublishing = tmp;
                                }
                            }
                        }
                        foreach (AptechBook item in listBook)
                        {
                            item.Display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("\nNhap ten sach can tim :");
                        string bookName = Console.ReadLine();
                        foreach(AptechBook item1 in listBook)
                        {
                            if (bookName.Equals(item1._BookName))
                            {
                                item1.Display();
                            }
                            else
                            {
                                Console.WriteLine("Khong tim thay sach can tim !!!");
                                break;
                            }
                        }
                        break;
                    case 5:
                        Console.WriteLine("\nNhap tac gia can tim : ");
                        string authorName = Console.ReadLine();
                        foreach (AptechBook bn in listBook)
                        {
                            if (authorName.Equals(bn._BookAuthor))
                            {
                                bn.Display();
                            }
                            else
                            {
                                Console.WriteLine("Khong tim thay tac gia can tim !!!");
                                break;
                            }
                        }
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        Console.WriteLine("Thoat");
                        break;
                    default:
                        Console.WriteLine("Nue!!!");
                        break;
                }
            } while (choose != 8);
        }
        static void showMenu()
        {
            Console.WriteLine("1. Nhập thông tin n cuốn sách của Aptech");
            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 năm xuất bản và hiển thị");
            Console.WriteLine("4. Tìm kiếm theo tên sách");
            Console.WriteLine("5. Tìm kiếm theo tên tác giả");
            Console.WriteLine("6. Lưu thông tin sách đã nhập vào file");
            Console.WriteLine("7. Đọc nội dung từ file và lưu vào mang quản lý sách");
            Console.WriteLine("8. Thoát.");
        }
       
    }
}



lê văn phương [T1907A]
lê văn phương

2020-05-26 08:32:10



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

namespace produck
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            List<Product> products = new List<Product>();
            int choose;
            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        for (; ; )
                        {
                            Product pro = new Product();
                            pro.Input();
                            products.Add(pro);
                            Console.WriteLine("Bạn có muốn tiếp tục không ?(Y/N)");
                            String option = Console.ReadLine();
                            if (option.Equals("N"))
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        foreach (Product pro in products)
                        {
                            pro.display();
                        }
                        break;
                    case 3:
                        products.Sort((x, y) => y.price.CompareTo(x.price));
                        foreach (Product pro in products)
                        {
                            pro.display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoát !!!");
                        break;
                    default:
                        Console.WriteLine("Lỗi");
                        break;
                }

            } while (choose != 4);
            Console.ReadKey();
        }
        static void ShowMenu()
        {
            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 !!!");
            Console.Write("Choose: ");
        }



    }
}



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

namespace produck
{
    class Product
    {
        public string product { get; set; }
        public string producer { get; set; }
        public float price { get; set; }

        public Product() { }

        public void Input()
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Tên sản phẩm : ");
            product = Console.ReadLine();

            Console.WriteLine("Tên nhà sản xuất : ");
            producer = Console.ReadLine();

            Console.WriteLine("Nhập giá tiền : ");
            price = float.Parse(Console.ReadLine());
        }
        public void display()
        {
            Console.WriteLine("---------");
            Console.ReadLine();
            Console.WriteLine("Tên sản phẩm : {0} , Tên nhà sản xuất : {1} , Giá tiền : {2}", product, producer, price);
        }
    }
}



Đường Thanh Bình [T1907A]
Đường Thanh Bình

2020-05-26 07:30:22



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

namespace Product_Managent
{
    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;
        }
    }
}



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

namespace Product_Managent
{
    class Product
    {
        public string productName { get; set; }
        public string producer { get; set; }
        public double price { get; set; }

        public Product()
        {
            productName = null;

            producer = null;
            price = 0;
        }
        public Product(string productName ,string producer , double price)
        {
            this.price = price;
            this.producer = producer;
            this.productName = productName;
        }
        public void input()
        {
            proName();
            isproducer();
            isPrice();

        }
        public void display()
        {
            Console.WriteLine("productName : {0} , producer : {1} , price : {2} ", productName, producer, price);
        }
        private void proName()
        {
            bool isName = true;
            while (isName)
            {
                Console.WriteLine("Enter name of product : ");
                productName = Console.ReadLine();
                if (string.IsNullOrEmpty(productName))
                {
                    Console.WriteLine("errol--> productName is empty!");
                }
                else
                {
                    isName = false;
                }
            }
        }
        private void isproducer()
        {
            bool isName = true;
            while (isName)
            {

                Console.WriteLine("Enter name of producer : ");
                producer = Console.ReadLine();
                if (string.IsNullOrEmpty(producer))
                {
                    Console.WriteLine("errol--> productName is empty!");
                }
                else
                {
                    isName = false;
                }
            }
        }
        private void isPrice()
        {
            bool isName = false;
            while (!isName)
            {

                Console.WriteLine("Enter price of product : ");
                string strPrice = Console.ReadLine();

                if (!string.IsNullOrEmpty(strPrice))
                {
                    price = double.Parse(strPrice);
                    if (price >= 0)
                    {
                        isName = true;
                    }
                    else
                    {
                        Console.WriteLine("errol--> (price > 0 )!");
                    }
                }
                else
                {
                    Console.WriteLine("errol--> price is empty!");
                }

            }
        }
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-05-25 10:16:04



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;
        }


    }
}



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

namespace Product
{
    class Product
    {
        public string productName { get; set; }
        public string producer { get; set; }
        public double price { get; set; }
        
        public Product()
        {
            productName = null;

            producer = null;
            price = 0;

        }
        public Product(string productName ,string producer ,double price)
        {
            this.price = price;
            this.producer = producer;
            this.productName = productName;
        }
        public void input()
        {
            proName();
            isproducer();
            isPrice();
        }
        public void display()
        {
            Console.WriteLine("productName : {0} , producer : {1} , price : {2} ",productName,producer,price);
        }
        private void proName()
        {
            bool isName = true;
            while (isName)
            {
                Console.WriteLine("Enter name of product : ");
                productName = Console.ReadLine();
                if (string.IsNullOrEmpty(productName))
                {
                    Console.WriteLine("errol--> productName is empty!");
                }
                else
                {
                    isName = false;
                }
            }
        }
        private void isproducer()
        {
            bool isName = true;
            while (isName)
            {
                
                Console.WriteLine("Enter name of producer : ");
                producer = Console.ReadLine();
                if (string.IsNullOrEmpty(producer))
                {
                    Console.WriteLine("errol--> productName is empty!");
                }
                else
                {
                    isName = false;
                }
            }
        }
        private void isPrice()
        {
            bool isName = false;
            while (!isName)
            {

                Console.WriteLine("Enter price of product : ");
                string strPrice = Console.ReadLine();
                
                if (!string.IsNullOrEmpty(strPrice))
                {
                    price = double.Parse(strPrice);
                    if (price >= 0)
                    {
                        isName = true;
                    }
                    else
                    {
                        Console.WriteLine("errol--> (price > 0 )!");
                    }
                }
                else
                {
                    Console.WriteLine("errol--> price is empty!");
                }
                
            }
        }
       
    }
}



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-05-25 10:15:00



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

namespace Lession6
{
    class ProductMenu
    {
        public static int chosse;
        public static void Main(String[] agrs)
        {
            Console.InputEncoding = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;
            List<Product> Products = new List<Product>();
            
            do
            {
                showMenu();
                checkChosse();
                switch (chosse)
                {
                    case 1:
                        input(Products);
                        break;
                    case 2:
                        display(Products);
                        break;
                    case 3:
                        sort(Products);
                        display(Products);
                        break;
                    case 4:
                        Console.WriteLine("Thoat!!!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!! Moi nhap lai.....");
                        break;
                }
            } while (chosse != 4);
        }
        public static void showMenu()
        {
            Console.WriteLine("\t\t\tMenu");
            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.ToUpper().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));
        }
        private static void checkChosse()
        {
            bool isName = true;
            while (isName)
            {
                string str;
                Console.Write("Chon: ");
                str = Console.ReadLine();

                if (String.IsNullOrEmpty(str))
                {
                    Console.WriteLine("Lua chon khong duoc de trong!!!");
                }
                else
                {
                    if (check(str))
                    {
                        chosse = int.Parse(str);
                        if (chosse > 0)
                        {
                            isName = false;
                        }
                        else
                        {
                            Console.WriteLine("Lua chon phai lon hon 0");
                        }
                    } else
                    {
                        Console.WriteLine("Khong duoc nhap chu!! ");
                    }
                    
                }

            }
        }

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




using System;

namespace Lession6
{
    class Product
    {
        public string Name { get; set; }
        public string Producer { get; set; }
        private float _price;
        public float Price
        {
            get
            {
                return _price;
            }
            set
            {
            if (value >= 0)
                {
                    this._price = value;
                }
            else
                {
                    Console.WriteLine("Gia nhap vao phai lon hon 0");
                }
            }
        }

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

        public void input()
        {
            checkName();
            checkProducer();
            checkPrice();
        }

        public void display()
        {
            Console.WriteLine("Ten: {0}\nNha san xuat: {1}\nGia: {2}", Name, Producer, Price);
        }

        private void checkName()
        {
            bool isName = true;
            while(isName)
            {
                Console.Write("Nhap ten san pham: ");
                Name = Console.ReadLine();
                if (String.IsNullOrEmpty(Name))
                {
                    Console.WriteLine("Ten san pham khong duoc de trong!!!");
                }
                else
                {
                    isName = false;
                }
            }
        }
        private void checkProducer()
        {
            bool isName = true;
            while (isName)
            {
                Console.Write("Nhap ten nha san xuat: ");
                Producer = Console.ReadLine();
                if (String.IsNullOrEmpty(Producer))
                {
                    Console.WriteLine("Ten nha san khong duoc de trong!!!");
                }
                else
                {
                    isName = false;
                }
            }
        }

        private void checkPrice()
        {
            bool isName = true;
            while (isName)
            {
                Console.Write("Nhap gia san pham: ");
                string str;
                str = Console.ReadLine();
                
                if (String.IsNullOrEmpty(str))
                {
                    Console.WriteLine("Gia san pham khong duoc de trong!!!");
                }
                else
                {
                    Price = float.Parse(str);
                    if (Price > 0)
                    {
                        isName = false;
                    }
                }
                
            }
        }
    }
}



Trần Ngọc Hải [T1907A]
Trần Ngọc Hải

2020-05-25 10:05:28


#Product


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

namespace QLSP
{
    public class Product
    {
        public string TenHangHoa { get; set; }
        public string NhaSanXuat { get; set; }
        public float Gia { get; set; }



        public Product()
        {

        }
        public void Input()
        {
            Console.WriteLine("Nhap Ten Hang Hoa: ");
            TenHangHoa = Console.ReadLine();

            Console.WriteLine("Nhap ten Nha San Xuat: ");
            NhaSanXuat = Console.ReadLine();

            Console.WriteLine("Nhap gia tien : ");
            Gia = float.Parse(Console.ReadLine());
            
        }
        public void Output()
        {
            Console.WriteLine("Tên hàng hóa: {0} \nNhà sản xuất: {1} \nGiá bán: {2}", TenHangHoa, NhaSanXuat, Gia);
        }


    }
}
#Program


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

namespace QLSP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            List<Product> products = new List<Product>();
            int choose;
            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        for (; ; )
                        {
                            Product pro = new Product();
                            pro.Input();
                            products.Add(pro);
                            Console.WriteLine("Ban co muon nhap tiep ko ?(Y/N)");
                            String option = Console.ReadLine();
                            if (option.ToUpper().Equals("N"))
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        foreach (Product pro in products)
                        {
                            pro.Output();
                        }
                        break;
                    case 3:
                        products.Sort((x, y) => y.Gia.CompareTo(x.Gia));
                        foreach (Product pro in products)
                        {
                            pro.Output();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat");
                        break;
                    default:
                        Console.WriteLine("Error");
                        break;
                }

            } while (choose != 4);
            Console.ReadKey();
        }
        static void ShowMenu()
        {
            Console.WriteLine("1.Nhập thông tin 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.Thoat");
            Console.Write("Choose: ");
        }
    }
}



Luong Dinh Dai [T1907A]
Luong Dinh Dai

2020-05-25 09:26:14


#Product.cs


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

namespace QuanLySanPham
{
    class Product
    {
        public string Tenhanghoa { get; set; }
        public string Nhasanxuat { get; set; }
        public int Gia { get; set; }

        public Product() { }
        public Product(string tenhanghoa, string nhasanuat, int gia)
        {
            Tenhanghoa = tenhanghoa;
            Nhasanxuat = nhasanuat;
            Gia = gia;
        }
        public void Nhap()
        {
            Console.WriteLine("Nhap ten hang: ");
            Tenhanghoa = Console.ReadLine();
            Console.WriteLine("Nhap nha san xuat: ");
            Nhasanxuat = Console.ReadLine();
            Console.WriteLine("Nhap gia: ");
            Gia = Convert.ToInt32(Console.ReadLine());
        }
        public void Hienthi()
        {
            Console.WriteLine("Ten Hang: "+Tenhanghoa+"\nNha san xuat: "+Nhasanxuat+"\nGia: "+Gia);
        }
    }
}


#Program.cs


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

namespace QuanLySanPham
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> lst = new List<Product>();
            int sw;

            do
            {
                showmenu();
                sw = Convert.ToInt32(Console.ReadLine());
                switch (sw)
                {
                    case 1:
                        inputProduct(lst);
                        break;
                    case 2:
                        display(lst);
                        break;
                    case 3:
                        lst.Sort((x, y) => y.Gia.CompareTo(x.Gia));
                        foreach (var item in lst)
                        {
                            item.Hienthi();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat thanh cong");
                        break;
                    default:
                        Console.WriteLine("Chon sai");
                        break;

                }
            } while (sw != 4);
        }
        public static void showmenu()
        {
            Console.WriteLine("1.Nhap thong tin N san pham: \n2.Hien thi thong tin vua nhap: \n3.Sap xep thong tin giam dan theo gia tri va hien thi: \n4.Thoat");
        }

        public static void inputProduct(List<Product> list)
        {
            Console.WriteLine("nhap N san pham");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Product sp = new Product();
                sp.Nhap();
                list.Add(sp);
            }
        }

        public static void display(List<Product> list)
        {
            Console.WriteLine("Danh sach cac san pham ");
            foreach (var item in list)
            {
                item.Hienthi();
            }
        }


    }
}



NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-05-25 09:21:19



using System;
using System.Collections.Generic;

namespace BookProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;
            List<product> productlist = new List<product>();



            do
            {
                showMenu();
                Console.WriteLine("Nhap vao choice :");
                choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        while (true)
                        {
                            product product = new product();
                            product.input();
                            productlist.Add(product);

                            Console.WriteLine("Ban co muon nhap tiep khong ?");
                            String option = Console.ReadLine();
                            if (option.Equals("N"))
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        foreach (product product in productlist)
                        {
                            product.display();
                        }
                        break;
                    case 3:
                        Sort(productlist);
                        foreach (product product in productlist)
                        {
                            product.display();
                        }


                        break;
                    case 4:
                        Console.WriteLine("exit");
                        break;
                    default:
                        Console.WriteLine("Error");
                        break;
                }

            } while (choice != 4);

        }


        static public void Sort(List<product> productlist)
        {
            productlist.Sort((y,x) => x.price.CompareTo(y.price));
        }
        public static void showMenu()
        {
            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. Exit");
        }
    }
    }





Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-05-25 09:12:50


#Product.cs


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

namespace BaitapQaunliSP
{
    class Product
    {
        public string tenhanghoa { get; set; }
        public string nhasanxuat { get; set; }
        public float giaban { get; set; }
        public Product()
        {

        }
        public Product(String tenhanghoa, String nhasanxuat, float giaban)
        {
            this.tenhanghoa = tenhanghoa;
            this.nhasanxuat = nhasanxuat;
            this.giaban = giaban;
        }
        public void intput()
        {
            Console.Write("Nhập tên hàng hóa: ");
            tenhanghoa = Console.ReadLine();
            Console.Write("Nhập nhà sản xuất: ");
            nhasanxuat = Console.ReadLine();
            Console.Write("Giá bán: ");
            giaban = float.Parse(Console.ReadLine());
        }
        public void output()
        {
            Console.WriteLine("Tên hàng hóa: {0} \nNhà sản xuất: {1} \nGiá bán: {2}", tenhanghoa, nhasanxuat, giaban);
        }

    }
}


#Program.cs


using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace BaitapQaunliSP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            List<Product> listProduct = new List<Product>();
            int c;
            do
            {
                Menu();
                Console.Write("Chọn: ");
                c = int.Parse(Console.ReadLine());
                switch (c)
                {
                    case 1:
                        Console.Write("Nhập số lượng sản phẩm: ");
                        int n = int.Parse(Console.ReadLine());
                        for (int i = 1; i <= n; i++)
                        {
                            Product pro = new Product();
                            Console.WriteLine("================");
                            Console.Write("Nhập sản phẩm thứ {0}", i);
                            pro.intput();
                            listProduct.Add(pro);
                        }
                        break;
                    case 2:
                        Console.WriteLine("Sản phẩm vừa nhập: ");
                        Show(listProduct);
                        break;
                    case 3:
                        Sort(listProduct);
                        Show(listProduct);
                        break;
                    case 4:
                        Console.WriteLine("Thoats");
                        break;
                    default:
                        break;
                }
            } while (c != 8);
        }
        static void Menu()
        {
            Console.WriteLine("1. Nhập thông tin 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 Show(List<Product> listProduct)
        {
            int show = 0;
            foreach (var item in listProduct)
            {
                ++show;
                Console.WriteLine("==============");
                Console.Write("Sản phẩm thứ {0}", show);
                item.output();
            }
        }
        static void Sort(List<Product> listProduct)
        {
            listProduct.Sort((x, y) => x.giaban.CompareTo(y.giaban));
        }
    }
    }




Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-05-25 09:07:02



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

namespace BaiTapNgay25_5_2020
{
    class Product
    {
        public string ProductName{ get; set; }
        public string ProducerName { get; set; }

        public float Price { get; set; }

        public Product() { }
        public void Input()
        {
            Console.WriteLine("Enter product information: ");
            Console.Write("Enter product name: ");
            ProductName = Console.ReadLine();
            Console.Write("Enter producer name: ");
            ProducerName = Console.ReadLine();
            Console.Write("Enter product price: ");
            Price = float.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine("Product name: {0}, producer: {1}, price: {2}", ProductName, ProducerName, Price);
        }
    }
}



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

namespace BaiTapNgay25_5_2020
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> products = new List<Product>();
            int choose;
            do {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        for(; ; )
                        {
                            Product pro = new Product();
                            pro.Input();
                            products.Add(pro);
                            Console.WriteLine("Ban co muon nhap tiep ko ?(Y/N)");
                            String option = Console.ReadLine();
                            if (option.Equals("N"))
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        foreach(Product pro in products)
                        {
                            pro.Display();
                        }
                        break;
                    case 3:
                        products.Sort((x, y) => y.Price.CompareTo(X.Price));
                        foreach(Product pro in products)
                        {
                            pro.Display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat");
                        break;
                    default:
                        Console.WriteLine("Loi.");
                        break;
                }

            } while (choose != 4);
            Console.ReadKey();
        }
        static void ShowMenu()
        {
            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.Thoat");
            Console.Write("Choose: ");
        }
    }
}