FunctionX Logo

C# Examples: Ice Cream

 

Introduction

This is an example that contains arrays and displays menus to process ice cream order processing:

using System;

class IceCream
{
	public const decimal BasePrice = 1.55M;
	
	int ChoiceFlavor;
	int ChoiceContainer;
	int ChoiceIngredient;
	int Scoops;
	decimal TotalPrice;

	private string[] PrepareFlavors()
	{
		string[] Flavors = new string[10];

		Flavors[0] = "Vanilla";
		Flavors[1] = "Cream of Cocoa";
		Flavors[2] = "Chocolate Chip";
		Flavors[3] = "Organic Strawberry";
		Flavors[4] = "Butter Pecan";
		Flavors[5] = "Cherry Coke";
		Flavors[6] = "Chocolate Brownies";
		Flavors[7] = "Caramel Au Lait";
		Flavors[8] = "Chunky Butter";
		Flavors[9] = "Chocolate Cookie";

		return Flavors;
	}

	private void CreateIngredients(ref string[] Ingredients)
	{
		Ingredients[0] = "No Ingredient";
		Ingredients[1] = "Peanuts";
		Ingredients[2] = "M & M";
		Ingredients[3] = "Cookies";
	}

	private void ChooseContainer(ref string[] Containers)
	{
		Containers[0] = "Cone";
		Containers[1] = "Cup";
		Containers[2] = "Bowl";

		do 
		{
			try 
			{
				Console.WriteLine("What type of container do you want?");
				for(int i = 0; i < 3; i++)
					Console.WriteLine("{0} - {1}", i+1, Containers[i]);
				Console.Write("Your Choice? ");
				ChoiceContainer = int.Parse(Console.ReadLine());
			}
			catch(FormatException)
			{
				Console.WriteLine("You must enter a valid number and no other character!");
			}
				
			if( ChoiceContainer < 1 || ChoiceContainer > 3 )
				Console.WriteLine("Invalid Choice - Try Again!");
		} while( ChoiceContainer < 1 || ChoiceContainer > 3 );
	}

	private void RequestFlavor(ref string[] Flavors)
	{
		Flavors = PrepareFlavors();

		do 
		{
			try 
			{
				Console.WriteLine("What type of flavor do you want?");
				for(int i = 0; i < 10; i++)
					Console.WriteLine("{0} - {1}", i+1, Flavors[i]);
				Console.Write("Your Choice? " );
				ChoiceFlavor = int.Parse(Console.ReadLine());
			}
			catch(FormatException)
			{
				Console.WriteLine("You must enter a valid number and no other character!");
			}
				
			if( ChoiceFlavor < 1 || ChoiceFlavor > 10 )
				Console.WriteLine("Invalid Choice - Try Again!\n");
		} while( ChoiceFlavor < 1 || ChoiceFlavor > 10 );
	}

	private void ProcessIngredients(ref string[] Ingredients)
	{
		CreateIngredients(ref Ingredients);
		
		do 
		{
			try 
			{
				Console.WriteLine("Do you want an ingredient or not");
				for(int i = 0; i < 4; i++)
					Console.WriteLine("{0} - {1}", i+1, Ingredients[i]);
				Console.Write("Your Choice? ");
				ChoiceIngredient = int.Parse(Console.ReadLine());
			}
			catch(FormatException)
			{
				Console.WriteLine("You must enter a valid number and no other character!");
			}

			if( ChoiceIngredient < 1 || ChoiceIngredient > 4 )
				Console.WriteLine("Invalid Choice - Try Again!");
		} while( ChoiceIngredient < 1 || ChoiceIngredient > 4 );
	}

	private void GetTheNumberOfScoops()
	{
		do
		{
			try 
			{
				Console.Write("How many scoops(1, 2, or 3)? ");
				Scoops = int.Parse(Console.ReadLine());
			}
			catch(FormatException)
			{
				Console.WriteLine("You must enter a valid number and no other character!");
			}

			if( Scoops < 1 || Scoops > 3 )
				Console.WriteLine("Invalid Choice - Try Again!");
		} while( Scoops < 1 || Scoops > 3 );
	}

	public void ProcessAnOrder()
	{
		decimal PriceIngredient, PriceScoop;

		string[] Flavor = new string[10];	
		string[] Container = new string[3];
		string[] Ingredient = new string[4];

		RequestFlavor(ref Flavor);
		ChooseContainer(ref Container);
		ProcessIngredients(ref Ingredient);
		GetTheNumberOfScoops();

		if( ChoiceIngredient == 2 || ChoiceIngredient == 3 || ChoiceIngredient == 4 )
			PriceIngredient = 0.50M;
		else
			PriceIngredient = 0.00M;

		if( Scoops == 1 )
			PriceScoop = 0.65M;
		else if( Scoops == 2 )
			PriceScoop = 1.05M;
		else
			PriceScoop = 1.55M;

		Console.WriteLine("Ice Cream Vendor Machine");

		TotalPrice = BasePrice + PriceScoop + PriceIngredient;

		DisplayReceipt(ref Flavor, ref Container, ref Ingredient);
	}

	public void DisplayReceipt(ref string[] Flv, ref string[] Cont, ref string[] Ingrd)
	{
		Console.WriteLine("\nIce Cream Order");
		Console.WriteLine("Flavor:      {0}", Flv[ChoiceFlavor]);
		Console.WriteLine("Container:   {0}", Cont[ChoiceContainer]);
		Console.WriteLine("Ingredient:  {0}", Ingrd[ChoiceIngredient]);
		Console.WriteLine("Scoops:      {0}",   Scoops);
		Console.WriteLine("Total Price: {0:C}\n", TotalPrice);
	}
}
 

Home Copyright 2004 FunctionX, Inc.