Home > database >  the Console.Write get's printed in a loop, and appears before all the input has been submited
the Console.Write get's printed in a loop, and appears before all the input has been submited

Time:09-11

this is the input I'm using:

Bread Juice Fruits Lemons 
10 50 20 30
2.34 1.23 3.42 1.50
Bread
Juice
done

this is my code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace List.Store
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = Console.ReadLine();
            string quantity = Console.ReadLine();
            string price = Console.ReadLine();
            List<string> names = name.Split(" ").ToList();
            List<long> quantities = quantity.Split(" ").Select(long.Parse).ToList();
            List<double> prices = price.Split(" ").Select(double.Parse).ToList();
            string input = Console.ReadLine();
            while(input != "done")
            {
                for (int a = 0; a < names.Count; a  )
                {
                    if (names[a] ==  input)
                    {
                        Console.WriteLine($"{input} costs {prices[a]}; Available quantity: {quantities[a]}");
                    }
                    input = Console.ReadLine();
                }
            }
        }
    }
}

this is the output I want to receive:

Bread costs: 2.34; Available quantity: 10
Juice costs: 1.23; Available quantity: 50

this is the output I'm receiving: enter image description here The problem is that the 1st and 2nd message is getting printed before the "done" command. I don't know if that is a problem, but still, I'm writing this. Usually, I will move the Console.WriteLine outside of the loop, but I need it to be inside that loop so I can print the right thing with the variable "a".

CodePudding user response:

Fully OOP solution check the commented lines comments.

using System;
using System.Collections.Generic;
using System.Linq;


namespace Store
{
    class Program
    {
        static void Main(string[] args)
        { 
            //UNCOMMENT TO BE ABLE TO TYPE
            /*
            string name = Console.ReadLine();
            string quantity = Console.ReadLine();
            string price = Console.ReadLine();

            */
            //COMMENT TO DO WHAT IS ABOVE
            string name = "Bread Juice Fruits Lemons";
            string quantity = "10 50 20 30";
            string price = "2.34 1.23 3.42 1.50";
            //END COMMENT WHAT IS ABOVE

            List<string> names = name.Split(Convert.ToChar(" ")).ToList();            
            List<long> quantities = quantity.Split(Convert.ToChar(" ")).Select(long.Parse).ToList();
            List<double> prices = price.Split(Convert.ToChar(" ")).Select(double.Parse).ToList();

            List<Store> stores = new List<Store>();
            if (names.Count == quantities.Count && quantities.Count == prices.Count)
            {
                for(int i=0;i<names.Count;i  )
                {
                    Store store = new Store();
                    store.Name = names[i];
                    store.Quantity = quantities[i];
                    store.Price = prices[i];
                    stores.Add(store);
                }            
            }
            
            string input = Console.ReadLine();
            while (input != "done")
            {
                var store =stores.Find(x => x.Name == input);
                Console.WriteLine(store.ToString());
                input=Console.ReadLine();
             
            }
            
        }
    }

    public class Store {

        public string Name { get; set; }
        public long Quantity { get; set; }
        public double Price { get; set; }

        public override string ToString()
        {
            return $"{this.Name} costs {this.Price}; Available quantity: {this.Quantity}";
        }

    }
}

CodePudding user response:

I added a list to save all searched items and at the end I print the list. I added a class to save all the stuff.

string name = Console.ReadLine();
string quantity = Console.ReadLine();
string price = Console.ReadLine();
List<string> names = name.Split(" ").ToList();
List<long> quantities = quantity.Split(" ").Select(long.Parse).ToList();
List<double> prices = price.Split(" ").Select(double.Parse).ToList();
string input = Console.ReadLine();
List<Product> searchedItems = new List<Product>();

while (input != "done")
{
    int index = names.FindIndex(x => x == input);
    if (index != -1)
        searchedItems.Add(new Product() { Name = names[index], Price = prices[index], Quantity = quantities[index] });

    input = Console.ReadLine();

}
searchedItems.ForEach(item =>
{
    Console.WriteLine($"{item.Name} costs {item.Price}; Available quantity: {item.Quantity}");
});


class Product
{
    public string Name { get; set; }
    public long Quantity { get; set; }
    public double Price { get; set; }
}
  •  Tags:  
  • c#
  • Related