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 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
}
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
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.
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>)
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.