C
C#•10mo ago
Jakub

Loop is returning the completely wrong value.

I am using the Linqpad to test a pricing algorithm but the result at the is completely wrong. Instead of returning the multiplier rate I am getting the normal rate even tho I am hitting the correct if statement... I am very lost with this, ive been staring at the debugger for a while so I hope someone here can point me in the right direction.
var transactionFeePercentage = 5M; // 5% processing fee (adjust as needed)
var processingFeePercentage = 10M; // 10% processing fee (adjust as needed)


var parkingBooking = new DateTime(12, 10, 7, 19, 0, 0);
var duration = TimeSpan.FromHours(2);

var bookingTotalMinutes = duration.TotalMinutes;
var bookingInterval = bookingTotalMinutes / 30;
var pricePerHour = 4M;

var subTotal = 0M;

parkingBooking.DayOfWeek.Dump();
parkingBooking.TimeOfDay.Dump();

for (int i = 0; i < bookingInterval; i++)
{
DateTime currentIntervalStart = parkingBooking.AddMinutes(i * 30);
DateTime currentIntervalEnd = currentIntervalStart.AddMinutes(30);

decimal intervalPrice = (decimal)30 / 60 * pricePerHour; // Calculate price for the current interval

if (currentIntervalStart.Hour >= 7 && currentIntervalStart.Hour < 14)
{
subTotal += intervalPrice; // Normal rate
}
else if ((currentIntervalStart.DayOfWeek == DayOfWeek.Friday || currentIntervalStart.DayOfWeek == DayOfWeek.Saturday)
&& currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21) {
subTotal += intervalPrice * 1.35M;
}
else if (currentIntervalStart.DayOfWeek == DayOfWeek.Sunday && (currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21))
{
subTotal += intervalPrice * 0.5M;
}
else if (currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21) {
subTotal += intervalPrice * 1.15M;
}
else if (currentIntervalStart.Hour >= 14 && currentIntervalStart.Hour < 17)
{
subTotal += intervalPrice * 1.75M; // Rush hour rate (adjust as needed)
}
else
{
subTotal += intervalPrice; // Quiet period rate (adjust as needed)
}

Console.WriteLine($"Normal Rate {subTotal} : Multiplier {subTotal * 0.5M}");
}

var totalCost = subTotal;
var transactionFee = totalCost * (transactionFeePercentage / 100);
var processingFee = totalCost * (processingFeePercentage / 100);

totalCost += transactionFee + processingFee;

Console.WriteLine("Sub Total: " + subTotal);
Console.WriteLine("Total Cost: " + totalCost);

var providerCharge = subTotal * 0.30M;
var providerCut = subTotal - providerCharge;

Console.WriteLine("Provider cut: " + providerCut);
Console.WriteLine("Parklink cut: " + (processingFee + transactionFee + providerCharge));
var transactionFeePercentage = 5M; // 5% processing fee (adjust as needed)
var processingFeePercentage = 10M; // 10% processing fee (adjust as needed)


var parkingBooking = new DateTime(12, 10, 7, 19, 0, 0);
var duration = TimeSpan.FromHours(2);

var bookingTotalMinutes = duration.TotalMinutes;
var bookingInterval = bookingTotalMinutes / 30;
var pricePerHour = 4M;

var subTotal = 0M;

parkingBooking.DayOfWeek.Dump();
parkingBooking.TimeOfDay.Dump();

for (int i = 0; i < bookingInterval; i++)
{
DateTime currentIntervalStart = parkingBooking.AddMinutes(i * 30);
DateTime currentIntervalEnd = currentIntervalStart.AddMinutes(30);

decimal intervalPrice = (decimal)30 / 60 * pricePerHour; // Calculate price for the current interval

if (currentIntervalStart.Hour >= 7 && currentIntervalStart.Hour < 14)
{
subTotal += intervalPrice; // Normal rate
}
else if ((currentIntervalStart.DayOfWeek == DayOfWeek.Friday || currentIntervalStart.DayOfWeek == DayOfWeek.Saturday)
&& currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21) {
subTotal += intervalPrice * 1.35M;
}
else if (currentIntervalStart.DayOfWeek == DayOfWeek.Sunday && (currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21))
{
subTotal += intervalPrice * 0.5M;
}
else if (currentIntervalStart.Hour >= 17 && currentIntervalStart.Hour < 21) {
subTotal += intervalPrice * 1.15M;
}
else if (currentIntervalStart.Hour >= 14 && currentIntervalStart.Hour < 17)
{
subTotal += intervalPrice * 1.75M; // Rush hour rate (adjust as needed)
}
else
{
subTotal += intervalPrice; // Quiet period rate (adjust as needed)
}

Console.WriteLine($"Normal Rate {subTotal} : Multiplier {subTotal * 0.5M}");
}

var totalCost = subTotal;
var transactionFee = totalCost * (transactionFeePercentage / 100);
var processingFee = totalCost * (processingFeePercentage / 100);

totalCost += transactionFee + processingFee;

Console.WriteLine("Sub Total: " + subTotal);
Console.WriteLine("Total Cost: " + totalCost);

var providerCharge = subTotal * 0.30M;
var providerCut = subTotal - providerCharge;

Console.WriteLine("Provider cut: " + providerCut);
Console.WriteLine("Parklink cut: " + (processingFee + transactionFee + providerCharge));
output:
Sunday
19:00:00
Normal Rate 1.00 : Multiplier 0.500
Normal Rate 2.00 : Multiplier 1.000
Normal Rate 3.00 : Multiplier 1.500
Normal Rate 4.00 : Multiplier 2.000
Sub Total: 4.00
Total Cost: 4.6000
Provider cut: 2.8000
Parklink cut: 1.8000
Sunday
19:00:00
Normal Rate 1.00 : Multiplier 0.500
Normal Rate 2.00 : Multiplier 1.000
Normal Rate 3.00 : Multiplier 1.500
Normal Rate 4.00 : Multiplier 2.000
Sub Total: 4.00
Total Cost: 4.6000
Provider cut: 2.8000
Parklink cut: 1.8000
Basically the problem I am having is that the return should contain the Multiplier rate since its Sunday and its 19. I'm not really sure why I am stilling getting the normal rate. Ive used the debugger and it seems to be hitting the correct if statement so idk why the value thats getting printed outside the loop is wrong.
9 Replies
i like chatgpt
i like chatgpt•10mo ago
You are using mtreit's avatar. 🙂
Angius
Angius•10mo ago
Debug it Not with LinqPad since it doesn't have a debugger But, yeah, debug it
Jakub
JakubOP•10mo ago
its working now : ) Cheers lads The premium version actually has a debugger lmao 🤣 Its super useful icl Clippy appreciation
Angius
Angius•10mo ago
Huh, I guess I just don't use Linqpad enough to need a debugger there lol
Jakub
JakubOP•10mo ago
Yeh thats actually by design, the only stuff I want to calculate is the subTotal * by the rate depending what time it is etc & day of the week Its like a scratchpad for me, I interact with the database with it etc draft quick stuff i cant live without it 🤣
Jakub
JakubOP•10mo ago
No description
i like chatgpt
i like chatgpt•10mo ago
What is DateTime currentIntervalEnd = currentIntervalStart.AddMinutes(30); for?
Jakub
JakubOP•10mo ago
its actually not being used tbf, i forgot to remove it before uploading this, my bad oh shit wait Do u think? What would u change them to? I'd definitely appreciate some advice, this is for my dissertation project Its an E-Parking solution
Want results from more Discord servers?
Add your server