using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson集合_
{
//图书管理系统 主要是增删改查
class BookInfo
{
public List<Dictionary<string,string>> _book;
public BookInfo()
{
_book = new List<Dictionary<string,string>>();
}
public static BookInfo _instance;
public static BookInfo Instance
{
get
{
if (_instance == null)
{
_instance = new BookInfo();
}
return _instance;
}
}
//添加
public void Add()
{
Dictionary<string, string> _b = new Dictionary<string,string>();
Console.WriteLine("请输入书名");
_b["书名"] = Console.ReadLine();
Console.WriteLine("请输入作者");
_b["作者"] = Console.ReadLine();
Console.WriteLine("请输入价格");
_b["价格"] = Console.ReadLine();
Console.WriteLine("请输入出版社");
_b["出版社"] = Console.ReadLine();
_book.Add(_b);
}
//判断是否有这本书
public bool HasBook(string _bookName,out int _bookNumber)
{
for (int i = 0; i < _book.Count; i++)
{
//判断数组里边是否包含输入书的名字,包含了就返回出去
if (_book[i]["书名"] == _bookName)
{
_bookNumber = i ;
return true;
}
}
_bookNumber = - 1;
return false;
}
//查询
public void Check()
{
Console.WriteLine("请输入书名");
string _bookName = Console.ReadLine();
int _number ;
bool _index = HasBook(_bookName,out _number);
if (_index)
{
for (int i = 0; i < _book.Count; i++)
{
if (_book[i]["书名"] == _bookName)
{
foreach (KeyValuePair<string ,string> item in _book[i])
{
Console.WriteLine(item.Key +":"+ item.Value);
}
}
}
Console.WriteLine();
}
else
{
Console.WriteLine("查无此书");
}
}
//修改一下资料
public void Change()
{
Console.WriteLine("请输入要修改书的名字");
string _bookName = Console.ReadLine();
int _number;
bool _index = HasBook(_bookName,out _number);
if (_index)
{
Console.WriteLine("请输入需要修改的书的名字");
_book[_number]["书名"] = Console.ReadLine();
Console.WriteLine("请输入需要修改的价格");
_book[_number]["价格"] = Console.ReadLine();
Console.WriteLine("请输入修改后的出版社");
_book[_number]["出版社"] = Console.ReadLine();
Console.WriteLine("输入修改后作者的名字");
_book[_number]["作者"] = Console.ReadLine();
}
else
{
Console.WriteLine("查无此书");
}
}
//删除需要删除的书籍
public void Delet()
{
Console.WriteLine("请输入需要删除书的名字");
string _bookName = Console.ReadLine();
int _number;
bool _index = HasBook(_bookName,out _number);
if (_index)
{
_book.RemoveAt(_number);
}
else
{
Console.WriteLine("查无此书");
}
}
//打印
public void Print()
{
foreach (Dictionary<string,string> item in _book)
{
foreach ( KeyValuePair<string,string> it in item)
{
Console.WriteLine(item.Keys + ":" +item.Values);
}
}
Console.WriteLine("");
}
}
class Program
{
static void Main(string[] args)
{
#region 投票系统
//Dictionary<string, int> _dic = new Dictionary<string, int>();
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine("第{0}次输入名字",i +1);
// string _name = Console.ReadLine();
// if (_dic.ContainsKey(_name))
// {
// _dic[_name] ++ ;
// }
// else
// {
// _dic[_name] = 1;
// }
//}
//foreach (KeyValuePair<string,int> item in _dic)
//{
// Console.WriteLine("名字:{0},票数{1}",item.Key,item.Value);
//}
#endregion
while (true) {
Console.WriteLine("*******图书管理系统*******");
Console.WriteLine("请输入: 1 增加,2 查询,3 修改,4 删除,5 打印,0 返回");
string _nubmer = Console.ReadLine();
switch (_nubmer)
{
case "1":
BookInfo.Instance.Add();
break;
case "2":
BookInfo.Instance.Check();
break;
case "3":
BookInfo.Instance.Change();
break;
case "4":
BookInfo.Instance.Delet();
break;
case "5":
BookInfo.Instance.Print();
break;
case "0":
return;
default:
break;
}
}
}
}
}