Lập trình C# - Quản lý sản phẩm
Viết chương trình quản lý sản phẩm theo yêu cầu:
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:
- Nhập thông tin cho n sản phẩm
- Hiển thị thông tin vừa nhập
- Sắp xếp thông tin giảm dần theo giá và hiển thị
- Thoát.
Bước 1: Trong project chúng ta tạo 1 class Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ProductManager
{
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()
{
try
{
Console.WriteLine("Nhập tên sản phẩm:");
Name = Console.ReadLine();
Console.WriteLine("Nhập nhà sản xuất:");
Producer = Console.ReadLine();
Console.WriteLine("Nhập giá bán:");
Price = int.Parse(Console.ReadLine());
}
catch(Exception ex)
{
Console.WriteLine("Giá bán là số");
}
}
public void Output()
{
Console.Write("{0,-15}\t\t|{1,-10}\t\t| {2,-5}\t |", Name, Producer, Price);
}
}
}
Bước 2: Mã nguồn chương trình trong Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ProductManager
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<Product> ListProduct = new List<Product>();
int choice = 0;
do
{
Menu();
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Nhập số lượng sản phẩm");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.WriteLine("Nhập vào thông tin sản phẩm thứ:" + (i + 1));
Product nProduct = new Product();
nProduct.Input();
ListProduct.Add(nProduct);
}
break;
case 2:
Console.Clear();
Console.WriteLine("Danh sách sản phẩm ");
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine("Tên sản phẩm\t\t| Nhà sản xuất\t\t|Giá bán| ");
Console.WriteLine("-------------------------------------------------------------------");
foreach (Product prd in ListProduct)
{
prd.Output();
Console.WriteLine();
}
Console.WriteLine("-------------------------------------------------------------------");
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;
}
}
}
Console.WriteLine("Danh sách sản phẩm giảm dần theo giá");
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine("Tên sản phẩm\t\t| Nhà sản xuất\t\t|Giá bán| ");
Console.WriteLine("-------------------------------------------------------------------");
foreach (Product prd in ListProduct)
{
prd.Output();
Console.WriteLine();
}
Console.WriteLine("-------------------------------------------------------------------");
break;
case 4:
Console.WriteLine("Tạm biệt...");
return;
default:
Console.WriteLine("Nhập sai thông tin");
break;
}
} while (choice != 4);
}
private static void Menu()
{
Console.WriteLine("\nCHƯƠNG TRÌNH QUẢN LÝ SẢN PHẨM C#");
Console.WriteLine("*****************************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 sếp thông tin giảm dần theo giá và hiển thị **");
Console.WriteLine("** 4: Thoát: **");
Console.WriteLine("** Chọn: **");
Console.WriteLine("***************************************************************");
}
}
}
Bước 3: Nhấn Ctrl+F5 để chạy và xem kết quả
Chọn 1 nhập thông tin sản phẩm
Chọn 2 xem thông tin sản phẩm