CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    1

    Issue with object arrays and classes

    I'm working on a school assignment. The program is to create a simple restaurant menu and ordering program. The program inputs data from a source file the user selects, displays the menu to them, the user orders, and then the bill is tallied and displayed.

    In the main method, I've created an array of objects "menuItems" and have succesfully populated it from an input file. However when I pass the array to the ShowMenu method and use a simple loop to display all the menu options I get 8 lines of blank data. Help please, I'm sure its just something simple that i'm missing but for a beggining programmer its very frustrating.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace Program1
    /*Breakfast Ordering Program
    This program will read data from an input file, storing the data in an array,
    then it will display ordering options to the customer.
    The customer will select the different items they want
    as well as the quantities of each item and the program
    will then print their order back to the user as well as the total bill. */
    {
    class Menu //class from which the object array menuItems will be created.
    {
    private string description; //stores the item description.
    private double price; //stores the price of the item.
    public Menu()
    {
    description = "pricea";
    price = 0.00;


    }
    public Menu(string item, double priceA)
    {
    Description = "menu item";
    Price = 0.00;
    }

    public string Description
    {
    get
    {
    return description;
    }
    set
    {
    description = value;
    }
    }
    public double Price
    {
    get
    {
    return price;
    }
    set
    {
    price = value;
    }

    }


    }
    class program //This class houses the program methods and utilizes them in the main method.
    {
    static void Main()
    /*This method will call the getData method to input data from the input file.
    * It will then call the showMenu method to display menu options and allow selections.
    * Then it will call the printCheck method which will display the selected menu items and quantities as well as the total bill.
    */
    {
    Menu[] menuItems = new Menu[10]; //stores input data.

    int noMenuItems; //stores the number of Menu Items.
    int noOrderItems; //stores the number of items ordered.
    noMenuItems = GetData(menuItems);
    int[] order = new int[noMenuItems];
    // for (int w = 0; w < noMenuItems; w++)
    //Console.WriteLine(menuItems[w].Price);
    ShowMenu(menuItems, order, noMenuItems);


    }
    public static int GetData(Menu[] menuItems)
    //This method imports data from an input file the user specifies and stores it in an array
    {
    int i = 0; //loop variable

    string fileName; //stored input file name.
    string recordIn; //will store input line by line.
    string itemString, priceString;
    double price; //Used to convert numeric input.

    Console.WriteLine("Enter the full filename you wish to import menu from.");
    fileName = Console.ReadLine();

    if (File.Exists(fileName)) //data validation ensuring proper file name and then imports data
    {
    Console.WriteLine("Entry accepted; Input in progress");
    Console.WriteLine(" ");
    FileStream readFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(readFile);
    recordIn = reader.ReadLine();
    i = 0;
    price = 0;
    itemString = ""; //variables initialized

    while (recordIn != null) //Loop reads data line by line and storing into the array.
    {


    itemString = recordIn;
    recordIn = reader.ReadLine();
    price = Convert.ToDouble(recordIn);
    Menu a = new Menu();
    a.Description = itemString;
    a.Price = price;
    //creates new object with input item and price

    menuItems[i] = a; //stores new object in array position i

    i++;
    recordIn = reader.ReadLine();


    }
    reader.Close(); //Ceases reading file.
    readFile.Close(); //Closes input file.

    }
    else //prompts user until proper filename is entered and then imports data
    {
    do
    {
    Console.WriteLine("File not found, check filepath and re-enter.");
    fileName = Console.ReadLine();

    }
    while (File.Exists(fileName) != true);


    FileStream readFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(readFile);
    recordIn = reader.ReadLine();
    price = 0;
    i = 0;
    itemString = "";


    while (recordIn != null) //Loop reads data line by line and storing into the array.
    {

    itemString = recordIn;
    recordIn = reader.ReadLine();
    price = Convert.ToDouble(recordIn);
    Menu a = new Menu(itemString, price); //creates new object with input item and price

    menuItems[i] = a; //stores new object in array position i

    i++;
    recordIn = reader.ReadLine();

    }
    reader.Close(); //Ceases reading file.
    readFile.Close(); //Closes input file.


    }



    return i;
    //Returns number of elements in the array.



    }


    public static void ShowMenu(Menu[] menuItems, int[] order, int noMenuItems)
    {
    int k = 0; //loop variable.

    double inputNum = 0; //stores converted input string.
    string inputString = ""; //stores user entry values.


    for (int j = 0; j < noMenuItems; j++)
    {

    Console.WriteLine(j.ToString("D"), " ", menuItems[j].Description, " ", menuItems[j].Price);

    }
    }




    }
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Issue with object arrays and classes

    Please format your code so that it is legible by using code tags.

    Your problem is simple; you have an empty array. 'menuItems' is an array of ten 'Menu" objects, but you never actually add anything to it, so it is really just an array with ten empty buckets that are large enough to hold 'Menu' objects.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Issue with object arrays and classes

    Out of curiosity, is this 'real' code or an assignment from a programming class? I ask because, if it's for a real project there are many better ways of dealing with a collection than using a raw array.

    Edit: nevermind - I see where you mentioned it's a school assignment. (too bad - you don't get to use the generic collection classes <g>)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured