Trang chủ » Diễn Đàn » Lập trình và Phát triển Web » Các trao đổi khác về Lập trình » lap trinh huong doi tuong
Chủ đề đã bị khóa, bạn không thể xóa, sửa hay trả lời trong chủ đề này!
|
|
|---|
|
0
//t dang phai viet 1 chuong trinh quan ly cua hang ban thuoc,co the giup t viet lop thuốc nước,thuốc bột,thuốc viên //kế thừa thuộc tính của lớp thuốc được ko
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;//thu vien doc ghi file namespace doannhom10 { class qlthuoc { public string mathuoc; public string tenthuoc; public string nhomthuoc; public DateTime nsx; public DateTime hsd; public DateTime ngaynhap; public DateTime ngayban; public int soluongnhap; public int soluongban; public double giamua; public double giaban; public double tienlai; public double tienlo; public int solo; public string nhasx; public string nhacungcap; public qlthuoc Link; class thuoc { static qlthuoc L; static string filename = "data.txt"; static void NhapTubanPhim() { ConsoleKeyInfo kt; do { Console.WriteLine("\n\n"); Console.WriteLine(" NHAP THONG TIN THUOC"); Console.WriteLine(" "); qlthuoc moi = new qlthuoc(); mathuoc: Console.Write("Nhap ma thuoc : "); moi.mathuoc = Console.ReadLine(); StreamReader doc = File.OpenText("data.txt"); string xau = doc.ReadLine(); while (xau != null) { string[] mang = xau.Split('#'); if (moi.mathuoc == mang[0]) { Console.Write("Ma Thuoc Da Ton Tai\n"); goto mathuoc; } xau = doc.ReadLine(); } doc.Close(); Console.Write("Nhap ten thuoc: "); moi.tenthuoc = Console.ReadLine(); Console.Write("Nhap nhom thuoc: "); moi.nhomthuoc = Console.ReadLine(); Console.Write("Nhap ngay san xuat : "); moi.nsx = DateTime.Parse(Console.ReadLine()); Console.Write("Nhap han su dung : "); moi.hsd = DateTime.Parse(Console.ReadLine()); Console.Write("Nhap gia mua thuoc : "); moi.giamua = double.Parse(Console.ReadLine()); Console.Write("Nhap gia ban thuoc: "); moi.giaban = double.Parse(Console.ReadLine()); Console.Write("Nhap so luong nhap thuoc : "); moi.soluongnhap = int.Parse(Console.ReadLine()); Console.Write("Nhap so luong thuoc da ban : "); moi.soluongban = int.Parse(Console.ReadLine()); Console.Write("Nhap tien lai : "); moi.tienlai = double.Parse(Console.ReadLine()); Console.Write("Nhap tien bi lo : "); moi.tienlo = double.Parse(Console.ReadLine()); Console.Write("Nhap so lo : "); moi.solo = int.Parse(Console.ReadLine()); Console.Write("Nhap nha san xuat thuoc : "); moi.nhasx = Console.ReadLine(); Console.Write("Nhap so luong bai hat/ten phim : "); moi.nhacungcap = Console.ReadLine(); Console.Write("Nhap ngay nhap dia : "); moi.ngaynhap = DateTime.Parse(Console.ReadLine()); Console.Write("Nhap ngay ban dia : "); moi.ngayban = DateTime.Parse(Console.ReadLine()); moi.Link = null; if (L == null) { L = moi; } else { qlthuoc P = L; while (P.Link != null) { P = P.Link; } P.Link = moi; } Console.Write("Ban co nhap them thuoc nua khong?(c/k): "); kt = Console.ReadKey(); Console.WriteLine(); } while (kt.KeyChar.Equals('y')); } static void NhapTuFile() { //kiem tra su ton tai cua file du lieu if (File.Exists(filename)) { //Nếu file này tồn tại tức file này có dữ liệu cần phải load nên chương trình StreamReader docFile = new StreamReader(filename); while (docFile.EndOfStream == false) { string motDong = docFile.ReadLine(); //cat thong tin string[] cut = motDong.Split('#'); string mathuoc = cut[0]; string tenthuoc = cut[1]; string nhomthuoc = cut[2]; string nhasx = cut[3]; string nhacungcap = cut[4]; qlthuoc ql = new qlthuoc(); ql.mathuoc = mathuoc; ql.tenthuoc = tenthuoc; ql.nhomthuoc = nhomthuoc; ql.nhasx = nhasx; ql.nhacungcap = nhacungcap; //tao con tro dau danh sach ql.Link = null; if (L == null) { L = ql; } else { qlthuoc P = L; while (P.Link != null) { P = P.Link; } P.Link = ql; } } //dóng luong doc file docFile.Close(); } } static void Sua(string oldMa, String newtenthuoc) { //Tìm ra dia này qlthuoc P = TimKiemTheoMa(oldMa); if (P != null) { P.tenthuoc = newtenthuoc; //ok dã sua xong } //ngược lại ko có dia mã như thế này, tuỳ bạn hiển thị thông báo tới người dùng else Console.WriteLine("khong tim thay thuoc co ma nhu ban vua nhap"); } static void Xoa(string mathuoc) { //Tìm ra dia này qlthuoc P = L; if (P.mathuoc == mathuoc) L = null; else { qlthuoc tmp = null; while (P != null && P.mathuoc != mathuoc) { tmp = P; P = P.Link; } if (P != null) { //xoá chỉ cần 1 lệnh sau tmp.Link = P.Link; Console.WriteLine("Da xoa thanh cong"); } } } static qlthuoc TimKiemTheoMa(string mathuoc) { //Tìm ra thuoc này qlthuoc P = L; while (P != null && P.mathuoc != mathuoc) { P = P.Link; } return P; } static qlthuoc TimKiemTheoTen(string ten) {//tim kiem theo gan dung //Tên có thể trùng nhau vì vậy trường hợp này nó sẽ trả ra 1 danh sách mới //Tạo con trỏ mới của dslk mới qlthuoc LNew = null; //duyệt danh sách trước L để tìm người có ten qlthuoc P = L; int i = 0; while (P != null) { if (P.tenthuoc == ten) { i = i + 1; Console.WriteLine("chay lan", i); qlthuoc tmp = new qlthuoc(); tmp.mathuoc = P.mathuoc; tmp.tenthuoc = P.tenthuoc; tmp.nhomthuoc = P.nhomthuoc; tmp.giaban = P.giaban; tmp.ngayban = P.ngayban; tmp.hsd = P.hsd; tmp.Link = null; if (LNew == null) { LNew = tmp; } else if (LNew.Link == null) // Trường hợp đã có 1 kết quả { Console.WriteLine("Da co 1 thuoc"); LNew.Link = tmp; } else // Đã có nhiều hơn 1 kết quả { qlthuoc duyet = LNew; while (duyet.Link != null) { duyet = duyet.Link; } duyet.Link = tmp; } } P = P.Link; } return LNew; } static void LuuRaFile() { //Luu toan bo danh sach ra file StreamWriter ghiFile = new StreamWriter(filename, false); qlthuoc P = L; while (P != null) { string motDong = P.mathuoc + "#" + P.tenthuoc + "#" + P.nhomthuoc + "#" + P.nsx + "#" + P.hsd + "#" + P.ngaynhap + "#" + P.ngayban + "#" + P.soluongnhap + "#" + P.soluongban + "#" + P.giamua + "+" + P.giaban + "+" + P.tienlai + "+" + P.tienlo + "+" + P.solo + "+" + P.nhasx + "+" + P.nhacungcap; ghiFile.WriteLine(motDong); P = P.Link; } //dóng lu?ng ghi file ghiFile.Close(); } static void HienThiAll(qlthuoc ctr) { //hien thi toan bo nhung dia trong danh sach qlthuoc P = ctr; Console.WriteLine("Mathuoc\tTenthuoc\tNhomthuoc\tGiaban\tHsd\tNgayban"); if (P == null) { Console.WriteLine("Xin loi ! KHONG CO DU LIEU"); } while (P != null) { Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t\t{5}", P.mathuoc, P.tenthuoc, P.nhomthuoc, P.giaban, P.hsd, P.ngayban); P = P.Link; } Console.ReadKey(); } static void HienThi(qlthuoc ql) {//Hien thi thong tin ve dia nay qlthuoc P = ql; if (P != null) { Console.WriteLine("Mathuoc\tTenthuoc\tNhomthuoc\tGiaban\tHsd\tngayban"); Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t\t{5}", P.mathuoc, P.tenthuoc, P.nhomthuoc, P.giaban, P.hsd, P.ngayban); P = P.Link; } else { Console.WriteLine("Xin loi ! KHONG CO DU LIEU"); } Console.ReadKey(); } static void Main(string[] args) {// lua chon mau cho nen va chu NhapTuFile(); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Magenta; Console.Title = "QUAN LI BANG THUOC"; Console.Clear(); //Xay dung menu cho chuong trinh quan li do { Console.Write("\n\n\t\t * CHUONG TRINH QUAN LY CUA HANG BAN THUOC * \n\n"); Console.WriteLine("\n\n\t\t ________*****__MUC LUC CONG VIEC__*****________\n"); Console.Write("\n | F1. Nhap moi thuoc | F4 .Tim thuoc theo ma |"); Console.Write("\n | | |"); Console.Write("\n | F2.Sua thong tin 1 thuoc | F5. Tim thuoc theo ten |"); Console.Write("\n | | |"); Console.Write("\n | F3.Xoa 1 thuoc | F6.Hien thi danh sach thuoc |"); Console.Write("\n | | |"); Console.Write("\n | Esc.Thoat khoi chuong trinh | |\n"); Console.Write("\n ___________________________________________________________ \n "); Console.Write("\n ********************* Chon Phim Chuc Nang*******************"); ConsoleKeyInfo phim; den: string mathuoc, tenthuoc; phim = Console.ReadKey(); switch (phim.Key) { case ConsoleKey.F1: Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; NhapTubanPhim(); LuuRaFile(); break; case ConsoleKey.F2: Console.Clear(); Console.WriteLine("\n\n"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(" SUA THONG TIN CUA 1 THUOC"); Console.WriteLine(" ..........."); Console.Write("Ma cua thuoc can sua: "); mathuoc = Console.ReadLine(); Console.Write("Ten moi: "); tenthuoc = Console.ReadLine(); Sua(mathuoc, tenthuoc); LuuRaFile(); break; case ConsoleKey.F3: Console.Clear(); Console.WriteLine("\n\n"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(" XOA THONG TIN 1 THUOC"); Console.WriteLine(" ..........."); Console.Write("Ma cua thuoc can xoa: "); mathuoc = Console.ReadLine(); Xoa(mathuoc); LuuRaFile(); break; case ConsoleKey.F4: Console.Clear(); Console.WriteLine("\n\n"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(" TIM KIEM THUOC THEO MA"); Console.WriteLine(" ..........."); Console.WriteLine("Tim kiem thuoc theo ma:"); Console.Write("Ma cua thuoc can tim: "); mathuoc = Console.ReadLine(); qlthuoc tim = TimKiemTheoMa(mathuoc); HienThi(tim); break; case ConsoleKey.F5: Console.Clear(); Console.WriteLine("\n\n"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" TIM KIEM THUOC THEO TEN"); Console.WriteLine(" ..........."); Console.WriteLine("Tim kiem thuoc theo ten:"); Console.Write("Ten cua thuoc can tim: "); tenthuoc = Console.ReadLine(); qlthuoc qlAll = TimKiemTheoTen(tenthuoc); HienThi(qlAll); break; case ConsoleKey.F6: Console.Clear(); Console.WriteLine("\n\n"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(" DANH SACH THUOC CO TRONG CUA HANG"); Console.WriteLine(" ..........."); HienThiAll(L); // hoadon(); break; case ConsoleKey.Escape: Console.Clear(); Console.WriteLine("\n\n\n\n\n\n\n Xin chao ! Hen gap lai "); Environment.Exit(0); break; default: Console.WriteLine("\n ==> Ban da nhap khong dung xin hay nhap lai theo dung yeu cau!<=="); goto den; } Console.Clear(); } while (true); } } } |
