.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

C# Examples: Ice Cream

Posted By:papsantony       Posted Date: August 13, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

C# Examples: Ice Cream
 

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);
}
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend