Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey , its my first time coding .i wanna make a list of books that got prices(value) so i can sort em out or search em via the price ik how to make a list but how do i add value to each book. here is the code .

What I have tried:

namespace project
{
    class Cs
    {
        static void Main()
        {
            List<string> Booklist = new List<string>();
            Booklist.Add("Csp");
            Booklist.Add("lol");
            Booklist.Add("idk how to code");
            Console.WriteLine("1) Add new book");
            Console.WriteLine("2) Search the books by name and show details");
            Console.WriteLine("3) Show all books");
            Console.WriteLine("4) Remove the book");
            Console.WriteLine("5) Show the most expensive book");
            Console.WriteLine("6) Show the cheapest book");
            Console.WriteLine("7) Show number of books");
            Console.WriteLine("8) Show books by price(sort by price)");
            Console.WriteLine("9) Show the books between two prices");
            Console.WriteLine("10) Exit");
            int Num = int.Parse(Console.ReadLine());
            switch (Num)
            {
                case 1:
                    Console.WriteLine("write new book name and price");
                    Booklist.Add(Console.ReadLine());
                    break;
                    case 2:
                    Console.WriteLine("Write books name");
                    string search = (Console.ReadLine());
                    bool isfound = false;
                    foreach (string item in Booklist)
                    {
                        if (search == item)
                        {
                            isfound = true;
                        }
                    }
                    if(isfound == true) 
                    {
                        Console.WriteLine(search);
                    }
                    else
                    { Console.WriteLine("Not Found");
                    }
                    break;
                    case 3:
                    foreach (string item in Booklist)
                    Console.WriteLine(item);
                    break;
                    case 4: 
                    Booklist.Remove(Console.ReadLine());
                    break;
                    case 5:
                    break;
                    case 6:
                    break;
                    case 7:
                    Console.WriteLine(Booklist.Count);
                    break;
                    case 8:
                    break;
                    case 9:
                    break;
                    case 10:
                    Console.WriteLine("Existing...");
                    break;

                    
                    
            }
            Console.ReadKey();
        }
    }
Posted

You should define a class "Books" which contains all information you want to store for a book (Title, authors name, price, year of print etc....).
Then you can define list with the type "Book".
 
Share this answer
 
The problem you have here is that you are using a list with a string. What this means is that you can only ever store an unstructured entry here; suppose that you wanted to have The Hobbit as a book, and it cost 9.99. If the user entered "The Hobbit 9.99" then, when you searched for The Hobbit, you wouldn't find the book. There are a couple of options that immediately spring to mind - use a class to represent the book, e.g.:
C#
public class Book
{
  public string Name { get; set; }
  public decimal Amount { get; set; }
}
Note that I'm using a decimal to represent the money, and not a string. You should always try to use the most appropriate data type.

While that solution would work, I would recommend using a Dictionary<string, decimal>[^] instead, if you could only ever have one instance of the book. The reason I am suggesting this is that searching for the book by the title becomes extremely trivial. You don't have to loop to find the entry, you just use the inbuilt capabilities of the dictionary. Be aware that you are going to have to prompt the user for two pieces of information to add the book; you are going to have to ReadLine the name first, and then do a separate ReadLine to read the amount.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900