i am working in one project related space booking.booked a space based on day/week/month with different price. i have created one class for calculate price either or like allow only hourly/day booking,some space avail for only day/month booking and some space allow for day/week and month and vice-versa.
public class PriceCalculation { /// <summary> /// Total Days /// </summary> public int TotalDays { get; } /// <summary> ///Open Days In One Week /// </summary> public int DaysInWeek { get; } /// <summary> /// Initializes a new instance of the PriceCalculation to the specified totalDays and daysInWeek /// </summary> /// <param name="totalDays">total number days</param> /// <param name="daysInWeek">total open day in week</param> public PriceCalculation(int totalDays,int daysInWeek) { TotalDays = totalDays; DaysInWeek = daysInWeek; } public double CalculateDailyPrice(double pricePerDay) { return TotalDays * pricePerDay; } public double CalculateWeeklyPrice(int pricePerWeek) { var week = TotalDays / DaysInWeek; return week * pricePerWeek; } public double CalculateMonthlyPrice(int pricePerMonth) { var month = TotalDays / (DaysInWeek * 4); return month * pricePerMonth; } public double CalculateDailyAndWeeklyPrice(double pricePerDay, int pricePerWeek) { var week = TotalDays / DaysInWeek; var extraDays = TotalDays % DaysInWeek; var week = TotalDays / DaysInWeek; var extraDays = TotalDays % DaysInWeek; return ((week * pricePerWeek) + (extraDays * pricePerDay)); } public double CalculateDailyAndMonthlyPrice(double pricePerDay, int pricePerMonth) { var month = TotalDays / (DaysInWeek * 4); var extradays = TotalDays - ((DaysInWeek * 4) * month); var totalPrice = month * pricePerMonth; return totalPrice + (extradays * pricePerDay); } public double CalculateWeeklyAndMonthlyPrice(int pricePerWeek, int pricePerMonth) { var month = TotalDays / (DaysInWeek * 4); var extradays = TotalDays - ((DaysInWeek * 4) * month); var totalPrice = month * pricePerMonth; var week = extradays / DaysInWeek; return totalPrice + (week * pricePerWeek); } public double CalculatePrice(double pricePerDay,int pricePerWeek, int pricePerMonth) { double totalPrice = 0; if (TotalDays >= (DaysInWeek * 4)) { var month = TotalDays / (DaysInWeek * 4); totalPrice = month * pricePerMonth; var extradays = TotalDays - ((DaysInWeek * 4) * month); if (extradays >= DaysInWeek) { var week = extradays / DaysInWeek; extradays = extradays % DaysInWeek; totalPrice = totalPrice + (week * pricePerWeek) + (extradays * pricePerDay); } else { totalPrice = totalPrice + (extradays * pricePerDay); } } else if (TotalDays >= DaysInWeek) { var week = TotalDays / DaysInWeek; var extraDays = TotalDays % DaysInWeek; totalPrice = (week * pricePerWeek) + (extraDays * pricePerDay); } else { totalPrice = (TotalDays * pricePerDay); } return totalPrice; } }
now if space only available for Day/Week booking then
PriceCalculation calculation = new PriceCalculation(8, 7);
for this situation i have called method for day/week calculation
totalPrice = calculation.CalculateDailyAndWeeklyPrice(88,165);//consider as 1 week and 1 day calculate.
this code working fine. now my question is in my code required changes for calculation based on
booking 7-26 days would be charged ($ 165 + (total days-7)*(week rate/7))1.5%
like above. now how to design my class for this pricecalculation and it not affect in future changes. please take look at into above code.