The following snippet is a console application for an inventory system that keeps track of cars in stock and will automatically restock/order when inventory has a certain number of cars left.
using System;
namespace Test
{
public class CarStock
{
public string carModel;
private int stock;
public delegate void LowOnStock(object sender, EventArgs e);
public event LowOnStock LowStock;
public CarStock(string modelName, int inventory)
{
carModel = modelName;
stock = inventory;
}
public void ReduceStock(CarStock c, int numOfSales)
{
if (c.stock >= numOfSales)
{
c.stock = c.stock - numOfSales;
Console.WriteLine("There are {0} cars remaining in stock.", stock);
if (c.stock <= 5)
{
LowStock?.Invoke(this, EventArgs.Empty);
ReorderStock(c);
}
}
else
{
Console.WriteLine("Invalid input. The number of sales cannot be greater than inventory available.");
}
}
public void ReorderStock(CarStock c)
{
int order = 0;
while (true)
{
Console.WriteLine("How many cars do you want to reorder?");
order = Convert.ToInt32(Console.ReadLine());
if (Program.IsPositiveInteger(order) == true)
{
break;
}
else
{
Console.WriteLine("Error: Invalid input. Please, try again.");
}
}
c.stock += order;
Console.WriteLine("You now have {0} {1} cars", c.stock, c.carModel);
}
}
public class Counter
{
private string CounterName;
public Counter(string Name)
{
CounterName = Name;
}
public void Sales(CarStock c, int amount)
{
Console.WriteLine("You have sold {0} {1} cars.", amount, c.carModel);
c.ReduceStock(c, amount);
}
public void LowStockHandler(object Sender, EventArgs e)
{
Console.WriteLine("Notice: There is currently low stock for {0} cars.", ((CarStock)Sender).carModel);
}
}
class Program
{
public static bool IsPositiveInteger(int number)
{
if (number > 0 && (number % 1) == 0)
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
Counter billingCounter1 = new Counter("Richard");
int currentStock = 0;
int soldCars = 0;
Console.WriteLine("How many cars do you have in stock?");
while (true)
{
try
{
var input = Console.ReadLine();
currentStock = int.Parse(input);
if (currentStock <= 0)
{
Console.WriteLine("Error: Please enter a positive number. Try again.");
}
}
catch (FormatException)
{
Console.WriteLine("Error: Invalid input. Please, try again.");
}
if (IsPositiveInteger(currentStock) == true)
{
break;
}
}
CarStock carObject = new CarStock("Ford Pinto", currentStock);
carObject.LowStock += new CarStock.LowOnStock(billingCounter1.LowStockHandler);
while (true)
{
Console.WriteLine("How many cars did you sell?");
while (true)
{
try
{
var input = Console.ReadLine();
soldCars = int.Parse(input);
if (soldCars <= 0)
{
Console.WriteLine("Error: Please enter a whole number greater than 0.");
}
else if (soldCars > currentStock)
{
Console.WriteLine("Error: You cannot sell more cars than available stock.");
}
}
catch (FormatException)
{
Console.WriteLine("Error: Invalid input. Please, try again.");
}
if (IsPositiveInteger(soldCars) == true && soldCars <= currentStock)
{
break;
}
}
billingCounter1.Sales(carObject, soldCars);
}
}
}
}
If there are only 5 or less Ford Pintos left in stock, the LowStock event is triggered. The console application asks the user to enter a valid number to resupply the inventory with that number of Ford Pintos. When successful, the order is complete.
Testing output screenshots:

Note: This is a follow-up example project that also uses delegates and events.