C# Create a program for an Ice Cream Shop. The program will store data about ice
ID: 638677 • Letter: C
Question
C#
Create a program for an Ice Cream Shop. The program will store data about ice cream cones and customers.
Create a Cone class that includes a choice of ice cream or yogurt, the flavor, the number of scoops and type of cone
This class should also include static data members for the cost per scoop (.50), cost per ice cream cone (.75)
Create a Customer class that includes last name, first name and CustomerID number
Store the Customer data to a file. You may not have data about the same customer in your file more than once
Create an Order class that includes a Customer object and an array of 10 Cone objects (the maximum number of ice cream cones a customer can order is 10)
Display all order information and totals
Explanation / Answer
using System;
namespace IceCream3
{
// This class is used to create an Ice Cream
// And to process an order
sealed class IceCream
{
// This is the base price of an Ice Cream
// Optional values may be added to it
public const decimal BasePrice = 1.55M;
// These arrays are used to build
// the components of various Ice Creams
// In C#, we can allocate an array's memory in the body of the class
private string[] Flavor = new string[10];
private string[] Container;
private string[] Ingredient;
// Additional factor used to process an Ice Cream order
private int Scoops;
private decimal TotalPrice;
// This default constructor is the common place
// for us to initialize the array
public IceCream()
{
Flavor[0] = "Vanilla";
Flavor[1] = "Cream of Cocoa";
Flavor[2] = "Chocolate Chip";
Flavor[3] = "Organic Strawberry";
Flavor[4] = "Butter Pecan";
Flavor[5] = "Cherry Coke";
Flavor[6] = "Chocolate Brownies";
Flavor[7] = "Caramel Au Lait";
Flavor[8] = "Chunky Butter";
Flavor[9] = "Chocolate Cookie";
Ingredient = new string[4];
Ingredient[0] = "No Ingredient";
Ingredient[1] = "Peanuts";
Ingredient[2] = "M & M";
Ingredient[3] = "Cookies";
Container = new string[3];
Container[0] = "Cone";
Container[1] = "Cup";
Container[2] = "Bowl";
}
// This method requests a flavor from the user and returns the choice
public int ChooseFlavor()
{
int Choice = 0;
// Make sure the user selects a valid number that represents a flavor...
do
{
// In case the user types a symbol that is not a number
try
{
Console.WriteLine("What type of flavor do you want?");
for (int i = 0; i < Flavor.Length; i++)
Console.WriteLine("{0} - {1}", i + 1, Flavor[i]);
Console.Write("Your Choice? ");
Choice = int.Parse(Console.ReadLine());
}
catch (FormatException) // display an appropriate message
{
Console.WriteLine("You must enter a valid number and no other character!");
}
// If the user typed an invalid number out of the allowed range
// let him or her know and provide another chance
if (Choice < 1 || Choice > Flavor.Length)
Console.WriteLine("Invalid Choice - Try Again! ");
} while (Choice < 1 || Choice > Flavor.Length);
// Return the numeric choice that the user made
return Choice;
}
// This method allows the user to select a container
public int ChooseContainer()
{
int Choice = 0;
// Make sure the user selects a valid number that represents a container
do
{
// If the user types a symbol that is not a number
try
{
Console.WriteLine("What type of container do you want?");
for (int i = 0; i < Container.Length; i++)
Console.WriteLine("{0} - {1}", i + 1, Container[i]);
Console.Write("Your Choice? ");
Choice = int.Parse(Console.ReadLine());
}
catch (FormatException) // display an appropriate message
{
Console.WriteLine("You must enter a valid number and no other character!");
}
// If the user typed an invalid number out of the allowed range
// let him or her know and provide another chance
if (Choice < 1 || Choice > Container.Length)
Console.WriteLine("Invalid Choice - Try Again!");
} while (Choice < 1 || Choice > Container.Length);
// Return the numeric choice that the user made
return Choice;
}
public int ChooseIngredient()
{
int Choice = 0;
do
{
try
{
Console.WriteLine("Do you want an ingredient or not");
for (int i = 0; i < Ingredient.Length; i++)
Console.WriteLine("{0} - {1}", i + 1, Ingredient[i]);
Console.Write("Your Choice? ");
Choice = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("You must enter a valid number and no other character!");
}
if (Choice < 1 || Choice > Ingredient.Length)
Console.WriteLine("Invalid Choice - Try Again!");
} while (Choice < 1 || Choice > Ingredient.Length);
return Choice;
}
public void SpecifyNumberOfScoops()
{
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);
}
// This method is used to process a customer order
// It uses the values of the above methods
public void ProcessAnOrder()
{
int ChoiceFlavor;
int ChoiceContainer;
int ChoiceIngredient;
decimal PriceIngredient, PriceScoop;
// Let the user know that this is a vending machine
Console.WriteLine("Ice Cream Vending Machine");
// Let the user select the components of the Ice Cream
ChoiceFlavor = ChooseFlavor();
ChoiceContainer = ChooseContainer();
ChoiceIngredient = ChooseIngredient();
SpecifyNumberOfScoops();
// If the user selects an ingredient instead of "No Ingredient",
// add $0.50 to the order
if (ChoiceIngredient == 2 || ChoiceIngredient == 3 || ChoiceIngredient == 4)
PriceIngredient = 0.50M;
else
PriceIngredient = 0.00M;
// Instead of multiplying a number scoops to a value,
// We will use an incremental value depending on the number of scoops
if (Scoops == 1)
PriceScoop = 0.50;
else if (Scoops == 2)
PriceScoop = 1.00;
else
PriceScoop = 1.55M;
// Calculate the total price of the Ice Cream
TotalPrice = BasePrice + PriceScoop + PriceIngredient;
// Create the Ice Cream...
// And display a receipt to the user
DisplayReceipt(ref ChoiceFlavor, ref ChoiceContainer, ref ChoiceIngredient);
}
// This method is used to display a receipt to the user
public void DisplayReceipt(ref int Flv, ref int Cnt, ref int Igr)
{
Console.WriteLine(" Ice Cream Order");
Console.WriteLine("Flavor: {0}", Flavor[Flv - 1]);
Console.WriteLine("Container: {0}", Container[Cnt - 1]);
Console.WriteLine("Ingredient: {0}", Ingredient[Igr - 1]);
Console.WriteLine("Scoops: {0}", Scoops);
Console.WriteLine("Total Price: {0:C} ", TotalPrice);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.