c# 集合实现图书管理系统

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;
            }
        }
    }
    }
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小呆子lazy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值