0

I want to create an application where the user can enter items and the price, then show the most expensive and the cheapest item.

I'm totally new to C# and have no programming experience of any language, started to learn 2,5 weeks ago. I don't understand how I can link the item with the price and then do the calculation. The penny hasn't dropped yet in terms of how I can build an application :(

        ItemInput();
        PricingInput();

    }


    private static void ItemInput()
    {
        Console.WriteLine("Add your items and price, once you're done, type 'end' ");
        AskForTheItem();
        AskForThePrice();
    }

    static void AskForTheItem()
    {

            while (itemPrice != "end")
            {
                Console.Write("Add your item:");
                string item = Console.ReadLine();
                Console.Write("Add the price:");
                int price = int.Parse(Console.ReadLine());
                itemPrice = Console.ReadLine();

            }
            Console.WriteLine("Add your item: ");
       name = Console.ReadLine();
        numberOfItems++;
    }
    static void AskForThePrice()
    {
        Console.WriteLine("Add the price: ");
        price = int.Parse(Console.ReadLine());
        numberOfPrice++;
    }
    private static void PricingInput()
    {
        Console.WriteLine($"The cheapest item is: {name} and the most expensive item is: {name} ");
  • You can do it using `.Min()` and `.Max()`, but before that you need to store item, price in a collection, it might be an array or list etc. Restructure your program to store list of ItemDetails, use of class would be best choice here with 2 properties i.e. ItemName and Price and then Perform min and Max operation on it. If someone tries to answer your question, he/she need to refactor your code first – Prasad Telkikar May 31 '19 at 09:41

2 Answers2

0

Welcome to StackOverflow! Your question seems to be pretty broad but I think what you'd need is some first steps into object-oriented programming with classes.

Your code seems like a good start but for storing the information you'll need some objects.
You can create your own objects using a class. In this case you class would need a property for its name and one for it's price. Defining the class would be as follows (you should do that in a new file):

class MyItem{
    public string Name { get; set; }
    public int Price { get; set; }
}

Then at the start of the class with the AskForTheItems method and probably also the Main method, you need to add a List to store the items. You'd do that like this:

private static List<MyItem> items = new List<MyItem>();

For the method which gets the items, you'll need to some adjustments. You also don't need the numberOfItems-variable anymore since you can just call items.Count if you need it.

private static void AskForTheItems()
{
    do
    {
        Console.WriteLine("Add your item:");
        // get the name
        Console.Write("Name: ");
        string name = Console.ReadLine();
        // get and convert the price
        Console.Write("Price: ");
        int price = int.Parse(Console.ReadLine());

        // create the item and fill it with the values
        MyItem item = new MyItem();
        item.Name = name;
        item.Price = price;

        // add the item to your list
        items.Add(item);

        // ask if the user want's to add another one
        Console.WriteLine("Again? (y/n)");
    }
    while (Console.ReadKey(true).Key != ConsoleKey.N);
}

Printing the cheapest and most expensive one is fairly easy using Linq (there might even be an easier/better way).

private static void PrintCheapestAndMostExpensive() {
    // get the first item where the price is the same as the minimum of all prices in the list
    MyItem cheapestItem = items.First(i => i.Price == items.Min(i2 => i2.Price));
    // get the first item where the price is the same as the maximum of all prices in the list
    MyItem mostExpensiveItem = items.First(i => i.Price == items.Max(i2 => i2.Price));

    Console.WriteLine($"The cheapest item is: {cheapestItem.Name} and the most expensive item is: {mostExpensiveItem.Name}");
}

I hope this helps. If you have any questions, feel free to ask - I'll try to explain anything you don't understand yet.

By the way there a are few things I didn't do/cover because I feel like it'd be a bit too much to explain for a small program like this. Currently there is no error handling when converting the input-string to an int (bad). There are also easier ways to write the initialisation for the item but I figured this way it's easier to understand.
Also with this code it always prints the first item which has the lowest price. If there are two which both have the lowest price, this might be a problem.
Linq itself is fairly hard (but very very important), that's why I would strongly reccomend you to read something about it. I'd happily explain the Linq I used here with more detail but since it's such a huge topic, I won't cover much more.

Joelius
  • 3,839
  • 1
  • 16
  • 36
0
 private static void ItemInput()
    {
         PricingInput(AskForTheItem());        
    }

    static Dictionary<String, int> AskForTheItem()
    {

            var result = new Dictionary<String, int>();
            Console.WriteLine("Add your items and price, once you're done, type empty item ");
            while (1)
            {
                Console.Write("Add your item or keep empty :");
                string item = Console.ReadLine();
                if(String.IsNullOrEmpty(item)) return result;
                Console.Write("Add the price:");
                while(!int.TryParse(out var price, Console.ReadLine())                 Console.Write("Wrong price, please Add the price:");
                result.Add(item, price);
            }
    }

    private static void PricingInput(Dictionary<String,int> list)
    {
        String minItem = null;
        int minPrice = -1;
        String maxItem = null;
        int maxPrice = -1;
        foreach(var i in list) {
            if(price<0 || i.Value<price) { minItem = i.Key; minPrice=i.Value;}
            if( i.Value>price) { maxItem = i.Key; maxPrice=i.Value;}
        Console.WriteLine($"The cheapest item is: {1} and the most expensive item is: {2} ",minItem,maxItem); }
Solarev Sergey
  • 223
  • 2
  • 4
  • Thank you for your answer. In C#, while(1) will not compile. – Joelius May 31 '19 at 10:19
  • Also if you use $ in front of a string the int-value in between {} will be a literal so the last line of your PricingInput method will always print cheapest item as 1 and most expensive item as 2. See [this answer](https://stackoverflow.com/a/31014895/10883465). – Joelius May 31 '19 at 10:27