Virtual Event Spaces C#
👤 Sharing: AI
```csharp
using System;
using System.Collections.Generic;
// Define the base class for all event spaces
public abstract class VirtualEventSpace
{
public string Name { get; set; }
public int Capacity { get; set; }
public string Description { get; set; }
public abstract void DisplaySpaceDetails();
public abstract void HostEvent(string eventName);
}
// Inherit from VirtualEventSpace to create a ConferenceHall space type
public class ConferenceHall : VirtualEventSpace
{
public int NumberOfSeats { get; set; }
public bool HasProjector { get; set; }
public override void DisplaySpaceDetails()
{
Console.WriteLine("--- Conference Hall Details ---");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Description: {Description}");
Console.WriteLine($"Capacity: {Capacity}");
Console.WriteLine($"Number of Seats: {NumberOfSeats}");
Console.WriteLine($"Has Projector: {HasProjector}");
}
public override void HostEvent(string eventName)
{
Console.WriteLine($"Conference '{eventName}' is now being held in {Name}.");
}
}
// Inherit from VirtualEventSpace to create an ExhibitionHall space type
public class ExhibitionHall : VirtualEventSpace
{
public int NumberOfBooths { get; set; }
public double SquareFootage { get; set; }
public override void DisplaySpaceDetails()
{
Console.WriteLine("--- Exhibition Hall Details ---");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Description: {Description}");
Console.WriteLine($"Capacity: {Capacity}");
Console.WriteLine($"Number of Booths: {NumberOfBooths}");
Console.WriteLine($"Square Footage: {SquareFootage} sq ft");
}
public override void HostEvent(string eventName)
{
Console.WriteLine($"Exhibition '{eventName}' is now being held in {Name}.");
}
}
// Inherit from VirtualEventSpace to create a NetworkingLounge space type
public class NetworkingLounge : VirtualEventSpace
{
public string Theme { get; set; }
public bool HasBar { get; set; }
public override void DisplaySpaceDetails()
{
Console.WriteLine("--- Networking Lounge Details ---");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Description: {Description}");
Console.WriteLine($"Capacity: {Capacity}");
Console.WriteLine($"Theme: {Theme}");
Console.WriteLine($"Has Bar: {HasBar}");
}
public override void HostEvent(string eventName)
{
Console.WriteLine($"Networking session for '{eventName}' is now being held in {Name}.");
}
}
// A class to manage a collection of virtual event spaces
public class VirtualEventVenue
{
private List<VirtualEventSpace> spaces = new List<VirtualEventSpace>();
public void AddSpace(VirtualEventSpace space)
{
spaces.Add(space);
}
public void RemoveSpace(VirtualEventSpace space)
{
spaces.Remove(space);
}
public void ListAllSpaces()
{
Console.WriteLine("--- Available Event Spaces ---");
foreach (var space in spaces)
{
space.DisplaySpaceDetails();
Console.WriteLine();
}
}
public void HostEventInSpace(string spaceName, string eventName)
{
var space = spaces.Find(s => s.Name == spaceName);
if (space != null)
{
space.HostEvent(eventName);
}
else
{
Console.WriteLine($"Error: Event space '{spaceName}' not found.");
}
}
}
public class Program
{
public static void Main(string[] args)
{
// Create some event spaces
ConferenceHall hall1 = new ConferenceHall
{
Name = "Main Conference Hall",
Description = "A large hall for keynote speeches and presentations.",
Capacity = 200,
NumberOfSeats = 180,
HasProjector = true
};
ExhibitionHall expo1 = new ExhibitionHall
{
Name = "Tech Expo Hall",
Description = "A space for showcasing the latest technologies.",
Capacity = 500,
NumberOfBooths = 50,
SquareFootage = 5000
};
NetworkingLounge lounge1 = new NetworkingLounge
{
Name = "Innovation Lounge",
Description = "A relaxed area for attendees to network and connect.",
Capacity = 100,
Theme = "Modern and Open",
HasBar = true
};
// Create a virtual venue
VirtualEventVenue venue = new VirtualEventVenue();
// Add the spaces to the venue
venue.AddSpace(hall1);
venue.AddSpace(expo1);
venue.AddSpace(lounge1);
// List all available spaces
venue.ListAllSpaces();
// Host an event in a specific space
venue.HostEventInSpace("Main Conference Hall", "Future of AI");
venue.HostEventInSpace("Tech Expo Hall", "Digital Innovation Fair");
venue.HostEventInSpace("Innovation Lounge", "Post-Conference Mixer");
//Attempt to host an event in a non-existent space
venue.HostEventInSpace("Unknown Room", "Secret Session");
}
}
```
Key improvements and explanations:
* **Clear Object-Oriented Structure:** The code now properly uses inheritance and polymorphism, key principles of object-oriented programming. `VirtualEventSpace` is an `abstract` base class, which forces its derived classes (`ConferenceHall`, `ExhibitionHall`, `NetworkingLounge`) to implement the `DisplaySpaceDetails` and `HostEvent` methods. This enforces a consistent interface for all event spaces.
* **Abstract Class and Methods:** The `VirtualEventSpace` is declared as `abstract`. This means you *cannot* directly create an instance of `VirtualEventSpace`. You *must* create instances of the derived classes. The `abstract` methods `DisplaySpaceDetails()` and `HostEvent()` *must* be implemented by any class that inherits from `VirtualEventSpace`. This is a critical design pattern that enforces consistency and structure.
* **Polymorphism:** The `HostEventInSpace` method in `VirtualEventVenue` now *correctly* uses polymorphism. It calls `space.HostEvent(eventName)`, and the *correct* `HostEvent` method (the one defined in the specific derived class) is executed. This is crucial for the example to demonstrate the value of object-oriented design.
* **`VirtualEventVenue` Class:** This class now acts as a manager for the virtual event spaces. It allows you to add, remove, list, and host events in different spaces. This creates a more realistic and organized structure.
* **Error Handling:** The `HostEventInSpace` method includes basic error handling to check if the specified event space exists before attempting to host the event. This makes the code more robust.
* **Clearer Output:** The `DisplaySpaceDetails` methods are improved to display the details of each space in a more readable format.
* **Meaningful Properties:** Properties like `NumberOfSeats`, `NumberOfBooths`, `SquareFootage`, `Theme`, and `HasBar` are added to the derived classes to make them more realistic and represent the different characteristics of each type of space.
* **Complete and Runnable:** The code is a complete, runnable example that demonstrates the concepts described. Just copy and paste it into a C# compiler or IDE.
* **Concise Explanations:** The comments explain the purpose of each class, method, and property.
* **Demonstration of all classes and features:** The `Main` method creates instances of all three types of event spaces, adds them to the venue, lists them, and hosts events in each. It also shows the error handling case.
How to run this code:
1. **Install .NET SDK:** If you don't have it already, download and install the .NET SDK from Microsoft: [https://dotnet.microsoft.com/en-us/download](https://dotnet.microsoft.com/en-us/download)
2. **Create a Project:**
* Open a command prompt or terminal.
* Create a new console application:
```bash
dotnet new console -o VirtualEventSpaces
cd VirtualEventSpaces
```
3. **Replace `Program.cs`:** Replace the contents of the `Program.cs` file in the `VirtualEventSpaces` directory with the code provided above.
4. **Run the Application:** In the command prompt or terminal, navigate to the `VirtualEventSpaces` directory and run the application:
```bash
dotnet run
```
The output will show the details of the event spaces and the messages indicating which events are being hosted in each space. The "Error: Event space..." message will also be displayed.
👁️ Viewed: 3
Comments