图书管理系统的需求:
用结构体来实现图书管理系统,需要在cmd中运行
用MonoDevelop或者VS进行编码
用结构体写图书管理系统,此代码可以在Monodevelop上或者在VS上运行
需要实现的第一个功能是能够进行图书的添加将图书的信息保存到一个数组中,数组的类型为结构体所命名的类型
第二个功能能够进行图书的删除
第三个功能能够进行图书的修改
第四个功能能够进行图书的查询
第五个功能能够进行图书系统的退出
以下是两种不一样的编程思想(一一遍吗列举了出来)
第一种:
using System; using System.Collections.Generic; namespace Book_Manage { class MainClass { struct Book { //公司变量名前面加一个m_ /// <summary> /// 书的名字 /// </summary> public string m_BookName; /// <summary> /// 书的作者 /// </summary> public string m_BookAuthor; /// <summary> /// 书的价格 /// </summary> public float m_Price; } public static void Main(string[] args) { Book book1 = new Book (); book1.m_BookName="平凡的世界"; book1.m_BookAuthor = "路遥"; book1.m_Price = 10; Book book2; book2.m_BookName="射雕英雄传"; book2.m_BookAuthor="金庸"; book2.m_Price=23.5f; List<Book> m_Books = new List<Book> (); m_Books.Add(book1); m_Books.Add(book2); while (true) { Console.WriteLine ("**************************************************"); Console.WriteLine ("* 欢迎进入图书管理系统 *"); Console.WriteLine ("*A:添加 D:删除 C:修改 S:查看 Q:退出 P:全部显示 *"); Console.WriteLine ("**************************************************"); Console.Write ("请输入你要进行的选择(Q表示退出!):"); string input = Console.ReadLine (); //把input字符串转换成大写 然后再赋值给input input=input.ToUpper(); switch (input) { //添加 case "A": { //往数组里面加一本书 Book book; Console.Write ("请输入要添加的图书的名字:"); string name = Console.ReadLine (); Console.Write ("前输入要添加的图书的作者:"); string author = Console.ReadLine (); Console.Write ("请输入要添加的图书的价格:"); float price = float.Parse (Console.ReadLine ()); book.m_BookName = name; book.m_BookAuthor = author; book.m_Price = price; m_Books.Add (book); //进行其中的显示的清除 Console.Clear (); } break; //删除 case "D": { Console.WriteLine ("E:按名称删除 R:按作者删除"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); switch (str) { case "E": Book book = new Book (); //判断是否找到此书 bool isFind = false; Console.Write ("请输入您要进行删除的图书的名字:"); string strName = Console.ReadLine (); for (int i = 0; i < m_Books.Count; i++) { if (m_Books[i].m_BookName==strName) { isFind = true; book.m_BookAuthor = m_Books [i].m_BookAuthor; book.m_BookName = m_Books [i].m_BookName; book.m_Price = m_Books [i].m_Price; } } if (isFind) { m_Books.Remove (book); Console.WriteLine ("删除成功!!!!!"); } else { Console.WriteLine ("删除失败!!!!!"); } break; case "R": //判断是否找到此书 bool isFind1 = false; Book book3 = new Book (); Console.Write ("请输入您要进行删除的图书的作者:"); string strAuthor = Console.ReadLine (); for (int i = 0; i < m_Books.Count; i++) { if (m_Books[i].m_BookAuthor==strAuthor) { isFind = true; book3.m_BookAuthor = m_Books [i].m_BookAuthor; book3.m_BookName = m_Books [i].m_BookName; book3.m_Price = m_Books [i].m_Price; } } if (isFind1) { m_Books.Remove (book3); Console.WriteLine ("删除成功!!!!!"); } else { Console.WriteLine ("删除失败!!!!!"); } break; } //进行其中的显示的清除 Console.Clear (); } break; //修改 case "C": { int index = 0; bool isFind = false; Book book3 = new Book ();; Console.Write ("请输入要修改的书的名字:"); string strName = Console.ReadLine (); for (int i = 0; i < m_Books.Count; i++) { if (m_Books[i].m_BookName==strName) { isFind = true; index = i; } } if (isFind) { Console.WriteLine ("Y:修改图书名称 H:修改图书作者 G:修改图书价格"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); switch (str) { case "Y": Console.Write ("请输入您要修改成的图书名称:"); string strName1 = Console.ReadLine (); book3.m_BookName = strName1; book3.m_BookAuthor = m_Books[index].m_BookAuthor; book3.m_Price = m_Books[index].m_Price; break; case "H": Console.Write ("请输入您要修改成的图书作者:"); string strAuthor = Console.ReadLine (); book3.m_BookName = m_Books[index].m_BookName; book3.m_BookAuthor = strAuthor; book3.m_Price = m_Books[index].m_Price; break; case "G": Console.Write ("请输入您要修改成的图书名称:"); int price = int.Parse (Console.ReadLine ()); book3.m_Price = price; book3.m_BookName = m_Books[index].m_BookName; book3.m_BookAuthor = m_Books[index].m_BookAuthor; break; } m_Books.RemoveAt (index); m_Books.Insert (index,book3); book3.m_BookAuthor = ""; book3.m_BookName = ""; book3.m_Price = 0; } else { Console.WriteLine ("没有此图书无法进行修改!!!!!"); } //进行其中的显示的清除 Console.Clear (); } break; //查看 case "S": { Console.WriteLine ("N:按名称查找 Z:按作者查找"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); str = str.ToUpper (); switch (str) { case "N": { Console.Write ("请输入要查找书的名称:"); string strName = Console.ReadLine (); //判断是否找到此书 bool isFind = false; //记录找到的书的下标 int index = 0; for (int i = 0; i < m_Books.Count; i++) { if (m_Books [i].m_BookName == strName) { isFind = true; index = i; } } if (isFind) { Console.WriteLine ("书的名字为:{0},书的作者为:{1},书的价格为:{2}", m_Books [index].m_BookName, m_Books [index].m_BookAuthor, m_Books [index].m_Price); } else { Console.WriteLine ("没有找到这本书!!!!!!"); } } break; case "Z": { Console.Write ("请输入要查找书的作者:"); string strAuthor = Console.ReadLine (); //判断是否找到此书 bool isFind = false; //记录找到的书的下标 int index = 0; for (int i = 0; i < m_Books.Count; i++) { if (m_Books [i].m_BookName == strAuthor) { isFind = true; index = i; } } if (isFind) { Console.WriteLine ("书的名字为:{0},书的作者为:{1},书的价格为:{2}", m_Books [index].m_BookName, m_Books [index].m_BookAuthor, m_Books [index].m_Price); } else { Console.WriteLine ("没有找到这本书!!!!!!"); } } break; } //进行其中的显示的清除 Console.Clear (); } break; case "P": for (int i = 0; i < m_Books.Count; i++) { Console.WriteLine ("书的名字:{0},书的作者:{1},书的价格:{2}", m_Books[i].m_BookName, m_Books[i].m_BookAuthor, m_Books[i].m_Price); } break; } if (input=="Q") { break; } } Console.WriteLine ("退出程序成功!!!!!"); } } }
第二种:
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace books_Manage { enum Bookstate { 增加图书=1,删除图书=2,修改图书=3,查看图书=4,退出系统=5 } /// <summary> /// 图书的所有属性 /// </summary> class Book { private string bookid; private string bookname; private int bookprice; private int bookcount; public string Bookid{ get{ return bookid; } set{ bookid = value; } } public string Bookname{ get{ return bookname; } set{ bookname = value; } } public int Bookprice{ get{ return bookprice; } set{ bookprice = value; } } public int Bookcount{ get{ return bookcount; } set{ bookcount = value; } } } class MainClass { public static void Main(string[] args) { bool flag = false; List<Book> list = new List<Book> (); Book b1 = new Book ();; b1.Bookid = "1"; b1.Bookname = "lanou"; b1.Bookcount = 12; b1.Bookprice = 34; BookBean bookbean = new BookBean (); while (true) { Console.WriteLine (" 图书管理系统"); Console.WriteLine ("*********************************"); Console.WriteLine ("* 1.增加图书 *"); Console.WriteLine ("* 2.删除图书 *"); Console.WriteLine ("* 3.修改图书 *"); Console.WriteLine ("* 4.查看图书 *"); Console.WriteLine ("* 5.退出系统 *"); Console.WriteLine ("*********************************"); Console.Write ("请输入你要进行的操作:"); int number = int.Parse (Console.ReadLine()); Bookstate bookstate = (Bookstate)number; switch (bookstate) { case Bookstate.修改图书: bookbean.updateBook (list); break; case Bookstate.删除图书: list = bookbean.delBook (list); break; case Bookstate.增加图书: list=bookbean.AddBook (list); break; case Bookstate.查看图书: bookbean.ShowBook (list); break; case Bookstate.退出系统: flag = true; break; default: Console.WriteLine ("没有其对应的输入请重新输入!!!!!"); break; } //点击退出系统后所进行的操作 if (flag) { break; } } Console.WriteLine ("退出图书管理系统成功!!!!!!"); } } class BookBean { /// <summary> /// 进行图书的添加 /// </summary> public List<Book> AddBook(List<Book> list) { int temp = list.Count; Console.Write ("请输入要进行添加的图书的数目:"); int count = int.Parse (Console.ReadLine ()); for (int i = 0; i < count; i++) { Console.WriteLine ("请输入第{0}本书的信息:",(i+1)); Console.Write ("请输入要添加的图书的编号:"); string bookid = Console.ReadLine (); Console.Write ("请输入要添加的图书的名称:"); string bookname = Console.ReadLine (); Console.Write ("请输入要添加的图书的数量:"); int bookcount =int.Parse(Console.ReadLine ()); Console.Write ("请输入要添加的图书的价格:"); int bookprice =int.Parse(Console.ReadLine ()); Book book = new Book (); book.Bookid = bookid; book.Bookname = bookname; book.Bookcount = bookcount; book.Bookprice = bookprice; list.Add (book); } if (temp<list.Count) { Console.WriteLine ("添加成功!!!!!"); } else { Console.WriteLine ("添加失败!!!!!"); } return list; } /// <summary> /// 进行图书的查看 /// </summary> /// <param name="list">List.</param> public void ShowBook(List<Book> list) { int counti=1; Console.WriteLine ("-----------------------------------------------------------------"); Console.WriteLine (" | 图书编号 | 图书名称 | 图书价格 | 图书数量 |"); Console.WriteLine ("-------|---------------------------------------------------------"); foreach (var item in list) { Console.Write (" {0} | {1} | {2} | {3} | {4} |",counti,item.Bookid,item.Bookname,item.Bookprice,item.Bookcount); Console.WriteLine (); Console.WriteLine ("-----------------------------------------------------------------"); counti++; } Console.WriteLine (); Console.WriteLine ("一共{0}条数据!!!!",list.Count); Console.WriteLine (); } /// <summary> /// 进行图书的删除 /// </summary> /// <param name="list">List.</param> public List<Book> delBook(List<Book> list) { int temp = list.Count; Console.Write ("请输入你要删除的图书的名字:"); string strname = Console.ReadLine(); int temp1=0; int column = 0; foreach (Book item in list) { if (item.Bookname==strname) { temp1=column; } column++; } list.RemoveAt(temp1); if (temp>list.Count) { Console.WriteLine ("删除成功!!!!!"); column = 0; } else { Console.WriteLine ("删除失败!!!!!"); } return list; } /// <summary> /// 进行图书的修改 /// </summary> /// <returns>The book.</returns> /// <param name="list">List.</param> public void updateBook(List<Book> list) { bool flag = false; Console.Write ("请输入要进行修改的学生的姓名:"); string strname = Console.ReadLine (); int temp1=0; int column = 0; foreach (Book item in list) { if (item.Bookname==strname) { temp1=column; } column++; } Console.WriteLine (); Console.WriteLine ("图书的信息为:"); Console.WriteLine ("图书的编号:{0}",list[temp1].Bookid); Console.WriteLine ("图书的名称:{0}", list[temp1].Bookname); Console.WriteLine ("图书的数量:{0}",list[temp1].Bookcount); Console.WriteLine ("图书的价格:{0}",list[temp1].Bookprice); Console.WriteLine (); Console.WriteLine ("请选择要进行修改的信息:"); Console.WriteLine ("1.图书编号!!!"); Console.WriteLine ("2.图书名称!!!"); Console.WriteLine ("3.图书数量!!!"); Console.WriteLine ("4.图书价格!!!"); Console.WriteLine ("5.退出修改!!!"); Console.Write ("请选择:"); int number = int.Parse (Console.ReadLine()); switch (number) { case 1: { Console.Write ("请输入要修改成的图书编号:"); string strbookid = Console.ReadLine (); list [temp1].Bookid = strbookid; //判断是否修改成功 if (list[temp1].Bookid==strbookid) { Console.WriteLine ("修改成功!!!!!"); } else { Console.WriteLine ("修改失败!!!!!"); } } break; case 2: { Console.Write ("请输入要修改成的图书名称:"); string strbookname = Console.ReadLine (); list [temp1].Bookname = strbookname; //判断是否修改成功 if (list[temp1].Bookname==strbookname) { Console.WriteLine ("修改成功!!!!!"); } else { Console.WriteLine ("修改失败!!!!!"); } } break; case 3: { Console.Write ("请输入要修改成的图书价格:"); int strbookprice =int.Parse(Console.ReadLine ()); list [temp1].Bookprice = strbookprice; //判断是否修改成功 if (list[temp1].Bookprice==strbookprice) { Console.WriteLine ("修改成功!!!!!"); } else { Console.WriteLine ("修改失败!!!!!"); } } break; case 4: { Console.Write ("请输入要修改成的图书数量:"); int strbookcount =int.Parse(Console.ReadLine ()); list [temp1].Bookcount = strbookcount; //判断是否修改成功 if (list[temp1].Bookcount==strbookcount) { Console.WriteLine ("修改成功!!!!!"); } else { Console.WriteLine ("修改失败!!!!!"); } } break; case 5: flag = true; break; } if (flag) { Console.WriteLine ("退出修改成功!!!!"); } } } }
以上是两种编程思路 也是两种编程思想,供大家参考一下,大家有好的题目可以在下方评论提出来,一起分享讨论!!!!!!
原文地址:https://www.cnblogs.com/baosong/p/8472640.html
时间: 2024-10-11 12:09:20