This is Q(6) of the incomplete program. Please complete this program.
using System;
namespace _8e6
{
class Program
{
static void Main(string[] args)
{
IceCreamCone icc1 = IceCreamConeProgram("Orange", 4);
IceCreamCone icc2 = IceCreamConeProgram("Apple", 5);
IceCreamCone icc3 = IceCreamConeProgram("Mango", 6);
Console.WriteLine(icc1.GetFlavor(), icc1.GetScoops());
Console.ReadKey();
}
static IceCreamCone IceCreamConeProgram(string flavor, int scoop)
{
IceCreamCone icc = null;
try
{
icc = new IceCreamCone(flavor, scoop);
}
catch (IceCreamConeException e)
{
Console.WriteLine(e.Message);
}
return icc;
}
class IceCreamConeException : Exception
{
public IceCreamConeException(string flavorAndNoOfScoops) : base(flavorAndNoOfScoops)
{
}
}
class IceCreamCone
{
string iceCreamFlavor;
int scoops;
public IceCreamCone(string iceCreamFlavor, int scoops)
{
this.iceCreamFlavor = iceCreamFlavor;
this.scoops = scoops;
}
public string GetFlavor()
{
if ((iceCreamFlavor != "vanilla") || (iceCreamFlavor != "chocolate") || (iceCreamFlavor != "strawberry") || (iceCreamFlavor != "peach") || (iceCreamFlavor != "banana"))
throw new IceCreamConeException(String.Format("Ice Cream Flavor {0}", iceCreamFlavor));
return this.iceCreamFlavor;
}
public int GetScoops()
{
if (scoops >= 3)
throw new IceCreamConeException(String.Format("No of Scoops {0}", scoops));
return this.scoops;
}
}
}
}
Loading
Posted Feb 15, 2012, 9:09 AM
VulpesPosted Feb 15, 2012, 8:58 AM
These would throw FormatExceptions if you're using Convert.ToInt32.
Posted Feb 15, 2012, 7:36 AM
VulpesPosted Feb 15, 2012, 4:57 AM
The second line will not have this effect and will, harmlessly, set scoops to zero if an invalid integer is entered.
Posted Feb 15, 2012, 2:18 AM
int scoops = Convert.ToInt32(Console.ReadLine());
AND
int.TryParse(Console.ReadLine(), out scoops);
Posted Feb 14, 2012, 4:58 PM
VulpesPosted Feb 14, 2012, 4:45 PM
Posted Feb 14, 2012, 4:34 PM
if ((iceCreamFlavor != "vanilla") && (iceCreamFlavor != "chocolate") && (iceCreamFlavor != "strawberry") && (iceCreamFlavor != "peach") && (iceCreamFlavor != "banana"))
if ((iceCreamFlavor != "vanilla") || (iceCreamFlavor != "chocolate") || (iceCreamFlavor != "strawberry") || (iceCreamFlavor != "peach") || (iceCreamFlavor != "banana"))
VulpesPosted Feb 14, 2012, 2:21 PM
Notice that you're supposed to get input from the user by calling the GetFlavor() and GetScoops() methods from the IceCreamCone class constructor: