This article focuses on extending the property pattern to the next level. The toll authority wants to add time-sensitive peak pricing for the final feature. Let’s write some code to identify the time slots using switch syntax with time in hours. The code makes use “**System.DaysOfWeek**”enum DayOfWeek.
This article focuses on extending the property pattern to the next level. Refer prerequisites section for the previous toll fare example used, and the following article extends that example further.
Prerequisites
Please review the article example below, which calculates a toll fare based on the vehicle type.
Advance Property Pattern C# 8.0The article describes how pattern matching provides an effective way to utilize and process that data in forms that…*medium.com
Recap of requirements already covered:
If the vehicle is Car => 100 Rs
If the vehicle is DeliveryTruck => 200 Rs
If the vehicle is Bus => 150 Rs
If the vehicle is Taxi => 120 Rs
Cars & taxis with “NO” passengers pay an extra 10 Rs.
Cars & taxis with two passengers get a 10 Rs discount.
Cars & taxis with three or more passengers get a 20 Rs discount.
Buses that are less than 50% of passengers pay an extra 30 Rs.
Buses with over 90% of passengers get a 40 Rs discount.
Trucks over 5000 lbs are charged an extra 100 Rs.
Light trucks under 3000 lbs, given a 20 Rs discount.
Let’s extend the property pattern.
The toll authority wants to add time-sensitive peak pricing for the final feature.
Extended Requirements
During peak hours, double the price; this rule applies in one direction, i.e., morning inbound traffic into the city and evening outbound traffic out of the city.
Late-night & early morning tolls are reduced by 25%.
During workday hours, the increasing toll is 50%.
During weekends, the toll price is constant concerning time.
Reference Table
Let’s write some code.
Let’s proceed with identifying each column of the reference table above.
Identify Day
Firstly the program should be aware of whether a particular day is a weekday or weekend. Refer new switch syntax.
The above code makes use “System.DaysOfWeek” enum.