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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-05-25 09:06:21
#Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLiSanPham
{
class Product
{
public string NameProduct { get; set; }
public string Producer { get; set; }
public int Price { get; set; }
public Product() { }
public Product(string nameProduct,string producer,int price) {
NameProduct = nameProduct;
Producer = producer;
Price = price;
}
public void display()
{
Console.WriteLine("NameProduct={0},Producer={1},Price={2}",NameProduct,Producer,Price);
}
public void input()
{
Console.WriteLine("nhap nameproduct");
NameProduct = Console.ReadLine();
Console.WriteLine("nhap producer");
Producer = Console.ReadLine();
Console.WriteLine("nhap price product");
Price = Convert.ToInt32(Console.ReadLine());
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLiSanPham
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<Product> list = new List<Product>();
int choose;
do
{
showmenu();
choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
inputProduct(list);
break;
case 2:
display(list);
break;
case 3:
list.Sort(new SortByPrice());
foreach (var item in list)
{
item.display();
}
break;
case 4:
Console.WriteLine("Thoat thanh cong");
break;
default:
Console.WriteLine("Chon tu 1-4, chon lai di");
break;
}
} while (choose != 4);
}
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. Thoát.");
Console.WriteLine("Choose:");
}
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.input();
list.Add(sp);
}
}
public static void display(List<Product> list)
{
Console.WriteLine("Danh sach cac san pham ");
foreach (var item in list)
{
item.display();
}
}
}
}
#SortByPrice.cs
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace QuanLiSanPham
{
class SortByPrice : IComparer<Product>
{
public int Compare( Product x,Product y)
{
return x.Price > y.Price ? -1 : 1;
}
}
}