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 BT1495

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.

Liên kết rút gọn:

https://gokisoft.com/1495

Bình luận

avatar
Nguyễn Hùng Anh [community,C2009G]
2021-10-07 04:01:17


#Product.cs


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

namespace Lesson02
{
    class Product
    {
        public string Name { get; set; }
        public string Manufactor { get; set; }
        private int _price;
        public int Price
        {
            get
            {
                return _price;
            }
            set
            {
                if(value >= 0)
                {
                    this._price = value;
                } else
                {
                    Console.WriteLine("Price must > 0");
                }
            }
        }

        public Product()
        {

        }

        public Product(string name, string manufactor, int price)
        {
            Name = name;
            Manufactor = manufactor;
            Price = price;
        }

        public void Input()
        {
            Console.WriteLine("Enter product's name: ");
            Name = Console.ReadLine();

            Console.WriteLine("Enter product's manufactor: ");
            Manufactor = Console.ReadLine();

            Console.WriteLine("Enter product's price: ");
            Price = int.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public override string ToString()
        {
            return "Name: " + Name + ", Manufactor: " + Manufactor + ", Price: " + Price;
        }

    }
}


#ProductMenu.cs


using System;

namespace Lesson02
{
    class ProductMenu
    {
        static Product[] productList;
        static void Main(string[] args)
        {
            Menu();
        }
        static void Menu()
        {
            Product product = new Product();
            int c;
            int n;
            do
            {
                ShowMenu();
                c = int.Parse(Console.ReadLine());
                switch(c)
                {
                    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 (c != 4);
        }

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

            productList = new Product[n];
            for (int i = 0; i < n; i++)
            {
                Product product = new Product();
                product.Input();

                productList[i] = product;
            }
        }

        static void Display()
        {
            foreach(Product product in productList)
            {
                product.Display();
            }
        }

        static void SortByPrice()
        {
            Array.Sort<Product>(productList, (p1, p2) => {
                if(p1.Price > p2.Price)
                {
                    return -1;
                }
                return 1;
            }); 
        }
        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap thong tin cho n san pham");
            Console.WriteLine("2. Hien thi thong tin san pham");
            Console.WriteLine("3. Sap xep thong tin giam dan theo gia va hien thi");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


avatar
Hieu Ngo [community,C2009G]
2021-10-07 03:50:33


#Product.cs


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

namespace Product
{
    class Product
    {
        public string Productname { get; set; }
        public string Manufacturer { get; set; }

        private float _price;

        public float Price
        {
            get
            {
                return _price;
            }
            set
            {
                if(value >= 0 )
                {
                    this._price = value;
                } else
                {
                    Console.WriteLine("price >= 0");
                }
            }
        }


        public Product()
        {
        }

        public Product(string productName, string manufacturer, float price)
        {
            Productname = productName;
            Manufacturer = manufacturer;
            Price = price;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten hang: ");
            Productname = Console.ReadLine();
            Console.WriteLine("Nhap ten nha san xuat: ");
            Manufacturer = Console.ReadLine();
            Console.WriteLine("Nhap gia ban:");
            Price = float.Parse(Console.ReadLine());
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public override string ToString()
        {
            return "Ten Hang: " + Productname + ", Nha san xuat: " + Manufacturer + ", price: " + Price;
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace Product
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> list = new List<Product>();
            int x;
            do
            {
                Menu();
                Console.WriteLine("Nhap so ban chon:");
                x = int.Parse(Console.ReadLine());

                switch (x)
                {
                    case 1:
                        Console.WriteLine("Nhap n:");
                        int n = int.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++)
                        {
                            Product product = new Product();
                            product.Input();
                            list.Add(product);
                        }
                        break;
                    case 2:
                        foreach (Product product in list)
                        {
                            product.Display();
                        }
                        break;
                    case 3:
                        list.Sort((p1, p2) => {
                            return p2.Price.CompareTo(p1.Price);
                        });
                        Console.WriteLine("After sort:");
                        foreach(Product product in list)
                        {
                           
                            product.Display();
                        }
                        break;

                    case 4:
                        return;
                }

            } while (x >= 1 && x <= 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 int sort(Product product1, Product product2)
        {
            return product1.Price.CompareTo(product2.Price);
        }
    }
}


avatar
Kim Văn Thiết [community,AAHN-C2009G]
2021-09-25 05:10:16



static void bai2()
        {
            ProductMenu menu = new ProductMenu();
            menu.Input();
            menu.Display();

            
        }

        class Product: IComparable<Product>
        {
            string ProductName { get; set; }
            string Manufactor { get; set; }

            private int _Price;
            int Price
            {
                get 
                {
                    return _Price;
                }

                set
                {
                    if(value < 0)
                    {
                        Console.WriteLine("Gia ban phai > 0!");
                        return;
                    }
                    _Price = value;
                } 
            }


            public Product()
            {

            }

            public Product(string productname, string manu, int price)
            {
                this.ProductName = productname;
                this.Manufactor = manu;
                this.Price = price;
            }

            public virtual void Input()
            {
                Console.WriteLine("Nhap ten san pham: ");
                ProductName = Console.ReadLine();
                Console.WriteLine("Nhap ten nha san xuat: ");
                Manufactor = Console.ReadLine();
                Console.WriteLine("Nhap gia ban: ");
                Price = int.Parse(Console.ReadLine());
            }

            public virtual void Display()
            {
                Console.WriteLine(ToString());
            }

            public string ToString()
            {
                return "Ten San Pham : " + ProductName + "\nTen Nha San Xuat:" + Manufactor + "\n Gia ban :" + Price;
            }

            public int CompareTo(Product p)
            {
                return p.Price.CompareTo(this.Price);
            }
        }

        class ProductMenu : Product
        {
            private List<Product> p = new List<Product>();
            public override void Input()
            {
                Console.WriteLine("Enter number of Product: ");
                int n = int.Parse(Console.ReadLine());
                for(int i = 0; i < n; i++)
                {
                    Console.WriteLine("Nhap thong tin san pham thu {0}",i+1);
                    Product sp = new Product();
                    sp.Input();
                    p.Add(sp);

                }
            }

            public override void Display()
            {
                p.Sort();
                foreach(Product pt in p)
                {   
                    pt.Display();
                    Console.WriteLine();
                }
            }

            
        }


avatar
Hoàng Thiện Thanh [community,AAHN-C2009G]
2021-09-23 15:47:13


#Program.cs


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

namespace ProductManager
{
    class Product : IComparable<Product>
    {
        
        public string ProductName { get; set; }
        public string Manufacturer { get; set; }
        private double price;
        public double Price { get
            {
                return price;
            }
            set
            {
                while (value < 0)
                {
                    Console.WriteLine("Price cannot be below 0");
                    Console.WriteLine("Enter Price again: ");
                    value = int.Parse(Console.ReadLine());
                    
                }
                this.price = value;

            }
        }
        public Product()
        {

        }

        public Product(string _ProductName, string _Manufacturer, double _Price)
        {
            this.ProductName = _ProductName;
            this.Manufacturer = _Manufacturer;
            this.Price = _Price;
        }

        public virtual void input()
        {
            Console.Write("\nEnter Product Name: ");
            ProductName = Console.ReadLine();
            Console.Write("\nEnter Manufacturer: ");
            Manufacturer = Console.ReadLine();
            Console.Write("\nEnter Price: ");
            Price = int.Parse(Console.ReadLine());
        }
        public virtual void display()
        {
            Console.WriteLine("\nProduct Name: {0}\nProducer: {1}\nPrice: {2}", ProductName, Manufacturer, Price);
        }

        public virtual int CompareTo(Product other)
        {
            if (this.Price.CompareTo(other.Price) > 0)
            {
                return this.Price.CompareTo(other.Price);
            }
            else
            {
                return 0;
            }
        }
    }

    class ProductMenu : Product
    {
        List<Product> AllProducts = new List<Product>();
        List<int> ID = new List<int>();
        private int n;
        public int N { set {
                while (value <= 0)
                {
                    Console.WriteLine("Maximum number need to be above 0");
                    value = int.Parse(Console.ReadLine());

                }
                this.n = value;
            }
            get
            {
                return n;
            }
        }

        public override void input()
        {
            Console.WriteLine("Please enter number of products:");
            N = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                var count = i + 1;
                Console.WriteLine("Product[{0}]", count);
                Console.WriteLine("Please enter Product Name: ");
                ProductName = Console.ReadLine();
                Console.WriteLine("Please enter manufacturer: ");
                Manufacturer = Console.ReadLine();
                Console.WriteLine("Please enter price: ");
                Price = int.Parse(Console.ReadLine());
                var p = new Product(ProductName, Manufacturer, Price);
                AllProducts.Add(p);
                ID.Add(count);
            }
            
        }
        public override void display() {
            for (int i = 0; i < AllProducts.Count; i++)
            {
                Console.WriteLine("Product[{0}] Product Name: {1}, Manufacturer: {2}, Price:{3}", ID[i] , AllProducts[i].ProductName, AllProducts[i].Manufacturer, AllProducts[i].Price);
            }
        }

        public void sort_Product_By_Price_desc()
        {
            AllProducts.Sort((a, b) => b.CompareTo(a));
            for (int i = 0; i < AllProducts.Count; i++)
            {
                Console.WriteLine("Product[{0}] Product Name: {1}, Manufacturer: {2}, Price:{3}", ID[i], AllProducts[i].ProductName, AllProducts[i].Manufacturer, AllProducts[i].Price);
            }
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            ProductMenu m = new ProductMenu();
            m.input();
            m.display();

            Console.WriteLine("Sorted by Price in descending order:");
            m.sort_Product_By_Price_desc();
            
        }
    }
}


avatar
Nguyễn Việt Hoàng [community,AAHN-C2009G]
2021-09-23 15:46:28


#product.cs


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

namespace Ss2
{
    class product : IComparable<product>
    {
        public string Name { get; set; }
        public string Producer { get; set; }
        private int _price;
        public int Price {
            get
            {
                return this._price;
            }
            set
            {
                while(value <= 0)
                {
                    Console.WriteLine("Price must be > 0 ");
                    Console.WriteLine("Enter Price :");
                    value = int.Parse(Console.ReadLine());
                    
                }
                this._price = value;
            }
        }
        public product()
        {

        }
        public product(string name, string producer, int price)
        {
            this.Name = name;
            this.Producer = producer;
            this.Price = price;
        }
        public virtual void input()
        {
            Console.WriteLine("Enter Name :");
            Name = Console.ReadLine();
            Console.WriteLine("Enter Producer :");
            Producer = Console.ReadLine();
            Console.WriteLine("Enter Price :");
            Price = int.Parse(Console.ReadLine());
        }
        public virtual void display()
        {
            Console.WriteLine("Name :{0} , Producer :{1} , Price :{2}", Name, Producer, Price);
        }
        public  int CompareTo(product other)
        {
            if(this.Price.CompareTo(other.Price) > 0)
            {
                return this.Price.CompareTo(other.Price);
            }else
            {
                return 0;
            }
        }


    }
}


#productMenu.cs


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

namespace Ss2
{
    class productMenu : product
    {
        List<product> save = new List<product>();
        //List<int> cPr = new List<int>();
        public productMenu()
        {
            
        }
        public override void input()
        {
            int count = 0, i = 0;
            Console.WriteLine("Enter Sum products :");
            int sum = int.Parse(Console.ReadLine());
            while(sum <= 0)
            {
                Console.WriteLine("Sum must be > 0 ");
                Console.WriteLine("Enter Sum products :");
                sum = int.Parse(Console.ReadLine());
            }
            int[] sumPrd = new int[sum];
            do
            {
                count = i + 1;
                Console.WriteLine("Enter Product [" + count + "] :");
               //cPr.Add(count);
                base.input();
                product c = new product(Name, Producer, Price);
                save.Add(c);
                i++;
               
            } while (i < sum);
        
        }
       

        public override void display()
        {
            Console.WriteLine("Products :");
           // foreach (int j in cPr)
          //  {
             //   Console.Write("Products[{0}]", j);
           // }
            foreach(product i in save)
            {
                Console.WriteLine("Name :{0} , Producer :{1} , Price :{2}", i.Name, i.Producer, i.Price);
            }
        }
        public void display_by_des_price()
        {
            Console.WriteLine("Sort by des price :");
            save.Sort((a,b) => b.CompareTo(a));
            foreach (product i in save)
            {
                Console.WriteLine("Name :{0} , Producer :{1} , Price :{2}", i.Name, i.Producer, i.Price);
            }
        }
        static void Main(string[] args)
        {
           productMenu a = new productMenu();
            a.input();
            a.display();
            a.display_by_des_price();



        }
    }
}


avatar
Triệu Văn Lăng [T2008A]
2021-05-21 06:54:17


#Product.cs


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

namespace bai1495
{
    class Product
    {
        public string NameProduct { get; set; }
        public string NSX { get; set; }
        public float Price { get; set; }

        public Product() { }
        
        public Product (string NameProduct, string NSX, float Price)
        {
            this.NameProduct = NameProduct;
            this.NSX = NSX;
            this.Price = Price;
        }

        public void input()
        {
            Console.WriteLine("Nhap ten hang hoa: ");
            NameProduct = Console.ReadLine();

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

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

        public void display()
        {
            Console.WriteLine("Ten hang hoa: {0}, Nha san xuat: {1}, Gia ban: {2}", NameProduct, NSX, Price);
        }
    }
}


#Program.cs


using System;
using bai1495;
using System.Collections.Generic;
namespace bai1495
{
    class ProductMenu
    {
        static void Main(string[] args)
        {
            List<Product> PrList = new List<Product>();

            int choose;
            int n;
            do
            {
                Menu();
                choose = Convert.ToInt32(Console.ReadLine());
                switch(choose)
                {
                    case 1:
                        Console.WriteLine("Nhap so hang hoa can them: ");
                        n = Convert.ToInt32(Console.ReadLine());
                        for(int i = 0; i < n; i++)
                        {
                            Product product = new Product();
                                
                            Console.WriteLine("Nhap thong tin san pham thu {0}", i + 1);
                            product.input();
                            PrList.Add(product);
                        }
                        break;
                    case 2:
                        Console.WriteLine("---Thong tin san pham---");
                        for(int i = 0; i < PrList.Count; i++)
                        {
                            Console.WriteLine("San pham thu {0}", i + 1);
                            PrList[i].display();
                        }
                        break;
                    case 3:
                        for (int i = 0; i < PrList.Count - 1; i++)
                        {
                            for(int j = i + 1; j < PrList.Count; j++)
                            {
                                if (PrList[i].Price < PrList[j].Price)
                                {
                                    Product tmp = PrList[i];
                                    PrList[i] = PrList[j];
                                    PrList[j] = tmp;
                                }
                            }
                           
                        }

                        Console.WriteLine("Thong tin san pham sau khi sap xep: ");
                        for (int i = 0; i < PrList.Count; i++)
                        {
                            PrList[i].display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat");
                        break;
                    default:
                        Console.WriteLine("Nhap lai");
                        break;
                }
            } while (choose != 4);

        }

        static void Menu()
        {
            Console.WriteLine("1. Nhap thong tin cho n san pham");
            Console.WriteLine("2. Hien thi thong tin");
            Console.WriteLine("3. Sap xep thong tin");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Moi chon");
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-21 03:26:04



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

namespace Lessson3.ProductManager
{
    class Product
    {
        public string Name { get; set; }
        public string Manufacture { get; set; }
        public float Price { get; set; }

        public Product()
        {

        }

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

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

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

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

        public void Display()
        {
            Console.WriteLine("Ten san pham: {0}, Nha san xuat: {1}, Gia ban: {2}", Name, Manufacture, Price);
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-21 03:25:52



using System;
using System.Collections.Generic;

namespace Lessson3.ProductManager
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> ProductList = new List<Product>();
            Console.WriteLine("Nhap so luong san pham muon them N = ");
            int N  = Int32.Parse(Console.ReadLine());

            for(int i = 0; i < N; i++)
            {
                Console.WriteLine("Nhap thong san pham {0} ",i+1);
                Product product = new Product();
                product.Input();
                ProductList.Add(product);
            }

            for (int i = 0; i < N - 1 ; i++)
            {
              for(int j = i + 1; j < N ; j++)
                {
                    if (ProductList[i].Price >= ProductList[j].Price) {
                        Product ProductSwap = ProductList[i];
                        ProductList[i] = ProductList[j];
                        ProductList[j] = ProductSwap;
                    }
                }
            }

            Console.WriteLine("San pham sau khi sap xep gia tu thap den cao nhu sau: ");

            for (int i = 0; i < N; i++)
            {
                ProductList[i].Display();
            }

        }
    }
}


avatar
vuong huu phu [T2008A]
2021-05-19 11:47:37



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

namespace Sanpham
{
    class Productcom : IComparer<Product>
    {
        int IComparer<Product>.Compare(Product x, Product y)
        {
            if (x.gia > y.gia)
            {
                return 1;
            }
            else {
                return 1;
            }
        }
    }
}



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

namespace Sanpham
{
    class Product
    {
        public String Name { set; get; }
        public String nsx { set; get; }
        public int gia { set; get; }

        public Product() { }
        public Product(String Name, String nsx, int gia)
        {
            this.Name = Name;
            this.nsx = nsx;
            this.gia = gia;
        }
        public void nhap()
        {
            Console.WriteLine("Ten");
            Name = Console.ReadLine();
            Console.WriteLine("Nha san xuat");
            nsx = Console.ReadLine();
            Console.WriteLine("Gia");
            gia = Int32.Parse(Console.ReadLine());
        }
        public void hienthi()
        {
            Console.Write("Ten : " + Name + ", Nha san xuat : " + nsx + ", Gia : " + gia);
        }

    }
    
}



using System;
using System.Collections.Generic;

namespace Sanpham
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> list1 = new List<Product>();
            int lc;
            do
            {
                menu();
                Console.WriteLine("Nhap lua chon");
                 lc = Int32.Parse(Console.ReadLine());
                switch (lc) {
                    case 1:
                        Console.WriteLine("Nhap so luong ");
                        int n = Int32.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++) {
                            Product p = new Product();
                            p.nhap();
                            list1.Add(p);
                        }
                        break;
                    case 2:
                        for (int i = 0; i < list1.Count; i++) {
                            list1[i].hienthi();
                        }
                        break;
                    case 3:
                        list1.Sort(new Productcom());
                        foreach (Product p in list1) {
                            p.hienthi();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Thoat!!!!");
                        break;
                }

            } while (lc < 5);
            
           
        }
        public static void menu() {
            Console.WriteLine(" 1 Nhap thong tin : ");
            Console.WriteLine(" 2 Hien thi");
            Console.WriteLine(" 3 Sap xep");
            Console.WriteLine(" 4 Thoat");
        }
    }
}


avatar
Nguyễn Anh Vũ [T2008A]
2021-05-19 10:22:47



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!");
                }

            }
        }
    }
}