Read This
Read This
Creating a console project named “SellingProducts”, the source file “Program.cs” might be coded as follow:
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
Adding static methods: ViewSales, CreateSales, SearchSales, DeleteSales, and ExitProgram in the class Program.
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
Creating a new class, Menu, presenting a menu with a text and action to perform operation. The source file,
Menu.cs is coded as follow:
public class Menu
{
public string Text { get; set; }
public Action Action { get; set; }
}
Creating a new class, MenuBank, presenting a bank of menues to display for user to select. The source file,
MenuBank.cs is coded as follow:
public class MenuBank
{
public string Title { get; set; } = "";
public List<Menu> Menues { get; set; } = new();
In the class Program, add a new static method MenuSimulate(), and modify the Main() method as following:
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Selling Products");
MenuBank mainBank = new MenuBank()
{
Title = "Sales",
Menues =
{
new Menu() { Text = "Viewing", Action=ViewSales},
new Menu() { Text = "Creating", Action = CreateSales },
new Menu() { Text = "Searching", Action=SearchSales},
new Menu() { Text = "Deleting", Action = DeleteSales},
new Menu() { Text = "Exiting", Action = ExitProgram}
}
};
MenuSimulate(mainBank);
Build and run the program to test how the progrom operates with a given menu bank.
Create a new class Product representing a product. The source file, Product.cs, was coded as follow:
public class Product
{
protected static int count = 0;
public static string IdPrefix { get; set; } = "PRD";
public static string GetNewId() { return $"{IdPrefix}-{++count:00000000}"; }
public static Product Create()
{
Product prd = new Product(GetNewId());
return prd;
}
public static Product Create(string name, double price)
{
Product prd = new Product(GetNewId(), name, price);
return prd;
}
#region Products
private Dictionary<string, Product> dictPrds = new Dictionary<string, Product>();
Create a static method InitializeProducts() in the class Program, and modify the Main() method to initialize products to
be sold.
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Selling Products");
InitializeProducts();
MenuBank mainBank = new MenuBank()
...
}
...
Create a new class Sale representing a sale. The source file, Sale.cs, was coded as follow:
public class Sale
{
protected static int count = 0;
public static string IdPrefix { get; set; } = "SAL";
public static string GetNewId() { return $"{IdPrefix}-{++count:0000}"; }
#region Sales
private Dictionary<string, Sale> dictSales = new Dictionary<string, Sale>();
#region Product-Sales
private Dictionary<Product, List<Sale>> dictPrdSales =
new Dictionary<Product, List<Sale>>();
public List<Sale> GetFor(Product prd)
{
try
{
return dictPrdSales[prd];
}
catch (Exception)
{
return new List<Sale>();
}
}
public List<Product> AllProducts=> dictPrdSales.Keys.ToList<Product>();
#endregion
}
Create a static method InitializeSales() in the class Program, and modify the Main() method to initialize sales for some
products.
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Selling Products");
InitializeProducts();
InitializeSales();
MenuBank mainBank = new MenuBank()
...
}
...
In the class Program, create static methods: ViewAllSales(), ViewProductSales(), and ReturnBack()
In the class Program, modify the existing static method ViewSales()
internal class Program
{
...
static void ViewSales()
{
MenuBank bank = new MenuBank()
{
Title = "Viewing Sales",
Menues = {
new Menu() { Text = "ALL Sales", Action = ViewAllSales },
new Menu() { Text = "Sales by Products", Action = ViewProductSales },
new Menu() { Text = "Returning back", Action = ReturnBack }
}
};
MenuSimulate(bank);
}
...
private static void ReturnBack()
{
Console.WriteLine();
Console.WriteLine("Returning back...");
}
Build and run the program to test how the progrom operates on Viewing Sales.
After testing, let go to next steps.
In the class Program, create a new static method GetBrowseProduct() modify the existing static method CreateSales().
internal class Program
{
...
SaleManager.GetInstance().Add(prd, qty);
Console.WriteLine("Successfully created a new sale");
Console.Write("\nEsc to stop, any key for another new sale...");
if (Console.ReadKey(true).Key == ConsoleKey.Escape) break;
Console.WriteLine();
}
Console.WriteLine();
}
Build and run the program to test how the progrom operates on creating sales.
Build and run the program to test how the progrom operates on searching for sales.
Build and run the program to test how the progrom operates on deleting existing sales.
Run the program to test all operations to make sure it works property.