C# 面向对象 创建一个图书管理系统

编程入门 行业动态 更新时间:2024-10-10 00:20:56

C# 面向对象 <a href=https://www.elefans.com/category/jswz/34/1771345.html style=创建一个图书管理系统"/>

C# 面向对象 创建一个图书管理系统

这个系统需要两个类class Book,class BookManager
首先先进行对Book类成员的声明。

class Book{//数据string id;public string Id{get { return id; }set { id = value;}}//使用了lamda表达式, 跟上面的get/set等价public string Name { get => name; set => name = value; }string name;string author;float price;int page;public int Page { get; set; }public void print(){string str = string.Format("书名是{0},作者是{1},编号是{2},价格{3},页数{4}",name,author,id,price,page);Console.WriteLine(str);}}

BookManager类的声明

//数据成员int size;//容器的大小public int curIndex;//当前存储的位置Book[] books; //定义一个Book类型的数组//函数成员public BookManager(int size){curIndex = 0;this.size = size;//申请了一个容器,目前里面还没有值books = new Book[size];}

实现添加的方法
1、curIndex一开始的位置是[0],将b的内存地址存入数组中的第0位后,curIndex+1,这样的话下一次存储时会将数据存储到第1位。
2、判断数组的游标是否大于容器大小, 大于则要进行数组扩容

public void addBook(Book b)//传入Book的一个引用 b{books[curIndex] = b;//将b存入数组 curIndex++;if (curIndex>=size)//判断数组的游标是否大于容器大小, 大于则要进行数组扩容{size = size + size / 2;//将容器的大小扩容增加原来的一半Book[] booksNew = new Book[size];//定义一个新的Book类型数组 booksNewArray.Copy(books,booksNew, books.Length);//使用Copy函数将books数组里面的值赋值给booksNew,长度为books的长度 books = booksNew;}}

重写删除的文本,通过ID遍历

public bool delBook(string id){int findCurIndex = 0;bool isFind = false;for (int i = 0; i < curIndex; i++)//通过遍历比较两个id{if (books[i].Id.Equals(id)){findCurIndex = i;isFind = true;break;}}return false;}

查找书籍方法

public Book find(string id){if (string.IsNullOrEmpty(id)){return null;//返回空引用}for (int i = 0; i < curIndex; i++)//通过遍历比较两个id{if (books[i]!=null&&books[i].Id.Equals(id)){return books[i];}}return null;}

显示所有书籍方法

 public void showAllBook(){Console.WriteLine("所有的书籍信息如下");for (int i = 0; i < curIndex; i++){books[i].print();}}
以上,书和管理器的类就写完了 接下来在主函数中运行测试:
using System;namespace 图书管理系统
{class Program{static void Main(string[] args){Console.WriteLine("欢迎进入图书管理软件");int num = 1;BookManager manager = new BookManager(2);//容器(数据库)while (true){Console.Clear();//清理控制台信息Console.WriteLine("1、录入书籍\n2、查找书籍\n3、删除书籍\n4、显示所有书籍\n按对应的数字进入该功能,按Q退出");string str = Console.ReadLine();num = int.Parse(str);switch (num){case 1://录入书籍Console.WriteLine("已经进入录入书籍功能");Console.Write("输入编号:");string id = Console.ReadLine();Console.Write("输入书名:");string name = Console.ReadLine();Console.Write("输入作者名字:");string authName = Console.ReadLine();Console.Write("输入书的价格:");float price = float.Parse(Console.ReadLine());Console.Write("输入书的页数:");int page = int.Parse(Console.ReadLine());Book book = new Book(authName, price);//通过构造函数生成对象,并且赋值//通过属性对对象赋值book.Id = id;book.Name = name;book.Page = page;//把书存储到管理器中manager.addBook(book);break;case 2://查找书籍if (manager.curIndex == 0){Console.Write("系统里一本书都没有!");break;}id = Console.ReadLine();Book curBook = manager.find(id);if (curBook != null){Console.WriteLine("找到了书,信息如下:");curBook.print();}else{Console.WriteLine("没有找到书");}break;case 3://删除书籍Console.Write("输入要删除的书籍编号:");id = Console.ReadLine();Book _curBook = manager.find(id);if (_curBook != null){Console.WriteLine("已删除{0}这本书", _curBook.Name);manager.delBook(_curBook.Id);}else{Console.WriteLine("没有找到书");}break;case 4://显示所有书籍if (manager.curIndex != 0){manager.showAllBook();}else{Console.WriteLine("系统里一本书都没有!");}break;}if (str.ToLower().Contains('q')){break;}Console.ReadKey();}}}
}

更多推荐

C# 面向对象 创建一个图书管理系统

本文发布于:2024-02-27 19:00:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   面向对象   图书管理系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!