//hàm thêm sách vào file public void Themsach(SACH sach) { sach.Nhap(); try { StreamWriter file = new StreamWriter("C:\\Users\\Admin\\source\\repos\\BaiTapLon\\BaiTapLon\\DSSach.txt", true); file.Write(sach.toString() + "\r\n"); file.Close(); Console.WriteLine("Luu thanh cong!\n"); } catch (Exception e) { Console.WriteLine("khong the ghi file\n"); Console.WriteLine(e.Message); } }
//hàm mở và đọc file và tăch chuỗi public void AddFromFile() { try { using (StreamReader sr = new StreamReader("C:\\Users\\Admin\\source\\repos\\BaiTapLon\\BaiTapLon\\DSSach.txt")) while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] arr = line.Split(','); string tensach = arr[0]; string tacgia = arr[1]; int masach = Int32.Parse(arr[2]); int namsx = Int32.Parse(arr[3]); double gia = Double.Parse(arr[4]); SACH sach = new SACH(tensach, tacgia, masach, namsx, gia); listSach.Add(sach); } } catch (Exception e) { Console.WriteLine("khong the doc file\n"); Console.WriteLine(e.Message); } }
static void Main(string[] args) { Menu m = new Menu("Labrary"); // tạo menu DSSach ds = new DSSach(); //tạo danh sách ds.AddFromFile(); // đọc file Home: //tạo vòng lặp quay lại Menu m.Display(); switch (m.GetChoice()) { case 1: { SACH.PrintHeader(); ds.Xuat(); ds.PrintFooter(); } break; case 2: { Console.WriteLine("\n\tDanh Sach Theo Ten Sach"); SACH.PrintHeader(); ds.Namesort(); ds.Xuat(); ds.PrintFooter(); } break; case 3: { Console.WriteLine("\n\tDanh Sach Theo Nam Xuat Ban"); SACH.PrintHeader(); ds.Publishingyear(); ds.Xuat(); ds.PrintFooter(); } break; case 4: { ds.Themsach(new SACH()); Console.ReadLine(); } goto Home; case 5: { Console.WriteLine("\n\t-- -- Xoa -- -- "); Console.Write("\tMa sach : "); int i = Convert.ToInt32(Console.ReadLine()); ds.Xoa(i); } goto Home; case 6: { ds.Find(); } break; case 7: { Menu.SayBye(); } break; default: { Console.WriteLine("khong co gia tri"); } break; } Console.ReadKey(); }