C
C#17mo 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
JessieOP17mo 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
cap5lut17mo 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
JessieOP17mo 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
SinFluxx17mo ago
it feels like ItemShop should be a class as well, not a namespace?
Jessie
JessieOP17mo 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
cap5lut17mo 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
SinFluxx17mo ago
Not sure why you moved the Program class out of Program.cs then 🙂
Jessie
JessieOP17mo 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
cap5lut17mo 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
JessieOP17mo 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
cap5lut17mo 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
SinFluxx17mo 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
cap5lut17mo 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
JessieOP17mo ago
ok
cap5lut
cap5lut17mo 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
JessieOP17mo ago
ill give that a shot, thx fo the help
Accord
Accord17mo 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