This code checks a string called breakfastHours
similar to
1567
which translates to Monday, Friday, Saturday, Sunday
.
1 = Monday
2 = Tuesday
....
7 = Sunday
The user then checks the boxes for the days they visited the club, which is all handled within jQuery later on, with the chkB{0}
tags.
#region BuildOpeningHours
private string BuildOpeningHours() {
StringBuilder sb = new StringBuilder();
string hoursPrefix = "Opening";
if (listing.ListingTypeParentName == "restaurants-dining") {
//display opening hours
if (!String.IsNullOrEmpty(listing.BreakfastOpeningDays) || !String.IsNullOrEmpty(listing.LunchOpeningDays) || !String.IsNullOrEmpty(listing.DinnerOpeningDays)) {
sb.Append(OpeningHoursTable(listing.BreakfastOpeningDays, listing.LunchOpeningDays, listing.DinnerOpeningDays, listing.LateOpeningDays));
} else if (!String.IsNullOrEmpty(listing.OpeningHours)) {
sb.AppendFormat("<p itemprop=\"openingHours\">{0}</p>", listing.OpeningHours);
}
} else if (listing.ListingTypeParentName == "accommodation") {
//display reception hours
if (!String.IsNullOrEmpty(listing.OpeningHours)) {
hoursPrefix = "Reception";
sb.AppendFormat("<p>{0}</p>", listing.OpeningHours);
}
} else {
//display opening hours
if (!String.IsNullOrEmpty(listing.OpeningHours)) {
sb.AppendFormat("<p>{0}</p>", listing.OpeningHours);
}
}
if (sb.Length > 0) {
return string.Format("<div id=\"opening-hours\"><h3>{2} {0} Hours</h3>{1}</div>", hoursPrefix, sb.ToString(), listing.ListingName);
} else {
return "";
}
}
private string OpeningHoursTable(string breakfastHours, string lunchHours, string dinnerHours, string lateHours) {
StringBuilder sb = new StringBuilder();
int mo = 0;
int tu = 0;
int we = 0;
int th = 0;
int fr = 0;
int sa = 0;
int su = 0;
sb.Append("<table class=\"openinghours\" cellpadding=\"0\" cellspacing=\"0\">");
sb.Append("<tr class=\"days\">");
sb.Append("<th class=\"void\"><a href=\"\" class=\"saveEdit\" id=\"save-openingHours\" data=\"hastwo\">Save</a></th>");
sb.Append("<th><span>M</span></th>");
sb.Append("<th>T</th>");
sb.Append("<th><span>W</span></th>");
sb.Append("<th>T</th>");
sb.Append("<th>F</th>");
sb.Append("<th>S</th>");
sb.Append("<th>S</th>");
sb.Append("</tr>");
//breakfast opening hours
sb.Append("<tr>");
sb.Append("<th>Bfast</th>");
char a1 = '1';
for (char c = '1'; c <= '7'; c++)
{
if (breakfastHours.Contains(c))
{
sb.AppendFormat("<td class=\"active-box\"><input id=\"chkB{0}\"style=\"margin: 0px;\" type=\"checkbox\" checked></td>",a1);
switch (c)
{
case '1': mo++; break;
case '2': tu++; break;
case '3': we++; break;
case '4': th++; break;
case '5': fr++; break;
case '6': sa++; break;
case '7': su++; break;
}
}
else
{
sb.AppendFormat("<td class=\"inactive-box\"><input id=\"chkB{0}\"style=\"margin: 0px;\" type=\"checkbox\"></td>",a1);
}
a1++;
}
sb.Append("</tr>");
//lunch opening hours
sb.Append("<tr>");
sb.Append("<th>Lunch</th>");
char a2 = '1';
for (char c = '1'; c <= '7'; c++)
{
if (lunchHours.Contains(c))
{
sb.AppendFormat("<td class=\"active-box\"><input id=\"chkL{0}\" style=\"margin: 0px;\" type=\"checkbox\" checked></td>", a2);
switch (c)
{
case '1': mo++; break;
case '2': tu++; break;
case '3': we++; break;
case '4': th++; break;
case '5': fr++; break;
case '6': sa++; break;
case '7': su++; break;
}
}
else
{
sb.AppendFormat("<td class=\"inactive-box\"><input id=\"chkL{0}\" style=\"margin: 0px;\" type=\"checkbox\"></td>", a2);
}
a2++;
}
sb.Append("</tr>");
//dinner opening hours
sb.Append("<tr>");
sb.Append("<th class=\"last\">Dinner</th>");
char a3 = '1';
for (char c = '1'; c <= '7'; c++)
{
if (dinnerHours.Contains(c))
{
sb.AppendFormat("<td class=\"active-box\"><input id=\"chkD{0}\" style=\"margin: 0px;\" type=\"checkbox\" checked></td>", a3);
switch (c)
{
case '1': mo++; break;
case '2': tu++; break;
case '3': we++; break;
case '4': th++; break;
case '5': fr++; break;
case '6': sa++; break;
case '7': su++; break;
}
}
else
{
sb.AppendFormat("<td class=\"inactive-box\"><input id=\"chkD{0}\" style=\"margin: 0px;\" type=\"checkbox\"></td>", a3);
}
a3++;
}
sb.Append("</tr>");
//late opening hours
sb.Append("<tr>");
sb.Append("<th class=\"last\">Late</th>");
char a4 = '1';
for (char c = '1'; c <= '7'; c++)
{
if (lateHours.Contains(c))
{
sb.AppendFormat("<td class=\"active-box\"><input id=\"chkS{0}\" style=\"margin: 0px;\" type=\"checkbox\" checked></td>", a4);
switch (c)
{
case '1': mo++; break;
case '2': tu++; break;
case '3': we++; break;
case '4': th++; break;
case '5': fr++; break;
case '6': sa++; break;
case '7': su++; break;
}
}
else
{
sb.AppendFormat("<td class=\"inactive-box\"><input id=\"chkS{0}\" style=\"margin: 0px;\" type=\"checkbox\"></td>",a4);
}
a4++;
}
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<p>Seasonal variations may occur.</p>");
sb.Append("<meta itemprop=\"openingHours\" content=\"");
sb.Append("\"/>");
return sb.ToString();
}
#endregion
mo
,tu
,we
,th
,fr
,sa
&su
are all integers to be sent back to the server later on.
This code is duplicated four times for Breakfast hours, Lunch hours, Dinner hours, Late hours.
sb
aStringBuilder
? Why not just include the entire method? Is it the shown snippet that's duplicated within the same method, or you have 4 almost-identical methods? What is (are) the method(s)'s name(s)? Is this ASP.NET? Which page event runs this code? With a little bit more [code] context, you could get some very meaty answers here! \$\endgroup\$a1
appears to be the same thing asc
. You have 3 times the same code, factorize.breakfastHours
appears to be days, not hours. Your switch case is not very pretty, a dict or an array would look better \$\endgroup\$