Use C# to solve this. Program Overview The program simulates a shopping cart by
ID: 3892946 • Letter: U
Question
Use C# to solve this.
Program Overview The program simulates a shopping cart by managing information about products. The application should prompt the user to enter the following three pieces of information for 5 items. It should record/save all three pieces of information for each of the 5 items.
Information to be recorded- Description- Valid Values
Product Name- The name of the product. This could be any term or phrase.- Any valid combination of letters and numbers
Product Category- The category describes the type of the product. There can be only 3 possible values of category. - “Tech” or “Grocery” or “Office”
Price- The price of the item in dollars. -Any positive Dollar value
After the user has entered information for all 5 items, the program should present a list of all items in the shopping cart sorted by category, i.e. all Tech items first, all Grocery items second, and all office items third. All items with an invalid category should be listed under “Other.” Each category listing should also provide a category total.
The output of the program should list all the items sorted by category as well as the average dollars spent on each category.
Sample Output
Requirements:
Name your class: “ShoppingCart.cs”
You must use arrays to store the information that the user enters for the products.
The program should recognize the product category even if the user enters it in improper casing, i.e. the program should recognize the category regardless of whether the input is in upper case, lower case, or mixed case.
You cannot use any other data structure for any purpose including storing, sorting, and calculation.
You must use for loops to traverse arrays.
The program must present a list of all items in each category. Items in invalid categories should be presented under one heading, i.e. “Other”.
The program must present an average of the dollar values for each category
Your program must calculate the average manually, i.e. no built-in functionality, methods, functions, etc. can be used to automate the calculation of the average. The set of statements which calculate the average should be visible in the program.
In case there are zero products in a category, the program should display an average of 0.
Dollar values should be formatted using Currency format specifier.
Explanation / Answer
public class ShoppingCart
{
public static void Main()
{
string[] name=new string[5];
float[] price=new float[5];
string[] category=new string[5];
int tec,gros,off,oth;
tec=gros=off=oth=0;
float tec_price,gros_price,off_price,oth_price;
tec_price=gros_price=off_price=oth_price=0;
string[] name_list=new string[4];
Console.WriteLine("This program maintains a list of products (name,category,price). It will alculate average doller spent per category for you ");
for(int i=0;i<5;i++)
{
Console.Write("Enter product Name:");
name[i]=Console.ReadLine();
Console.Write("Enter product categort(Tech,Grocery,Office):");
category[i]=Console.ReadLine();
Console.Write("Enter product Price:");
float.TryParse(Console.ReadLine(), out price[i]);
}
for(int i=0;i<5;i++)
{
string temp_category=category[i].ToLower();
switch(temp_category)
{
case "tech":
tec++;
tec_price+=price[i];
name_list[0]=name_list[0]+" "+name[i];
break;
case "grocery":
gros++;
gros_price+=price[i];
name_list[1]=name_list[1]+" "+name[i];
break;
case "office":
off++;
off_price+=price[i];
name_list[2]=name_list[2]+" "+name[i];
break;
default:
oth++;
oth_price+=price[i];
name_list[3]=name_list[3]+" "+name[i];
break;
}
}
Console.WriteLine("TECH PRODUCTS");
Console.WriteLine(name_list[0]);
Console.WriteLine("Average Doller Spent: $"+tec_price/tec);
Console.WriteLine(" GROCERY PRODUCTS");
Console.WriteLine(name_list[1]);
Console.WriteLine("Average Doller Spent: $"+gros_price/gros);
Console.WriteLine(" OFFICE PRODUCTS");
Console.WriteLine(name_list[2]);
Console.WriteLine("Average Doller Spent: $"+off_price/off);
Console.WriteLine(" OTHER PRODUCTS");
Console.WriteLine(name_list[3]);
Console.WriteLine(" Average Doller Spent: $"+oth_price/oth);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.