2

Friends,

I' working Appointment booking Project, Details are as follows: Business hour starts from 9:00 to 7:00 with default duration of 30mins. So, Slots start like (9:00, 9:30, 10:00.... 7:00).

Here, to show the available slots, I'm using the following Logic.

  1. Storing all the Slots with 30 min duration in a list (LIST A) like [9:00, 9:30, 10:00, 10:30, ... 7:00]
  2. Looping through booked appointments (contains start and end time), and if start time is matched with any of LIST A elements, I', removing that element from that List. and Loop continues.

Here, the problem is, Consider If appointment is booked 9:30-10:00. Based on my logic, 9:30 is matched with LIST A element, and It will remove 9:30 from that list.

So, available slots will be displayed as [9:00, X ,10:00, 10:30, .... 7:00]. Actually It should be [9:00, 9:30, 10:30, 11:00... 7:00]

Instead of showing available slots 9:00-9:30, 10:30-11:00 it shows 9:00-10:00, 10:30-11 since 9:30 is removed from the list.,

Please help to solve this, or suggest me some alternative approaches for this problem. Badly needed.

Haavali
  • 221
  • 1
  • 4
  • 15

2 Answers2

2

The thing you are mixing up is, you are taking second slot's start time as first slot's end time. So rather then doing that, what you can do is to store start time and duration.

And to simply compute the end time, you do

StartTime.AddMinutes(30);

And to add one more comment at end; you are trying to build a very rigid structure. And will face problems if you'd try to extend the application, IMHO.

Yahya
  • 3,386
  • 3
  • 22
  • 40
2

I suggest, Instead of using Single Dimensional Array, use Multidimensional array like

[[9:00][9:30],[9:30][10:00],[10:00][10:30], .... nth Item] 

Here, Logic should be like this

var start=[start time]
var end=[end time]
var duration=[duration]

for (i=start;i<end;i+=duration)
{
   if(start==A[i][0])
   remove(A[i][0]);
}
A.sort();
return A; 
Unknown Coder
  • 1,510
  • 2
  • 28
  • 56