C
C#12mo ago
Jessie

❔ Assignment

Hi, pretty fresh to C# and im currently working on a assignment wich requires me to make a simple yet functional Inventory System utilizing interfaces, this is a console project, ive been stuck on this for the past few hours, currently my only issue would be how to turn a specific file into a public class so that i can mention it within the main file of the project. These are the files i currently have: IPanel.cs
interface IPanel
{
/// <summary>
/// Gere o input relacionado com o painel
/// </summary>
public void HandleInput();

/// <summary>
/// Função utilizada para imprimir os paineis na consola
/// </summary>
public void Draw();
}
interface IPanel
{
/// <summary>
/// Gere o input relacionado com o painel
/// </summary>
public void HandleInput();

/// <summary>
/// Função utilizada para imprimir os paineis na consola
/// </summary>
public void Draw();
}
Item.cs
public class Item
{
public string name;
public int stock;
public int price;

/// <summary>
/// Construtor da classe Item
/// </summary>
/// <param name="n">nome do item</param>
/// <param name="s">stock do item</param>
/// <param name="p">preço unitário do item</param>
public Item(string n, int s, int p)
{
this.name = n;
this.stock = s;
this.price = p;
}

//revisitar video de conversão de dados
public override string ToString()
{
//reservados 24 espaços para o nome dos items e 3 para o preço
return $"{name, 30} - {price, 5}$ x{stock}";
}
}
public class Item
{
public string name;
public int stock;
public int price;

/// <summary>
/// Construtor da classe Item
/// </summary>
/// <param name="n">nome do item</param>
/// <param name="s">stock do item</param>
/// <param name="p">preço unitário do item</param>
public Item(string n, int s, int p)
{
this.name = n;
this.stock = s;
this.price = p;
}

//revisitar video de conversão de dados
public override string ToString()
{
//reservados 24 espaços para o nome dos items e 3 para o preço
return $"{name, 30} - {price, 5}$ x{stock}";
}
}
17 Replies
Jessie
Jessie12mo ago
ItemShop.cs, this is the file i want turned into a public class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ItemShop
{
class Program
{
private static List<Item> items = new List<Item>();

static void Main(string[] args)
{
InsertItem();
Console.WriteLine("Greetings Traveller! Care to take a look at my wares?");
while (true)
{
PrintItem();
string userInput = Console.ReadLine();
Console.Clear();
Console.WriteLine("User Input " + userInput);
//verifies if user input is null or empty or not a number.
if (string.IsNullOrEmpty(userInput) || !userInput.All(char.IsDigit))
{
Console.WriteLine("Sorry, but you choose to buy something i dont have!");
continue;
}
//turns the value the player has inputed from string to int.
int id = int.Parse(userInput) - 1;
//checks if the item consists within the list
if (id < 0 || id >= items.Count)
{
Console.WriteLine("Excuse me? are you messing with me?");
continue;
}
RemoveFromStore(id);
if (items.Count <= 0)
{
Console.WriteLine("Sorry but im out of stock.");
return;
}
}
}

private static void InsertItem()
{
items.Add(new Item("Health Restorative", 5, 25));
items.Add(new Item("Mana Restorative", 5, 25));
items.Add(new Item("Health Restorative EX", 10, 50));
items.Add(new Item("Mana Restorative EX", 10, 50));
items.Add(new Item("Cursed Greatsword", 5, 250));
items.Add(new Item("Battle Axe", 5, 250));
items.Add(new Item("Strange Elixir", 50, 50));
items.Add(new Item("Giant Ring", 1, 2500));
items.Add(new Item("Spell: Wrath of the Gods", 1, 25000));
items.Add(new Item("Spell: Hidden Body", 1, 25000));
}

private static void PrintItem()
{
foreach (var i in items)
{
Console.WriteLine(items.IndexOf(i) + 1 + i.ToString());
}
}

public static void RemoveFromStore(int value)
{
if (items[value].stock.Equals(1))
{
items.RemoveAt(value);
return;
}
items[value].stock--;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ItemShop
{
class Program
{
private static List<Item> items = new List<Item>();

static void Main(string[] args)
{
InsertItem();
Console.WriteLine("Greetings Traveller! Care to take a look at my wares?");
while (true)
{
PrintItem();
string userInput = Console.ReadLine();
Console.Clear();
Console.WriteLine("User Input " + userInput);
//verifies if user input is null or empty or not a number.
if (string.IsNullOrEmpty(userInput) || !userInput.All(char.IsDigit))
{
Console.WriteLine("Sorry, but you choose to buy something i dont have!");
continue;
}
//turns the value the player has inputed from string to int.
int id = int.Parse(userInput) - 1;
//checks if the item consists within the list
if (id < 0 || id >= items.Count)
{
Console.WriteLine("Excuse me? are you messing with me?");
continue;
}
RemoveFromStore(id);
if (items.Count <= 0)
{
Console.WriteLine("Sorry but im out of stock.");
return;
}
}
}

private static void InsertItem()
{
items.Add(new Item("Health Restorative", 5, 25));
items.Add(new Item("Mana Restorative", 5, 25));
items.Add(new Item("Health Restorative EX", 10, 50));
items.Add(new Item("Mana Restorative EX", 10, 50));
items.Add(new Item("Cursed Greatsword", 5, 250));
items.Add(new Item("Battle Axe", 5, 250));
items.Add(new Item("Strange Elixir", 50, 50));
items.Add(new Item("Giant Ring", 1, 2500));
items.Add(new Item("Spell: Wrath of the Gods", 1, 25000));
items.Add(new Item("Spell: Hidden Body", 1, 25000));
}

private static void PrintItem()
{
foreach (var i in items)
{
Console.WriteLine(items.IndexOf(i) + 1 + i.ToString());
}
}

public static void RemoveFromStore(int value)
{
if (items[value].stock.Equals(1))
{
items.RemoveAt(value);
return;
}
items[value].stock--;
}
}
}
Program.cs, this is the main file, i turned it into a comment so that it would not break the code due to the itemshop file
/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace InventorySystem
{
class InventorySystem
{
static void Main(string[] args)
{

}
}
}
*/
/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace InventorySystem
{
class InventorySystem
{
static void Main(string[] args)
{

}
}
}
*/
cap5lut
cap5lut12mo ago
it seems like ur Item and IPanel types are not in a namespace, u should not do that. put them in a fitting namespace like ur other classes u can automatically access types in the same namespaces as if they would be written in the same file if the namespaces are different, u can use the using <namespace>; statement to import that namespace and then all types of that namespace are directly accessible by its name eg. if u have
namespace MyNamespaceA
{
public class MyClass {}
}
namespace MyNamespaceA
{
public class MyClass {}
}
u can use it somewhere else like this:
using MyNamespaceA; // import everything from MyNamespaceA

namespace MyNamespaceB
{
public class Program
{
public static void Main()
{
MyClass instance = new MyClass(); // here u can use the name directly
}
}
}
using MyNamespaceA; // import everything from MyNamespaceA

namespace MyNamespaceB
{
public class Program
{
public static void Main()
{
MyClass instance = new MyClass(); // here u can use the name directly
}
}
}
if Program would have been also in MyNamespaceA, or MyClass also in MyNamespaceB, u would not need the using at all another way would be to use the "full qualified name" MyNamespaceA.MyClass instance = new MyNamespaceA.MyClass(); tho that is quite long as u can see
Jessie
Jessie12mo ago
those didnt use namespaces cuz thats how i was given them and from what ive been told i cant really alter that i think although i could be wrong on that
SinFluxx
SinFluxx12mo ago
it feels like ItemShop should be a class as well, not a namespace?
Jessie
Jessie12mo ago
thats what i want to do i want to turn ItemShop into its own class so that i can reference it within program.cs
cap5lut
cap5lut12mo ago
well in case u are not allowed to alter their namespace, then just dont stuff Program into a namespace either u do not have a ItemShop class right now, just a namespace by that name
SinFluxx
SinFluxx12mo ago
Not sure why you moved the Program class out of Program.cs then 🙂
Jessie
Jessie12mo ago
what i want to do is change that, i want ItemShop to be a class while program.cs would be the only thing with a namespace i didnt, i imported a file from a project i used before, i was gonna adapt that file to this project
cap5lut
cap5lut12mo ago
if they are in the default namespace/no namespace, their full qualified name is just their name (i mean u r using them like that already in ur Program class)
Jessie
Jessie12mo ago
how would i get rid of the static void Main without fucking up everything tho? cuz every time i try to do that it jsut comepletely breaks everything
cap5lut
cap5lut12mo ago
do u mean top level statements?
using System;

Console.WriteLine("hello world!");
using System;

Console.WriteLine("hello world!");
and
using System;

public class Program
{
public static void Main()
{
Console.WriteLine("hello world!");
}
}
using System;

public class Program
{
public static void Main()
{
Console.WriteLine("hello world!");
}
}
are basically the same the first one will automatically be transformed into the second one under the hood
SinFluxx
SinFluxx12mo ago
I think probably it's because Program/Main are in ItemShop.cs, where they don't want them to be, and not in Program.cs - which is fully commented out
cap5lut
cap5lut12mo ago
so basically u could have the following
// Program.cs
using System;

Console.WriteLine("Inventory System");
ItemShop itemShop = new ItemShop();
itemShop.DoSomething();
// Program.cs
using System;

Console.WriteLine("Inventory System");
ItemShop itemShop = new ItemShop();
itemShop.DoSomething();
// ItemShop.cs
using System;

public class ItemShop : IPanel
{
// implement the interface instead of this method
public void DoSomething()
{
Console.WriteLine("doing something");
}
}
// ItemShop.cs
using System;

public class ItemShop : IPanel
{
// implement the interface instead of this method
public void DoSomething()
{
Console.WriteLine("doing something");
}
}
note that i made the DoSomething method public here, private stuff can only be accessed from inside the class where u define it, public allowes access from anywhere
Jessie
Jessie12mo ago
ok
cap5lut
cap5lut12mo ago
(i also assumed that ur ItemShop would implement the IPanel here, could be different) its after all just an example how to structure it ;p
Jessie
Jessie12mo ago
ill give that a shot, thx fo the help
Accord
Accord12mo 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
More Posts
❔ Is there osme way to check if a user is active/online with aspnet core identity? (cookie)Title is slightly incorrect: is there some way to check based on a users id if they are signed in* ❔ Storing user media from Instagram Basic Display APIHello, is there anyone who has worked with the Instagram Basic Display API? I've got it working howe✅ What is the purpose of `[FromServices]`Aren't dependencies already being injected from the services registered?❔ MediatR Issue with IRequest and Generic Method in ASP.NET CoreI'm facing an issue with MediatR in my ASP.NET Core project. I have an action method in a controller✅ What knowledge base is necessary to create a winforms app that communicates with a server?I'm trying to create a simple Winforms app to better understand how communications with servers work✅ MSI InstallersI am working on generate a msi installer using Visual Studio Installer Projects 2022. The idea is t✅ Does anyone have experience with SFML.NET (Graphics library), if so, are there any good guidesTitle explains it, please help, im trying to make a small 2D game with no engine (proof of concept +❔ Initializing an objectHello, what is the difference between these two lines? ```csharp var p = new Point() { X = 42, Y = 1❔ Trying to run operations off of a MVC APIEverything is loading properly, however this solution is very primitive because it uses a list insteCopy form with uiHow can I copy a form exactly as it is and put it into my current project. I have a login.cs, login.