I have multiple classes that interact with each other and I got them to work and its diplaying properly but I cant help but feel that theres an optimal, cleaner and maybe even simpler way of coding this. Function is simple that it displays Product Name, Duration till Sold and Cost.
public class Item
{
private string name;
private int duration;
private int cost
public Item(string n, int d, double c)
{
Name = n;
Duration = d;
Cost = c;
}
}
Second class:
class Inventory
{
private List<Item> item = new List<item>();
public void AddItem(Item p) // Add item
{
item.Add(p);
}
public Item this[int i]
{
get
{
return item[i];
}
set
{
item[i] = value;
}
}
}
In my Form.cs I got this:
private void Form1_Load(object sender, EventArgs e)
{
{
var myInventory = new Inventory();
var i1 = new Item("iPod", 200, 9);
var i2 = new Item("Samsung", 700, 5);
var i3 = new Item("Nokia", 100, 17);
var i4 = new Item("Motorolla", 50, 50);
myInventory.AddItem(i1);
myInventory.AddItem(i2);
myInventory.AddItem(i3);
myInventory.AddItem(i4);
lstProduct.Items.Add(myInventory[0]); // Add items into listbox via indexing
lstProduct.Items.Add(myInventory[1]);
lstProduct.Items.Add(myInventory[2]);
lstProduct.Items.Add(myInventory[3]);
}
Completely new to programming so Im still learning. Im mainly concerned with my Form.cs and the way I coded it but I welcome any feedback/suggestions if you have a better idea on how to code the other classes I made.
Thanks !