Reputation: 63
I am trying to parse the string that contains "Enter q to quit or the whole data as a comma delimited string using the following format Name,D,C,D:minQ or R:roast" to check if the user typed "D:" or "R:" and depending on which one, instantiate an object of a particular type, Decaf decafCoffee = new Decaf for "D": and Regular regCoffee = new Regular for "R:". What would be the easiest way to go about this?
Console.Write("Enter q to quit or the whole data as a comma delimited string using the following format Name,D,C,D:minQ or R:roast ");
string s = Console.ReadLine();
// Loop
while (!s.ToLower().Equals("q"))
{
string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // Trim?
string name = values[0];
string demand = (values[1]);
string cost = (values[2]);
string min = values[3];
// Check for > 0 and convert to numbers
float D = CheckDemand(demand);
float C = CheckCost(cost);
float M = CheckMin(min);
// Create object
Decaf decafCoffee = new Decaf
Upvotes: 0
Views: 106
Reputation: 12944
Decaf decafCoffee = null;
Roast roastCoffee = null;
if (min.StartsWith("D:"))
decafCoffee = new Decaf();
else if (min.StartsWith("R:"))
roastCoffee = new Roast();
else
// Give an error or something.
Upvotes: 1