implement a C# class which can support below functionalities:
this class can manage a properties system
each property has below attributes: name, address, builder manager, contact phone, strata web site, maintainess fee per month
this class allow caller to add/update/delete/get the properties information
all the data can be persisted in a local JSON file
Assistant
Below is a C# implementation of a class that manages a properties system with the specified functionalities. The class allows adding, updating, deleting, and retrieving property information, and persists the data in a local JSON file.
csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
public class Property
{
public string Name { get; set; }
public string Address { get; set; }
public string BuilderManager { get; set; }
public string ContactPhone { get; set; }
public string StrataWebsite { get; set; }
public decimal MaintenanceFeePerMonth { get; set; }
}
public class PropertyManager
{
private const string FilePath = "properties.json";
private List<Property> properties;
public PropertyManager()
{
LoadProperties();
}
private void LoadProperties()
{
if (File.Exists(FilePath))
{
var json = File.ReadAllText(FilePath);
properties = JsonConvert.DeserializeObject<List<Property>>(json) ?? new List<Property>();
}
else
{
properties = new List<Property>();
}
}
private void SaveProperties()
{
var json = JsonConvert.SerializeObject(properties, Formatting.Indented);
File.WriteAllText(FilePath, json);
}
public void AddProperty(Property property)
{
properties.Add(property);
SaveProperties();
}
public void UpdateProperty(string name, Property updatedProperty)
{
var property = properties.FirstOrDefault(p => p.Name == name);
if (property != null)
{
property.Address = updatedProperty.Address;
property.BuilderManager = updatedProperty.BuilderManager;
property.ContactPhone = updatedProperty.ContactPhone;
property.StrataWebsite = updatedProperty.StrataWebsite;
property.MaintenanceFeePerMonth = updatedProperty.MaintenanceFeePerMonth;
SaveProperties();
}
else
{
throw new InvalidOperationException("Property not found.");
}
}
public void DeleteProperty(string name)
{
var property = properties.FirstOrDefault(p => p.Name == name);
if (property != null)
{
properties.Remove(property);
SaveProperties();
}
else
{
throw new InvalidOperationException("Property not found.");
}
}
public Property GetProperty(string name)
{
return properties.FirstOrDefault(p => p.Name == name);
}
public List<Property> GetAllProperties()
{
return properties.ToList();
}
}
Explanation:
Property Class:
Represents a property with attributes: Name, Address, BuilderManager, ContactPhone, StrataWebsite, and MaintenanceFeePerMonth.
PropertyManager Class:
Manages the collection of properties.
Uses a local JSON file (properties.json) to persist data.
Provides methods to add, update, delete, and retrieve properties.
Data Persistence:
LoadProperties(): Loads properties from the JSON file when the PropertyManager is initialized.
SaveProperties(): Saves the current list of properties to the JSON file after any modification.
Methods:
AddProperty(): Adds a new property to the list and saves it.
UpdateProperty(): Updates an existing property by name and saves the changes.
DeleteProperty(): Deletes a property by name and saves the changes.
GetProperty(): Retrieves a property by name.
GetAllProperties(): Returns a list of all properties.
Usage Example:
csharp
class Program
{
static void Main(string[] args)
{
var manager = new PropertyManager();
// Add a new property
var newProperty = new Property
{
Name = "Sunshine Apartments",
Address = "123 Main St",
BuilderManager = "John Doe",
ContactPhone = "555-1234",
StrataWebsite = "www.sunshineapartments.com",
MaintenanceFeePerMonth = 250.00m
};
manager.AddProperty(newProperty);
// Retrieve and display a property
var property = manager.GetProperty("Sunshine Apartments");
if (property != null)
{
Console.WriteLine($"Property: {property.Name}, Address: {property.Address}");
}
// Update a property
var updatedProperty = new Property
{
Address = "456 New St",
BuilderManager = "Jane Smith",
ContactPhone = "555-5678",
StrataWebsite = "www.newsunshineapartments.com",
MaintenanceFeePerMonth = 300.00m
};
manager.UpdateProperty("Sunshine Apartments", updatedProperty);
// Delete a property
manager.DeleteProperty("Sunshine Apartments");
}
}
This implementation ensures that property data is persisted locally in a JSON file and provides a simple API for managing properties. Make sure to include the Newtonsoft.Json NuGet package for JSON serialization and deserialization.