C#在列表中查找图书的方法

C#在列表中查找图书的方法,c#,C#,我必须创建一个Library类,其中包含一个书名、作者和ID的图书列表。我必须创建添加书籍、按标题查找书籍、显示所有书籍和删除书籍的方法 我的addBook()方法正确吗 我的findBook()方法有一个错误,它说在Book类中创建一个构造函数,但是已经有了一个构造函数 而且我真的不知道从哪里开始从列表中删除一本书。。。我已经找到了从列表中删除项目的解决方案,但我真的不知道如何在我的程序中实现它 我不了解很多事情,因为我的第一门大学课程CSE1301很糟糕,我在1302年必须学习基本的东西和更

我必须创建一个Library类,其中包含一个书名、作者和ID的图书列表。我必须创建添加书籍、按标题查找书籍、显示所有书籍和删除书籍的方法

我的addBook()方法正确吗

我的findBook()方法有一个错误,它说在Book类中创建一个构造函数,但是已经有了一个构造函数

而且我真的不知道从哪里开始从列表中删除一本书。。。我已经找到了从列表中删除项目的解决方案,但我真的不知道如何在我的程序中实现它

我不了解很多事情,因为我的第一门大学课程CSE1301很糟糕,我在1302年必须学习基本的东西和更复杂的东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 using System.Threading.Tasks;

namespace ConsoleApplication9
{

    public class Library
    {
        public string name;
        public List<Book> books = new List<Book>();

        public void addBook(string title, string author)
        {
            int bookQuantity;

            string btitle;
            string bauthor;

            bookQuantity = int.Parse(Console.ReadLine());

            for (int x = 0; x <= bookQuantity; x++)
            {
                Console.WriteLine("Enter Title:");

                btitle = Console.ReadLine();

                Console.WriteLine("Enter Author:");
                bauthor = Console.ReadLine();

                books.Add(new Book(btitle, bauthor));

            }
        }


        public void findBook()
        {
            var obj = new Book(title, author);
            obj.ID = "xy";
            string id = obj.ID;

            Book result = books.Find(x => x.ID == "xy");
        }

        public void displayBooks()
        {
            foreach (Book b in books)
            {
                Console.WriteLine(b.ToString());
            }
        }

        public void removeBook()
        {

        }

        public Library()
        {

        }
    }

    public class Book
    {
        public string title;
        public string author;
        public int id;
        static int isbn;

        public string ID { get; set; }

        public void assignID()
        {
            id = isbn;
            isbn++;
        }

        public Book(string title, string author)
        {
            this.title = title;
            this.author = author;
        }

        public override string ToString()
        {
            return string.Format("Title: " + title, "\nAuthor: " + author, "\nISBN: " + id);
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序9
{
公共班级图书馆
{
公共字符串名称;
公共列表书籍=新列表();
公共void addBook(字符串标题、字符串作者)
{
国际图书数量;
字符串btitle;
弦包托尔;
bookQuantity=int.Parse(Console.ReadLine());
对于(int x=0;x x.ID==“xy”);
}
公共图书
{
foreach(图书中的b册)
{
Console.WriteLine(b.ToString());
}
}
公共无效删除手册()
{
}
公共图书馆()
{
}
}
公共课堂用书
{
公共字符串标题;
公共字符串作者;
公共int id;
静态整数isbn;
公共字符串ID{get;set;}
公共无效分配ID()
{
id=国际标准书号;
isbn++;
}
公共书籍(字符串标题、字符串作者)
{
this.title=标题;
this.author=作者;
}
公共重写字符串ToString()
{
返回string.Format(“Title:”+Title,“\nautor:”+author,“\nISBN:”+id);
}
}
}
在“findBook”方法中,您创建了两次新的book对象,并且没有返回任何内容

你可能想要这样的东西:

public Book findBook(string id)
{
    return books.Find(x => x.ID == id);
}
如果我们将其分解:

“public”是一个“”并描述谁可以访问您的方法

“Book”是我们将从该方法返回的对象类。当我们正在寻找一本书时,在这里返回Book类的实例是有意义的

“findBook”是方法的名称

“(stringid)”在大括号中表示该方法作为输入的变量。我们声明需要一个“字符串”,并将其命名为“id”

最后,要调用此方法,我们可以使用以下代码:

Book myBook = findBook("xy");
检查一本书是否已经存在也可能有用

public bool hasBook(string title, string author)
{
    return (findBook(title, author) != null);
}
公共类库
{
公共列表书籍=新列表();
公共void addBook(字符串标题、字符串作者)
{
var bookQuantity=int.Parse(Console.ReadLine());
对于(intx=0;xbook.ID==ID));
}
公共图书
{
foreach(图书中的b册)
{
Console.WriteLine(b.ToString());
}
}
公共无效删除手册()
{
Console.WriteLine(“输入ID:”);
var id=int.Parse(Console.ReadLine());
var temp=books.FirstOrDefault(book=>book.ID==ID);
如果(温度!=null)
书籍。移除(临时);
}
}
公共课堂用书
{
公共字符串标题{get;set;}
公共字符串作者{get;set;}
公共整数isbn{get;set;}
公共int ID{get;set;}
公共重写字符串ToString()
{
返回string.Format(“Title:”+Title,“\nautor:”+author,“\nISBN:”+ID);
}
}

这会修复您的所有功能吗?

首先要做的事情是:了解.NET命名约定。其次,了解属性,避免使用公共字段。接下来,在每篇文章中问一个问题,使其非常具体,并提供一个(强调最低限度…)我现在有一个考试。我真的需要帮助我担心你的紧迫性丝毫不会影响Stack Overflow是一个高质量问题和答案的存储库这一事实,而目前这个问题不符合这一标准——首先,它太宽泛了。请读一读你现在帮不了我吗?这是我通过考试的最后希望。如果你一直等到最后一分钟才寻求帮助,以至于你甚至没有足够的时间准备一个合适的问题,那么网站的质量就不会因此受到影响。恐怕我们不是来为你做考试或家庭作业的。请拿着我写的东西,试着了解发生了什么。如果你还有其他问题,请提问。我不能做:(英语水平低。我不知道你怎么做你用代码写的东西。我不明白为什么,但它不会开始。如果它会开始,你说它不会开始是什么意思?
public bool hasBook(string title, string author)
{
    return (findBook(title, author) != null);
}
public class Library
    {
        public List<Book> books = new List<Book>();

        public void addBook(string title, string author)
        {
            var bookQuantity = int.Parse(Console.ReadLine());


            for (int x = 0; x <= bookQuantity; x++)
            {
                Console.WriteLine("Enter Title:");
                var btitle = Console.ReadLine();

                Console.WriteLine("Enter Author:");
                var bauthor = Console.ReadLine();

                books.Add(new Book() { title = btitle, author = bauthor, ID = x + 1, isbn = x + 1 });
            }
        }


        public void findBook()
        {
            Console.WriteLine("Enter ID:");
            var id = int.Parse(Console.ReadLine());

            Book result = books.FirstOrDefault((book => book.ID == id));
        }

        public void displayBooks()
        {
            foreach (Book b in books)
            {
                Console.WriteLine(b.ToString());
            }
        }

        public void removeBook()
        {
            Console.WriteLine("Enter ID:");
            var id = int.Parse(Console.ReadLine());
            var temp = books.FirstOrDefault(book => book.ID == id);
            if (temp != null)
                books.Remove(temp);
        }
    }

    public class Book
    {
        public string title { get; set; }
        public string author { get; set; }
        public int isbn { get; set; }
        public int ID { get; set; }

        public override string ToString()
        {
            return string.Format("Title: " + title, "\nAuthor: " + author, "\nISBN: " + ID);
        }
    }