C# 面向对象例题 -- 图书管理系统
【摘要】 图书管理系统
请利用面向对象分析实现图片管理系统,具有录入,查询 ,删除,显示所有图书信息功能。
class Program { static void Main(string[] args) { BookManager bm = new BookManager(); bm.Init(); } } class Book { /// <su...
图书管理系统
请利用面向对象分析实现图片管理系统,具有录入,查询 ,删除,显示所有图书信息功能。
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
BookManager bm = new BookManager();
-
bm.Init();
-
}
-
}
-
-
-
class Book
-
{
-
/// <summary>
-
/// 当一个对象被创建时,必须执行Book(构造函数)
-
/// </summary>
-
public Book()
-
{
-
-
}
-
public Book(string name, string author, int price)
-
{
-
-
this._name = name;
-
this._author = author;
-
this._price = price;
-
}
-
-
-
private string _name;
-
public string name { get { return _name; } }
-
-
-
private string _author;
-
public string Author { get { return _author; } }
-
-
private int _price;
-
public int Price { get { return _price; } }
-
-
public override string ToString()
-
{
-
return "<<" + name + ">> " + _author + " " + Price + "元";
-
}
-
}
-
-
-
-
class BookManager
-
{
-
public List<Book> list = new List<Book>();
-
public void Init()
-
{
-
///Console.Clear();
-
Console.WriteLine("=====图书管理系统====");
-
Console.WriteLine("1. 录入");
-
Console.WriteLine("2. 查询");
-
Console.WriteLine("3. 删除");
-
Console.WriteLine("4. 显示所有图书");
-
Console.WriteLine("5. 退出");
-
Console.WriteLine("=====================");
-
-
Console.WriteLine("");
-
Console.Write("请输入功能序号:");
-
-
int action = int.Parse(Console.ReadLine());
-
-
switch (action)
-
{
-
case 1:
-
-
Enter();
-
Init();
-
break;
-
case 2:
-
Find();
-
Init();
-
break;
-
case 3:
-
Dele();
-
Init();
-
break;
-
case 4:
-
ShowAll();
-
Init();
-
break;
-
case 5:
-
Environment.Exit(0);
-
break;
-
}
-
}
-
-
-
public void Enter()
-
{
-
Console.Write("请输入书名:");
-
string bookname = Console.ReadLine();
-
-
Console.Write("请输入作者:");
-
string bookauthor = Console.ReadLine();
-
-
Console.Write("请输入价格:");
-
int bookprice = int.Parse(Console.ReadLine());
-
-
//利用自己定义的构造函数创建Book对象
-
Book book = new Book(bookname, bookauthor, bookprice);
-
-
Console.WriteLine("书名:" + book.name);
-
-
list.Add(book);
-
-
Console.WriteLine("图书录入成功!");
-
}
-
-
public void Find()
-
{
-
-
Console.Write("请输入要查询的书名:");
-
string name = Console.ReadLine();
-
-
Console.WriteLine("======查询结果如下======");
-
foreach (Book book in list)
-
{
-
if (book.name == name)
-
{
-
Console.WriteLine(book);
-
}
-
Console.WriteLine();
-
}
-
Console.WriteLine();
-
}
-
-
-
public void Dele()
-
{
-
Console.Write("请输入要删除的书名:");
-
string name = Console.ReadLine();
-
-
foreach (Book book in list)
-
{
-
if (book.name == name)
-
{
-
list.Remove(book);
-
return;
-
}
-
}
-
}
-
-
-
public void ShowAll()
-
{
-
foreach (Book book in list)
-
{
-
Console.WriteLine(book);
-
Console.WriteLine();
-
}
-
}
-
-
}
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/77890415
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)