C# 2
C# 2
using System.IO;
using System.Security.Cryptography;
using System.Text;
class BTTH5
{
struct HangHoa
{
public string TenHang;
public int MaHang, sl, DonGia;
}
static HangHoa[] hangHoa = new HangHoa[100];
static int slhh = 0;
static void Nhapsl()
{
int sl;
do
{
Console.WriteLine("Nhap so luong hang hoa: ");
sl = int.Parse(Console.ReadLine());
}
while (sl > hangHoa.Length - slhh);
for (int i = slhh; i < slhh + sl; i++)
{
do
{
Console.Write("Nhap ma hang hoa: ");
hangHoa[i].MaHang = int.Parse(Console.ReadLine());
Console.Write("Nhap ten hang hoa: ");
hangHoa[i].TenHang = Console.ReadLine();
Console.Write("Nhap don gia mat hang: ");
hangHoa[i].DonGia = int.Parse(Console.ReadLine());
if (hangHoa[i].DonGia < 500 || hangHoa[i].DonGia > 520000)
{
Console.WriteLine("Don gia khong hop le, moi nhap lai: ");
}
}
while (hangHoa[i].DonGia < 500 || hangHoa[i].DonGia > 520000);
}
slhh = slhh + sl;
}
static void Hienthi()
{
Console.WriteLine("~~~Danh sach hang hoa~~~");
Console.WriteLine("STT\tMa hang\tTen hang\t\tSo luong\tDon gia ban\n");
for (int i = 0; i <slhh; i++)
{
Console.WriteLine("{0}\t{1}\t{2}\t\t{3}\t{4}\n", i+1, hangHoa[i].MaHang,
hangHoa[i].TenHang, hangHoa[i].sl, hangHoa[i].DonGia);
}
}
static void Hangconlai()
{
Console.WriteLine("~~~Danh sach mat hang con~~~");
Console.WriteLine("Ten hang\t\tSo luong\tDon gia ban\n");
for (int i = 0; i < slhh; i++)
{
if (hangHoa[i].sl != 0)
Console.WriteLine("{0}\t\t{1}\t{2}\n", hangHoa[i].Tenhang, hangHoa[i].sl,
hangHoa[i].DonGia);
}
}
static bool KiemTraSapXep()
{
for (int i = 0; i < slhh - 1; i++)
{
if (hangHoa[i].DonGia > hangHoa[i + 1].DonGia)
return false;
}
return true;
}
static void Ghivaotep()
{
string filePath = "E:\hanghoa.txt";
try
{
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.WriteLine("STT\tMa hang\tTen hang\t\tSo luong\tDon gia ban\n");
for (int i = 0; i < slhh; i++)
{
sw.WriteLine($"{i + 1,-4}\t{hangHoa[i].MaHang,-10}\
t{hangHoa[i].TenHang,-18}\t\t{hangHoa[i].sl,-10}\t{hangHoa[i].DonGia}\n");
}
}
Console.WriteLine($"Du lieu đa đuoc ghi vao tep {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Đa xay ra loi khi ghi du lieu vao tep tin: {ex.Message}");
}
}
static void Menu()
{
Console.WriteLine("~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~");
Console.WriteLine("QUAN LY HANG HOA");
Console.WriteLine("~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~");
Console.WriteLine("(1) Nhap ");
Console.WriteLine("(2) Hien thi ");
Console.WriteLine("(3) Hien thi hang con ");
Console.WriteLine("(4) Kiem tra danh sach sap xep ");
Console.WriteLine("(5) Ket xuat ra tep ");
Console.WriteLine("(0) Thoat ");
Console.WriteLine("Chon chuc nang: ");
cn = int.Parse(Console.ReadLine());
switch(cn)
{
case 1: NhapHangHoa(); Menu(); break;
case 2: Hienthi(); Menu(); break;
case 3: HienThiHangCon(); Menu(); break;
case 4: KiemTraSapXep(); Menu(); break;
case 5: KetXuatRaTep(); Menu(); break;
case 0: Thoat(); Menu(); break;
defualt: Console.WriteLine("Chuc nang ko hop le\nVui long nhap lai!"); break;
}
}
}