C
C#2y ago
GIGA BRAIN

❔ OOP Product Inventory Project

Hi I'm making a product inventory project to get better at OOP, and I have another class called Inventory where I have a method that calculates the value of the entire inventory. I'm trying to figure out how I would go about doing this, and so far my logic is whenever a new product is initialized, I want to add the quantity of that product and price of the product to a list so I can access that list in the Inventory class and then go about calculating the sum. My question is: for the constructor, when I initialize a new product in my Program.cs file, will it add the desired information to the list? I'm not sure if I'm doing it right.
48 Replies
GIGA BRAIN
GIGA BRAINOP2y ago
Product Inventory Project - Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand. Then create an inventory class which keeps track of various products and can sum up the inventory value.
Angius
Angius2y ago
Uh Price and quantity are already properties You can already access them
GIGA BRAIN
GIGA BRAINOP2y ago
oh so i don't even need a list in the first place
Angius
Angius2y ago
Yeah
GIGA BRAIN
GIGA BRAINOP2y ago
jeez haha i'll look into how to implement that then i guess in this case it would be good to have a list just so i can have all the products and their properties as a history reference?
Angius
Angius2y ago
Loop over the products in the inventory, then add product.Price * product.Quantity to some total Lastly, return the total You mean, historical quantities and prices of the product?
GIGA BRAIN
GIGA BRAINOP2y ago
I mean everything about the product price, ID, and quantity
Angius
Angius2y ago
Why? To be able to compare that the product used to cost 7.99 and now it costs 8.78?
GIGA BRAIN
GIGA BRAINOP2y ago
just so there is an option for the user to see everything at once
Angius
Angius2y ago
So.... not historical data You just want to place properties, that are already public, inside of some public list... to do what, exactly?
GIGA BRAIN
GIGA BRAINOP2y ago
ah now that you say it like that it doesn't make sense to do that i just want to have a nice interface when the user runs the program
Angius
Angius2y ago
Console.WriteLine($"Product with id {product.Id} costs {product.Price} and there's {product.Quantity} in stock.");
Console.WriteLine($"Product with id {product.Id} costs {product.Price} and there's {product.Quantity} in stock.");
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
yeah this is what i have in my program.cs
Angius
Angius2y ago
Ah, you're storing each product in a separate variable?
GIGA BRAIN
GIGA BRAINOP2y ago
But i feel like its redundant
Angius
Angius2y ago
Why not a list of products?
GIGA BRAIN
GIGA BRAINOP2y ago
exactly i think thats what i was trying to get at i want to add the product and its information to a list that has all the products
Angius
Angius2y ago
var products = List<Product>(){
new Product(...),
new Product(...),
// ...
};
var products = List<Product>(){
new Product(...),
new Product(...),
// ...
};
Then
foreach (var product in products)
{
Console.WriteLine($"Product with id {product.Id} costs {product.Price} and there's {product.Quantity} in stock.");
}
foreach (var product in products)
{
Console.WriteLine($"Product with id {product.Id} costs {product.Price} and there's {product.Quantity} in stock.");
}
to display all of them
GIGA BRAIN
GIGA BRAINOP2y ago
perfect
Angius
Angius2y ago
Lists and loops
GIGA BRAIN
GIGA BRAINOP2y ago
awesome gonna try and implement that now thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
GIGA BRAIN
GIGA BRAINOP2y ago
could someone point me in the right direction on how to calculate the total sum of my inventory?
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
GIGA BRAIN
GIGA BRAINOP2y ago
earlier in the post i was told to loop over each product in the inventory and then calculate the total fromthere but im not sure how I would do that since I already initialize a list of the products in my program.cs
GIGA BRAIN
GIGA BRAINOP2y ago
Stack Overflow
Access List from another class
can anyone tell me how to create a list in one class and access it from another?
GIGA BRAIN
GIGA BRAINOP2y ago
Would i create a list in product.cs and then somehow add each initialization of a product object to that list?
Angius
Angius2y ago
Keep the list of products in the inventory Not in the top level
GIGA BRAIN
GIGA BRAINOP2y ago
namespace OOP2
{
public class Inventory
{
// FIELDS:
public int NumberOfProducts { get; set; }
public List<Product> ProductList = new List<Product>
{
new Product("0001", "Macbook Pro", 100, 10000),
new Product("0002", "Iphone 13", 50, 100)
};

// CONSTRUCTOR:
public Inventory(int numProducts)
{
NumberOfProducts = numProducts;
}

// METHODS:
public decimal getInventorySum()
{
decimal totalSum = 0;
foreach (var product in ProductList)
{
totalSum += (product.Quantity * product.Price);
}
return totalSum;
}

public string getInventoryDetails()
{
var report = "";
foreach (var product in ProductList)
{
report = ($"Product #{product.Identification} costs ${product.Price} per unit. There are {product.Quantity} units available.");
}
return report;
}
}
}
namespace OOP2
{
public class Inventory
{
// FIELDS:
public int NumberOfProducts { get; set; }
public List<Product> ProductList = new List<Product>
{
new Product("0001", "Macbook Pro", 100, 10000),
new Product("0002", "Iphone 13", 50, 100)
};

// CONSTRUCTOR:
public Inventory(int numProducts)
{
NumberOfProducts = numProducts;
}

// METHODS:
public decimal getInventorySum()
{
decimal totalSum = 0;
foreach (var product in ProductList)
{
totalSum += (product.Quantity * product.Price);
}
return totalSum;
}

public string getInventoryDetails()
{
var report = "";
foreach (var product in ProductList)
{
report = ($"Product #{product.Identification} costs ${product.Price} per unit. There are {product.Quantity} units available.");
}
return report;
}
}
}
this makes a lot more sense and i implemented it, but now i'm confused on how I would call this
Angius
Angius2y ago
Instantiate Inventory And work with that instance Also, you never seem to use NumberOfProducts
GIGA BRAIN
GIGA BRAINOP2y ago
yeah i was just gonna talk about that i need to think on that one more actually but yes exactly i forgot to instantiate it thanks ok ive been struggling on how to implement NumberOfProducts in the code and i need some clarification please
GIGA BRAIN
GIGA BRAINOP2y ago
could I just do something like this?
GIGA BRAIN
GIGA BRAINOP2y ago
because from what i had originally i would have to manually say how many products there are and that doesn't really make sense im also not sure how I would add the list information to the constructor, do I even need to do that in the first place?
Angius
Angius2y ago
ProductList.Length List<T> has a Length property
GIGA BRAIN
GIGA BRAINOP2y ago
oooh ok i'll try and implement that how would I put that into the constructor? and did you mean the count function?
GIGA BRAIN
GIGA BRAINOP2y ago
Angius
Angius2y ago
Why do you need it in the constructor?
GIGA BRAIN
GIGA BRAINOP2y ago
to be honest im not even sure if i need a constructor i thought you did for oop
Angius
Angius2y ago
Every class has at least a default constructor, that's what allows you to do var f = new Foo(); But a constructor doesn't need to have any parameters
GIGA BRAIN
GIGA BRAINOP2y ago
oh ok so i can just leave the constructor blank?
Angius
Angius2y ago
Or you can just not have it A blank constructor is implicitly there
GIGA BRAIN
GIGA BRAINOP2y ago
gotcha yeah it works perfectly now 😄 well with the exception of number of products not being referenced i'm thinking I should just make it a method, what do you think?
Angius
Angius2y ago
You can make a pass-through property Or reference the list of items, and its .Length property, directly inventory.ProductsList.Length Or make a public int ProductCount => ProductsList.Length; property in the Inventory and use it with inventory.ProductCount
GIGA BRAIN
GIGA BRAINOP2y ago
ok trying to process the info i'll try these out
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OOP2
{
public class Inventory
{
// FIELDS:
public List<Product> ProductList = new List<Product>
{
new Product("0001", "Macbook Pro", 100, 10000),
new Product("0002", "Iphone 13", 50, 100)
};

// When not using a constructor, you don't even need to have one because there is already a blank one implicitly there
// // CONSTRUCTOR:
// public Inventory()
// {
// }

// METHODS:
public decimal getInventorySum()
{
decimal totalSum = 0;
foreach (var product in ProductList)
{
totalSum += (product.Quantity * product.Price);
}
return totalSum;
}

public string getInventoryDetails()
{
var report = new System.Text.StringBuilder();
foreach (var product in ProductList)
{
report.AppendLine($"Product #{product.Identification} costs ${product.Price} per unit. There are {product.Quantity} units available.");
}
return report.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OOP2
{
public class Inventory
{
// FIELDS:
public List<Product> ProductList = new List<Product>
{
new Product("0001", "Macbook Pro", 100, 10000),
new Product("0002", "Iphone 13", 50, 100)
};

// When not using a constructor, you don't even need to have one because there is already a blank one implicitly there
// // CONSTRUCTOR:
// public Inventory()
// {
// }

// METHODS:
public decimal getInventorySum()
{
decimal totalSum = 0;
foreach (var product in ProductList)
{
totalSum += (product.Quantity * product.Price);
}
return totalSum;
}

public string getInventoryDetails()
{
var report = new System.Text.StringBuilder();
foreach (var product in ProductList)
{
report.AppendLine($"Product #{product.Identification} costs ${product.Price} per unit. There are {product.Quantity} units available.");
}
return report.ToString();
}
}
}
i think i just like mentioning the property directly in the program.cs but other than that, i think the program is finished thanks as usual z, youre a real lifesaver
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server