0

I have 3 arrays. One for the Item Number, One for Item Description and One for Price.

I want to be able to create a "table" out of it so it reads as:

Item Number:    Description:    Price:
   50              Water          $50
   752             Carrots        $.60
   67              Ice             $9 

and etc. I tried creating methods using a foreach statement and then calling the methods. It works however it doesn't print side by side. Any ideas on how to fix this?

Here is the code `

using static System.Console;
class FoodOrder
{
         static void Main()
        {
        //Variables used in the method
        const int MENU_ASTERIKS = 42;
        string description = "Description", itemNumber = "Item number", price = "Price";
        string[] itemDescription = { "Cheese Frise", "Corn Dogs", "Cheeseburger", "Hamburger", "Fries", "Fountain Drink", "Philly Cheese Steak", "Tacos" };
        int[] itemNumList = { 20, 23, 25, 31, 35, 38, 39, 34, };
        double[] itemPrice = { 2.95, 1.95, 2.25, 3.10, 4.50, 3.65, 5.00, 2.75};
        Write("\t");
        for (int x = 0; x < MENU_ASTERIKS; ++x) //Creates a top border for the menu with the prices and is the start of the menu 
            Write("*");
        WriteLine(" ");
        Write("\t   {0}\t{1}\t{2}", itemNumber, description, price);
        DisplayItemNum(itemNumList); DisplayDescriptions(itemDescription);
    }
        //Method to dispay item number
        private static void DisplayItemNum( params int[] itemNums)
        {
        WriteLine("\n");
        foreach (int number in itemNums)
            WriteLine("\t        {0} ", number);
        }
        //Method to Display item Number
        private static void DisplayDescriptions(params string[] desc)
        {
        WriteLine("\n");
        foreach (string objects in desc)
            WriteLine("\t\t\t{0}", objects);


        }

}

`

source code

output of source file

SpacedOutKID
  • 95
  • 1
  • 14

1 Answers1

3

Right now you're looping 3 times, for all the lists. Then you call Console.WriteLine which adds a newline character to the end of your output, so it displays on next line.

What you istead want to do is call Console.Write which does not add the newline character. There are also better ways to do this. Instead of keeping 3 seperate lists, you could also bundle your information in a class and create a list of that:

public class MyTableRow
{
   public int Number {get;set;}
   public string Description {get;set;}
   public float Price {get;set;}
}

Then you can create a new object of this class, fill that with your data and .Add() that to a list in you Main, e.g. var MyTableRows = new List<MyTableRow>(); then MyTableRows.Add(new TableRow{ Number = 1, Description = "REE", Price = 1.0f});

You can then loop that list and display the output.

sommmen
  • 6,570
  • 2
  • 30
  • 51
  • @Sommment ohkay so I am really new to c# and just got to the methods in my class so I kind of understand this, so I am going to try it. If you have time would you be able to write a short description of what each action does? so I can learn it better instead of copying and pasting variables? – SpacedOutKID Apr 11 '20 at 13:40
  • @SpacedOutKID hi, well these are really the basics of programming in general so i would suggest you look up some tutorials instead. Also read through all features of the c# language online https://learn.microsoft.com/en-us/dotnet/csharp/getting-started/introduction-to-the-csharp-language-and-the-net-framework don't try to understand everything at once, it won't work. First read it all, get a overview in your mind and then get a general idea of what you're doing and then google the bits and pieces together. Thats how i learned, and still learn as a proffesional developer. – sommmen Apr 11 '20 at 18:21
  • Proffesional as in the sense that i use programming in a proffesional capability, and not like i'm a pro in any way ;) – sommmen Apr 11 '20 at 18:22