首页 > 代码库 > 图书借阅的小案例
图书借阅的小案例
这个小案例,主要练习类和方法的创建与调用
void Main(){ while (true) { Console.WriteLine ("请输入标题或作者"); var key = Console.ReadLine().Trim(); Library.Search(key).Dump(); Console.WriteLine ("请输入要借阅/归还的图书编号"); var id = Convert.ToInt32(Console.ReadLine()); var book = Library.Search(id); Library.ChangeStatus(book); Console.WriteLine ("是否继续?Y/N"); if (Console.ReadLine().ToLower()!="Y".ToLower()) { break; } }}class Library{ private static readonly List<Book> Books = new List<Book>{ new Book(1,"红楼梦","曹雪芹",Status.已归还), new Book(2,"西游记","吴承恩",Status.已归还), new Book(3,"水浒传","施耐庵",Status.已归还), new Book(4,"三国演义","罗贯中",Status.已归还) }; public static IEnumerable<Book> Search(string key){ foreach (var book in Books) { if (book.Title.Contains(key) || book.Author.Contains(key)) { yield return book; } } } public static Book Search(int id){ foreach (var book in Books) { if (book.Id==id) { return book; } } return null; } public static void ChangeStatus(Book book){ if (book!=null) { if (book.Status==Status.已归还) { Console.WriteLine ("图书借阅成功"); book.Status=Status.已借阅; }else { Console.WriteLine ("图书归还成功"); book.Status=Status.已归还; } } }}class Book{ public int Id; public string Title; public string Author; public Status Status; public Book(int id,string title,string author,Status status){ Id=id; Title = title; Author = author; Status = status; } public string Info(){ return string.Format("Id:{0}\tTitle:{1}\tAuthor:{2}\tStatus:{3}",Title,Author,Status); }}enum Status{ 已借阅,已归还}
图书借阅的小案例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。