static void Main(string[] args) { BookStore bookstore = new BookStore(); bookstore.menu(); }
public void menu() { BookStore store = new BookStore(); int choose; //Chọn 1 số trong menu String ans; //Chọn Yes No khi Continue bool isNum; //Boolean kiểm tra nhập vào phải là số String temp; //Biến phụ để lấy string so sánh với pattern regex String numberPattern = @"^\d+$"; //Pattern của regex, phải là số 0-9, dấu @ để giữ giá trị nguyên gốc, ko cần backslash (verbatim string) Regex regex = new Regex(numberPattern); //Gắn pattern vào regex do { //Menu hiển thị Console.WriteLine("---------------------------"); Console.WriteLine(" 1. Adding new book."); Console.WriteLine(" 2. Displaying all the details of books."); Console.WriteLine(" 3. Search book by the title"); Console.WriteLine(" 4. Exit"); Console.WriteLine("---------------------------"); do { //Nhập số, dữ liệu nhập vào dc gán vào temp, kiểm tra theo regex, trả về boolean //Nếu đúng thì chạy switch case, sai thì báo nhập lại Console.Write("Please choose an option: "); temp = Console.ReadLine(); isNum = regex.IsMatch(temp); if (isNum == true) { choose = int.Parse(temp); switch (choose) { case 1: store.addBook(); break; case 2: store.displayBook(); break; case 3: store.searchBook(); break; case 4: Console.WriteLine("Exit..."); Environment.Exit(0); break; default: Console.WriteLine("Error, your choice is not correct !"); break; } } else { Console.WriteLine("Please enter a number!"); } } while (isNum == false); //Chạy switch case xong thì hỏi Continue, Nếu Y thì lặp lại, ngược lại thì exit Console.Write("Continue (Y/N) ? "); ans = Console.ReadLine(); } while (String.Equals(ans, "y", StringComparison.OrdinalIgnoreCase) == true); }