使用c#,运用面向对象的思想构建的一个图书管理系统(无数据库)

0 人赞了该文章

实现截图如下图:

面向对象的思想,实现代码:

namespace s1025_面向对象_
{
    //使用面向对象的思想构建图书管理系统
    /// <summary>
    /// 功能:具有图书的增删改查功能;
    /// </summary>


    //构造图书类
    //重载
    public class Book
    {
        private string Name;
        private string Author;
        private int Price;
      public Book()
      {
            //Console.WriteLine("我是一个没有参数的构造方法");
      }
      public Book(string name, string author, int price)
      {
            //Console.WriteLine("我是一个有参数的构造方法");
            this.Name = name;
            this.Author = author;
            this.Price = price;
      }
      //对c#中属性行为的定义,如果是自动定义,则不能书写后备字段
        public string name
        {
            get { return Name; }
            set { Name = value; }

        }

        public string author
        {
            get { return Author; }
            set { Author = value; }
        }

        public int price
        {
            get { return Price; }
            set { Price = price; }
        }
        //属性构造结束
        //重写继承的方法,注意签名必须相同
        public override string ToString()
        {
            string str = Name+","+ Author+ "," +   Price.ToString();
            return  str;
        }
    }

    public class BookManager
    {

        //创建一个列表集合报讯所有的图书
        public List<Book> Books = new List<Book>();

        public void Init()
        {
            Console.WriteLine("===========图书管理系统=============");
            Console.WriteLine("请选择您需要进行的操作(输入序号即可):");
            Console.WriteLine("1.增加");
            Console.WriteLine("2.修改");
            Console.WriteLine("3.删除");
            Console.WriteLine("4.查看");
            Console.WriteLine("5.退出");
            int num = int.Parse(Console.ReadLine());
            switch (num)
            {
                case 1:
                    IncreaseBook();
                    Init();
                    break;
                case 2:
                    ChangeBook();
                    Init();
                    break;
                case 3:
                    DelBook();
                    Init();
                    break;
                case 4:
                    LookBook();
                    Init();
                    break;
                case 5:
                    Environment.Exit(0);
                    break;
                default:
                    Console.WriteLine("输入有误,自动退出");
                    break;
            }
        }

        public void IncreaseBook()
        {
            Console.WriteLine("============增加图书==============");
            Console.WriteLine("图书名称:");
            string  name=Console.ReadLine();
            Console.WriteLine("图书作者:");
            string author = Console.ReadLine();
            Console.WriteLine("图书的价格:");
            int price = int.Parse(Console.ReadLine());
            Book book = new Book(name,author,price);
            Books.Add(book);
            Console.WriteLine("录入成功!");

        }

        public void DelBook()
        {
            Console.WriteLine("==============删除图书================");

            Console.WriteLine("请输入图书的名字:");

            string book = Console.ReadLine();

            foreach (var item in  Books) 
            {
                if (item.name == book)
                {
                    Books.Remove(item);
                    return;
                }
            }

            Console.WriteLine("删除成功");
        }

        public void ChangeBook()
        {

            string name;
            string author;
            int price;
            Console.WriteLine("=============修改图书================");
            Console.WriteLine("请输入图书的名字:");
            string book1 = Console.ReadLine();
            try
            {
                foreach (Book item in Books)
                {
                    if (item.name == book1)
                    {

                        Console.WriteLine("按照以下序号输入您想修改的条目");
                        Console.WriteLine("1.名称");
                        Console.WriteLine("2.作者");
                        Console.WriteLine("3.价格");
                        int num = int.Parse(Console.ReadLine());
                        switch (num)
                        {
                            case 1:
                                Console.WriteLine("请输入您要修改的名称:");
                                name = Console.ReadLine();
                                item.name = name;
                                Console.WriteLine("修改成功");
                                break;
                            case 2:
                                Console.WriteLine("请输入您要修改的作者名字:");
                                author = Console.ReadLine();
                                item.author = author;
                                Console.WriteLine("修改成功");
                                break;
                            case 3:
                                Console.WriteLine("请输入价格:");
                                price = int.Parse(Console.ReadLine());
                                item.price = price;
                                Console.WriteLine("修改成功");
                                break;
                            default:
                                Console.WriteLine("您的输入有误");
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("查找成功,但是有点问题");
                    }
                }
            }
            catch
            {
                Console.WriteLine("未查询到");
            }


            }

        public void LookBook()
        {
            Console.WriteLine("=============图书信息============");
            foreach (var item in Books)
            {
                Console.WriteLine(item);
                Console.WriteLine();
            }
        }




    }

    class Program
    {
        static void Main(string[] args)
        {

            BookManager bookManager = new BookManager();
            bookManager.Init();

        }
    }
}

评论

暂无评论