INSTRUCTIONS: In C# (Do not U a class. Keep it all in the Main CS file) You will
ID: 3783694 • Letter: I
Question
INSTRUCTIONS: In C# (Do not U a class. Keep it all in the Main CS file)
You will be asking the user for a comma-separated list of items they wish to purchase.
a. Validate this to make sure it is not left blank.
b. Send this text string into the custom function TextToArray.
Create a custom function named TextToArray that will split apart the user’s list text string into an array of individual items.
a. Make sure to remove any spaces before or after the item itself. b. Return this array of items to be purchased to the Main method.
Once you have the string array of items back in your Main Method create a 2nd custom function called PromptForCosts.
a. This function should accept the string array of items.
b. Inside of this function create an array to hold the costs of these items.
i. Note the length of this Array or ArrayList must be depended on how many items the user typed in. AKA do NOT hard-code this.
c. Loop through the items array and prompt the user for the cost of each item in the array.
i. Validate that the user is typing in a valid response.
ii. Once it is valid store this cost inside of the cost array.
d. After you get each cost, Return this array to the Main.
Explanation / Answer
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the items to be purchased separated by commas");
String inp;
while(true)
{
inp=Console.ReadLine();
if(inp.Length==0)
{
Console.WriteLine("Enter some items to purchase");
continue;
}
else
break;
}
string[] ret=TextToArray(inp);
double[] cost_arr=PromptForCost(ret);
foreach(double j in cost_arr)
{
Console.WriteLine(j);
}
}
static string[] TextToArray(String s)
{
string[] arr=s.Split(',');
for(int j=0;j<arr.Length;j++)
{
arr[j]=arr[j].Trim();
}
return arr;
}
static double[] PromptForCost(string[] x)
{
double[] cost=new double[x.Length];
int i=0;
foreach(String item in x)
{
Console.WriteLine("Enter the cost for the item " + item);
String c=Console.ReadLine();
cost[i]=Convert.ToDouble(c);
i++;
}
return cost;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.