I have a number of pizzas. that number is defined by the user. the for loop will run for that length of time. each time the for loop runs I want a new class for that pizza to be made. in order to do this (as far as I know) the names need to be different. I want the default name to be "pizza" and then for each reiteration of the for loop I want it to tack on a number to pizza. i.e "pizza1" "pizza2"
using System;
namespace PizzaCalculator { public class Pizza { public double size{get;set;} public double price{get;set;} public double squarInch; public double pricePerInch; public double radius; public double radius2; public string brand{get;set;}
public void calculate()
{
radius = (size/2);
radius2 = Math.Pow(radius, 2);
squarInch = radius2*3.14159;
pricePerInch = Math.Round((price/squarInch),2);
}
}
class Program
{
static void Main(string[] args)
{
double pizzaNum;
Console.WriteLine("How many pizza's do you want to compair?");
pizzaNum = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < pizzaNum; i )
{
Console.Write("\nPlease enter the brand of the pizza: ");
Pizza pizza = new Pizza() {brand = Console.ReadLine()};
}
Console.ReadKey();
}
}
}
I've tried to attach [i] to the end of "pizza" but I don't really know for sure how to go about doing this.
CodePudding user response:
You can use a List<Pizza>
to store your pizzas.
List<Pizza> pizzas = new List<Pizza>();
for (int i = 0; i < pizzaNum; i )
{
Console.Write("\nPlease enter the brand of the pizza: ");
Pizza pizza = new Pizza() {brand = Console.ReadLine()};
pizzas.Add(pizza);
}
To read the data, you can use a foreach loop:
foreach(Pizza pizza in pizzas)
{
Console.WriteLine(pizza.brand);
}
Or to access a specific pizza, you can use the indexer:
Console.WriteLine(pizzas[0].brand);