Answer the question
In order to leave comments, you need to log in
How to create a "database" in a separate class?
Hello.
I have some database. For example, the weight, length and material of each product.
"Брусок"
имеет следующие параметры:int weightOfProduct = 2; // Вес изделия в килограммах.
int lengthOfProduct = 50; // Длина изделия в сантиметрах.
string materialOfProduct = "Дерево"; // Материал изделия.
"Арматура"
имеет параметры:int weightOfProduct = 4;
int lengthOfProduct = 200;
string materialOfProduct = "Сталь";
"Гвоздь"
имеет параметры:int weightOfProduct = 1;
int lengthOfProduct = 25;
string materialOfProduct = "Сталь";
"Брусок" или "Арматура" или "Гвоздь"
class Products
{
public string weightOfProduct { get; set; } // Вес изделия в килограммах.
public int lengthOfProduct { get; set; } // Длина изделия в сантиметрах.
public int materialOfProduct { get; set; } // Материал изделия.
}
class Block : Products
{
public int weightOfProduct = 2;
public int lengthOfProduct = 50;
public string materialOfProduct = "Дерево";
}
class Armature : Products
{
public int weightOfProduct = 4;
public int lengthOfProduct = 200;
public string materialOfProduct = "Сталь";
}
class Nail : Products
{
public int weightOfProduct = 1;
public int lengthOfProduct = 25;
public string materialOfProduct = "Сталь";
}
"Брусок" или "Арматура" или "Гвоздь"
Answer the question
In order to leave comments, you need to log in
Product class (if you want to use get and set, do it right):
/// <summary>
/// Изделие.
/// </summary>
class Product
{
private int _weight; // Вес изделия в килограммах.
private int _length; // Длина изделия в сантиметрах.
private string _material; // Материал изделия.
/// <summary>
/// Вес изделия в килограммах.
/// </summary>
public int Weight
{
get
{
return _weight;
}
set
{
_weight = value;
}
}
/// <summary>
/// Длина изделия в сантиметрах.
/// </summary>
public int Length
{
get
{
return _length;
}
set
{
_length = value;
}
}
/// <summary>
/// Длина изделия в сантиметрах.
/// </summary>
public string Material
{
get
{
return _material;
}
set
{
_material = value;
}
}
/// <summary>
/// Конструктор класса.
/// </summary>
public Product(int weight, int length, string material)
{
_weight = weight;
_length = length;
_material = material;
}
}
Dictionary<string, Product> products = new Dictionary<string, Product>();
products.Add("Брусок", new Product(2, 50, "Дерево"));
products.Add("Арматура", new Product(4, 200, "Сталь"));
products.Add("Гвоздь", new Product(1, 25, "Сталь"));
string materialArmature = products["Арматура"].Material;
int blockLength = products["Брусок"].Length;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question