简体   繁体   中英

C# Console Application where user enters resource and is told what Junk is required

Hoping someone can help. I'm a very basic beginner in C#. I set myself the task of creating a console application where I enter a resource I need ie Screw or screws into the console, hit enter and it retrieves all the junk from the list that contain that word, along with the amount of resources you would receive from the junk. So Type Writer would be 2 screws. It's for Fallout 76 so I can see what junk contains what resource.

However I'm having a few issues:

  1. The console only returns one result for screw, even though there are multiple results. (How do I resolve this and receive multiple results? ie Clip board and Toy Car)
  2. How do I search and get results for partial matches ie if I type scre or screws?
  3. Is there a better way of creating a console application where you store values and a user searches for those values? I cannot find anything close to what I need scouring the internet.

Thank you for all the help.

    using System;
using System.Collections.Generic;
class JunkList
{
    public string Resource { get; set; }
    public string Junk { get; set; }
    public int Amount { get; set; }

    public JunkList(string r, string j, int a)
    {
        this.Resource = r;
        this.Junk = j;
        this.Amount = a;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string searchName;
        List<JunkList> infoList = new List<JunkList>();
        infoList.Add(new JunkList("Screw", "Type Writer", 2));
        infoList.Add(new JunkList("Screw", "Clip Board", 1));
        infoList.Add(new JunkList("Screw", "Toy Car", 3));
        Console.Write("Which resource do you want to search for?? \n");
        searchName = Console.ReadLine();
        for (int i = 0; i < infoList.Count; i++)
        {
            if (string.Compare(searchName, infoList[i].Resource, true) == 0)
            {
                Console.Write("Resource : " + infoList[i].Resource + "\n");
                Console.Write("Junk : " + infoList[i].Junk + "\n");
                Console.Write("Resource Amount : " + infoList[i].Amount + "\n");
                break;
            }
        }
        Console.ReadKey();
    }
}

The console only returns one result for screw, even though there are multiple results. (How do I resolve this and receive multiple results? ie Clip board and Toy Car)

Your issue is that you're breaking after finding the very first result:

if (string.Compare(searchName, infoList[i].Resource, true) == 0)
{
     Console.Write("Resource : " + infoList[i].Resource + "\n");
     Console.Write("Junk : " + infoList[i].Junk + "\n");
     Console.Write("Resource Amount : " + infoList[i].Amount + "\n");
     break; // REMOVE THIS LINE
}

break means that you're exiting from your for loop, so you're actually exiting as soon as you find a result.

How do I search and get results for partial matches ie if I type scre or screws?

You can change your if condition from

if (string.Compare(searchName, infoList[i].Resource, true) == 0)

to something like

if (infoList[i].Resource.ToLowerInvariant().Contains(searchName.ToLowerInvariant()))

This checks if the inserted string is contained in one of the strings that you have in your list. The ToLowerInvariant is used to make the search case-insensitive.

Of course, this is not a fuzzy-search, so searching for screws won't lead to any result. Doing fuzzy searches is a little bit more complex and it's probably outside the scope of this question.

Is there a better way of creating a console application where you store values and a user searches for those values? I cannot find anything close to what I need scouring the internet.

Opinion-based questions are not meant for StackOverflow, and asking a better way to do something is one of those. I don't think you'll get an answer on this.

To make it a less subjective, I'm going to interpret your last question as "Is there a common software pattern or approach that is used for storing data objects that can then be easily searched?"

In that regard, you're describing features and capabilities that a database is really good at, specifically storing some table of data and providing a fast way to search (query) it using criteria that you define.

C# and the .NET frameworks offer some pretty in-depth and robust tools for leveraging databases and managing mappings between data classes like JunkList and database tables; these types of tools are called ORMs, or Object Relational Mappers, and you'll see reference to them scattered throughout Microsoft's documentation, especially Entity Framework.

When getting started, more complicated ORMs can get a bit overwhelming, so it's not a bad idea to start small. Check out some very simple ORMs that use something like SQLite, which just live in a file and require minimal dependencies (the act of calling the database actually can be enough to create it.).

You might take a look at https://github.com/praeclarum/sqlite-net , which is very easy to set up in a matter of minutes and start using with just the basics provided in its documentation.

  1. The reason you only get one result back is because of the break statement. It cancels the for loop. Try removing it.

  2. Try looking at the String.Contains() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM