everyone. I am a beginner in C# and recently I have faced one task, the logic for which is difficult for me to find. It sounds like this: Create an enumeration with flags(!), which stores the names of 6 software products – 3 systems for accounting automation and 3 systems for logistics management. Display the list of systems using constants (number – name). Allow the user to enter the numbers of the software products he would like to purchase in the form of a list, separating the numbers with commas. Print the name of the program author, lists of software products separately for each group (accounting/logistics).
The task itself is very easy, I wrote it quickly before. However, the task is to use flags, and this is where I got stuck. I would be very grateful for your help!
Here’s how the code looked like before enter image description here( I’d be happy to receive criticism of this code as well). But I need to do something like this code(it’s example):
{
class Program
{
[Flags]
enum Products
{
Not = 0b00000000,
Butter = 0b00000001,
Yogurt = 0b00000010,
Orange = 0b00000100,
Plum = 0b00001000,
Sausage = 0b00010000,
Ham = 0b00100000,
Milk = 0b00000011,
Fruit = Orange | Plum,
Meat = 0b00110000
}
static void Main(string[] args)
{
int goods;
while (true)
{
Console.WriteLine("Select product: ");
Console.WriteLine("1. Butter ");
Console.WriteLine("2. Yogurt ");
Console.WriteLine("3. Orange ");
Console.WriteLine("4. Plum ");
Console.WriteLine("5. Sausage ");
Console.WriteLine("6. Ham ");
string text = Console.ReadLine();
goods = Convert.ToInt16(text);
if (goods >= 1 && goods <= 6) { break; }
}
int product = (int)Math.Pow(2, goods - 1);
Console.WriteLine("Product selected: " + (Products)product);
Console.WriteLine("This product belongs to the group: ");
if (((Products)product & Products.Milk) != 0) //кон’юнкція
{
Console.Write(Products.Milk);
}
else
{
if (((Products)product & Products.Fruit) != 0)
{
Console.Write(Products.Fruit);
}
else
{
Console.Write(Products.Meat);
}
}
Console.ReadLine();
}
}
}
Дарина Дрига is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.