using System;
using System.Collections.Generic;
using System.Linq;
namespace WPH.DotNetInterview {
public class Appointment
{
public string Name { get; set; }
public DateTime StartTime { get; set; }
public int DurationInMinutes { get; set; }
public string Location { get; set; }
public Appointment(string name, DateTime startTime, int durationInMinutes, string location)
{
Name = name;
StartTime = startTime;
DurationInMinutes = durationInMinutes;
Location = location;
}
}
public class Calendar
{
private List<Appointment> appointments;
public Calendar()
{
appointments = new List<Appointment>();
}
public bool ScheduleAppointment(Appointment newAppointment)
{
if (IsOverlapping(newAppointment))
{
return false;
}
appointments.Add(newAppointment);
return true;
}
public void DeleteAppointment(string name)
{
// TODO: Implement method to delete appointment by name
appointments.RemoveAll(i => i.Name == name);
//throw new NotSupportedException("DeleteAppointment method is not implemented yet.");
}
private bool IsOverlapping(Appointment newAppointment)
{
// TODO: Implement method to check for overlapping appointments
//throw new NotSupportedException("IsOverlapping method is not implemented yet.");
var start = appointments.FindAll(i => i.StartTime.AddMinutes(i.DurationInMinutes) > newAppointment.StartTime);
if (start is not null) return true;
var end = appointments.FindAll(i => i.StartTime < newAppointment.StartTime.AddMinutes(newAppointment.DurationInMinutes));
if(end is not null) return true;
}
public List<Appointment> GetAppointments()
{
// TODO: Implement method to return sorted list of appointments
//throw new NotSupportedException("GetAppointments method is not implemented yet.");
return appointments.OrderBy(i => i.Name).ToList();
}
public List<Appointment> GetAppointmentsByLocation(string location)
{
// TODO: Implement method to return list of appointments for a specific location
//throw new NotSupportedException("GetAppointmentsByLocation method is not implemented yet.");
return appointments.FindAll(i => i.Location == location);
}
}
public class Program {
public static void Main(string[] args) {
RunTests();
}
static void RunTests()
{
TestScheduleAppointment_NoOverlap_ReturnsTrue();
TestScheduleAppointment_WithOverlap_ReturnsFalse();
TestDeleteAppointment_ExistingAppointment_DeletesSuccessfully();
TestGetAppointments_ReturnsSortedAppointments();
TestGetAppointmentsByLocation_ReturnsAppointmentsForLocation();
}
static void TestScheduleAppointment_NoOverlap_ReturnsTrue()
{
var calendar = new Calendar();
var appointment = new Appointment("Meeting", DateTime.Now, 60, "Office");
bool result;
try
{
result = calendar.ScheduleAppointment(appointment);
}
catch (NotSupportedException e)
{
Console.WriteLine($"TestScheduleAppointment_NoOverlap_ReturnsTrue: Failed ({e.Message})");
return;
}
Console.WriteLine($"TestScheduleAppointment_NoOverlap_ReturnsTrue: {(result ? "Passed" : "Failed")}");
}
static void TestScheduleAppointment_WithOverlap_ReturnsFalse()
{
var calendar = new Calendar();
var appointment1 = new Appointment("Meeting", DateTime.Now, 60, "Office");
var appointment2 = new Appointment("Follow-up", DateTime.Now.AddMinutes(30), 60, "Office");
bool result;
try
{
calendar.ScheduleAppointment(appointment1);
result = calendar.ScheduleAppointment(appointment2);
}
catch (NotSupportedException e)
{
Console.WriteLine($"TestScheduleAppointment_WithOverlap_ReturnsFalse: Failed ({e.Message})");
return;
}
Console.WriteLine($"TestScheduleAppointment_WithOverlap_ReturnsFalse: {(!result ? "Passed" : "Failed")}");
}
static void TestDeleteAppointment_ExistingAppointment_DeletesSuccessfully()
{
var calendar = new Calendar();
var appointment = new Appointment("Meeting", DateTime.Now, 60, "Office");
try
{
calendar.ScheduleAppointment(appointment);
calendar.DeleteAppointment("Meeting");
var appointments = calendar.GetAppointments();
Console.WriteLine($"TestDeleteAppointment_ExistingAppointment_DeletesSuccessfully: {(appointments.Count == 0 ? "Passed" : "Failed")}");
}
catch (NotSupportedException e)
{
Console.WriteLine($"TestDeleteAppointment_ExistingAppointment_DeletesSuccessfully: Failed ({e.Message})");
}
}
static void TestGetAppointments_ReturnsSortedAppointments()
{
var calendar = new Calendar();
var appointment1 = new Appointment("Meeting", DateTime.Now.AddHours(1), 60, "Office");
var appointment2 = new Appointment("Follow-up", DateTime.Now, 30, "Office");
List<Appointment> appointments;
try
{
calendar.ScheduleAppointment(appointment1);
calendar.ScheduleAppointment(appointment2);
appointments = calendar.GetAppointments();
}
catch (NotSupportedException e)
{
Console.WriteLine($"TestGetAppointments_ReturnsSortedAppointments: Failed ({e.Message})");
return;
}
var isSorted = appointments[0].Name == "Follow-up" && appointments[1].Name == "Meeting";
Console.WriteLine($"TestGetAppointments_ReturnsSortedAppointments: {(isSorted ? "Passed" : "Failed")}");
}
static void TestGetAppointmentsByLocation_ReturnsAppointmentsForLocation()
{
var calendar = new Calendar();
var appointment1 = new Appointment("Meeting", DateTime.Now, 60, "Office");
var appointment2 = new Appointment("Follow-up", DateTime.Now, 30, "Home");
List<Appointment> officeAppointments;
try
{
calendar.ScheduleAppointment(appointment1);
calendar.ScheduleAppointment(appointment2);
officeAppointments = calendar.GetAppointmentsByLocation("Office");
}
catch (NotSupportedException e)
{
Console.WriteLine($"TestGetAppointmentsByLocation_ReturnsAppointmentsForLocation: Failed ({e.Message})");
return;
}
var correctAppointment = officeAppointments.Count == 1 && officeAppointments[0].Name == "Meeting";
Console.WriteLine($"TestGetAppointmentsByLocation_ReturnsAppointmentsForLocation: {(correctAppointment ? "Passed" : "Failed")}");
}
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: