Case Background
A courier company ships parcels in the local area. There are four zones in the company’s business area. The shipping fees are a certain percentage of the given item price. Some destinations are a bit dangerous or far from the company’s depot, which has an additional risk fee $25.
- Zone 1 – 25%
- Zone 2 – 12%. Zone 2 is a dangerous area and it imposes $25.
- Zone 3 – 8%
- Zone 4 – 4%. Zone 4 is a dangerous area and it imposes $25.
Approach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegatesSolution
{
class Program
{
static void Main(string[] args) {
ShippingFeesDelegate theDel;
ShippingDestination theDest;
string theZone;
do {
Console.WriteLine("What is the destination zone?");
theZone = Console.ReadLine();
if (!theZone.Equals("exit")) {
theDest = ShippingDestination.getDestinationInfo(theZone);
if (theDest != null) {
Console.WriteLine("What is the item price?");
string thePriceStr = Console.ReadLine();
decimal itemPrice = decimal.Parse(thePriceStr);
theDel = theDest.calcFees;
if (theDest.m_isHighRisk) {
theDel += delegate(decimal thePrice, ref decimal itemFee) {
itemFee += 25.0m;
};
}
//CALCULATION STARTS FROM HERE
decimal theFee = 0.0m;
theDel(itemPrice, ref theFee);
Console.WriteLine("The shipping fees are: {0}", theFee);
}
else {
Console.WriteLine("Hmm, you seem to have entered an uknown destination. Try again or 'exit'");
}
}
} while (theZone != "exit");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegatesSolution
{
public delegate void ShippingFeesDelegate(decimal thePrice, ref decimal fee);
abstract class ShippingDestination
{
public bool m_isHighRisk;
public virtual void calcFees(decimal price, ref decimal fee) {}
public static ShippingDestination getDestinationInfo(string dest) {
if (dest.Equals("zone1")) {
return new Dest_Zone1();
}
if (dest.Equals("zone2")) {
return new Dest_Zone2();
}
if (dest.Equals("zone3")) {
return new Dest_Zone3();
}
if (dest.Equals("zone4")) {
return new Dest_Zone4();
}
return null;
}
}
class Dest_Zone1 : ShippingDestination
{
public Dest_Zone1() {
this.m_isHighRisk = false;
}
public override void calcFees(decimal price, ref decimal fee) {
fee = price * 0.25m;
}
}
class Dest_Zone2 : ShippingDestination
{
public Dest_Zone2() {
this.m_isHighRisk = true;
}
public override void calcFees(decimal price, ref decimal fee) {
fee = price * 0.12m;
}
}
class Dest_Zone3 : ShippingDestination
{
public Dest_Zone3() {
this.m_isHighRisk = false;
}
public override void calcFees(decimal price, ref decimal fee) {
fee = price * 0.08m;
}
}
class Dest_Zone4 : ShippingDestination
{
public Dest_Zone4() {
this.m_isHighRisk = true;
}
public override void calcFees(decimal price, ref decimal fee) {
fee = price * 0.04m;
}
}
}
Study Note
In the Program.cs
, we use a basic structure as logic to implement the solution. The do-while
ensures the program running till use type in exit
.
string theZone;
do {
//Implementation
} while(theZone != "exit");
In the do-while
statement, we retrieve a ShippingDestination
object to represents a shipping zone. How do we do that? Have a look the following code snippet in ShippingInfo.cs
:
abstract class ShippingDestination
{
public bool m_isHighRisk;
public virtual void calcFees(decimal price, ref decimal fee) {}
public static ShippingDestination getDestinationInfo(string dest) {
if (dest.Equals("zone1")) {
return new Dest_Zone1();
}
if (dest.Equals("zone2")) {
return new Dest_Zone2();
}
if (dest.Equals("zone3")) {
return new Dest_Zone3();
}
if (dest.Equals("zone4")) {
return new Dest_Zone4();
}
return null;
}
}
We use keyword abstract
to declare an abstract class to prevent it from being instantiated by itself. We can’t create a ShippingDestination
class object; we have to create a subclass to represent a zone. This is an excellent approach for the cleaning code practice.
As a result, when user types in a zone, we will have a designated zone object as a subclass object of ShippingDestination
class.
In regard to shipping fee calculation, we use a delegate to calculate and manipulate the shipping fee for us. How?
In each of the ShippingDestination’s subclass, we have a method called public override void calcFees(decimal price, ref decimal fee)
. It shares the same signature with delegate public delegate void ShippingFeesDelegate(decimal thePrice, ref decimal fee)
.
We use a reference variable so that we can return the calculated value and it can be further manipulated by another delegate.
In the Program.cs file, we assign a specific zone’s fee calculate method to our delegate. It passes a reference variable, which can be further manipulated if needed.
When we need to calculate the fee in a dangerous area, we use an anonymous delegate in line to further modify shipping fee to include an additional $25.
theDel = theDest.calcFees;
if (theDest.m_isHighRisk) {
theDel += delegate(decimal thePrice, ref decimal itemFee) {
itemFee += 25.0m;
};
}
In the end, we define a local variable that will hold the result, then we invoke the delegate.
decimal theFee = 0.0m;
theDel(itemPrice, ref theFee);
Console.WriteLine("The shipping fees are: {0}", theFee);
Delegate is a heavily used techniques in development, it can take time to sink in. Good Luck 🙂 Happy programming.
Write more, thats all I have to say. Literally, it seems as though you
relied on the video to make your point. You definitely know what youre talking
about, why throw away your intelligence on just posting videos to your site when you could be giving us
something enlightening to read? https://parbrizeladomiciliu.ro/parbriz-nissan-navara.html
Incredible! This blog looks exactly like my old one!
It’s on a totally different topic but it has pretty much the same page layout
and design. Superb choice of colors! https://parbrize24ore.ro/oferta-parbrize.php?an=2010&produs=reparatie+parbriz&marca=FIAT&model=DOBLO+MPV+%28263_%29+&foto=parbriz-fiat.png
Incredible! This blog looks exactly like my old one! It’s on a totally different topic but it has pretty much the same page
layout and design. Superb choice of colors! https://parbrize24ore.ro/oferta-parbrize.php?an=2010&produs=reparatie+parbriz&marca=FIAT&model=DOBLO+MPV+%28263_%29+&foto=parbriz-fiat.png
Ahaa, its pleasant dialogue on the topic of this paragraph here
at this blog, I have read all that, so at this time me also commenting at this
place. https://parbriz-luneta.ro/Parbriz-jeep-cherokee-1994.html
You really make it appear so easy with your presentation but I to find this topic to be actually something which I think I might never understand.
It sort of feels too complex and extremely large for me.
I’m having a look forward on your subsequent post, I will
attempt to get the hold of it! https://parbriz-luneta.ro/Luneta-audi-a2-2001.html
Every weekend i used to pay a visit this web page, because i wish for enjoyment,
for the reason that this this web site conations in fact pleasant funny material too. https://anunturi-parbrize.ro/index.php?page=42463
I’m gone to tell my little brother, that he should also pay a visit this blog on regular basis to get updated
from latest gossip. https://anunturi-parbrize.ro/index.php?page=47157
Hi there to every body, it’s my first pay a quick visit of this blog;
this blog consists of awesome and really fine material in support of visitors. https://anunturi-parbrize.ro/index.php?page=13852
I do not even know how I ended up here, but I thought this post was good.
I don’t know who you are but definitely you are going to a famous
blogger if you aren’t already 😉 Cheers! https://parbrizeladomiciliu.ro/parbriz-opel-ampera.html
I will right away grasp your rss feed as I can’t find your e-mail subscription hyperlink or
newsletter service. Do you have any? Please permit me realize so that I could subscribe.
Thanks. https://parbrize24ore.ro/oferta-parbrize.php?an=2005&produs=reparatie+parbriz&marca=CITROEN&model=JUMPY+Box+%28BS_%2C+BT_%2C+BY_%2C+BZ_%29&foto=parbriz-citroen.png
I will right away grasp your rss feed as I can’t find your e-mail subscription hyperlink
or newsletter service. Do you have any? Please permit me realize so that
I could subscribe. Thanks. https://parbrize24ore.ro/oferta-parbrize.php?an=2005&produs=reparatie+parbriz&marca=CITROEN&model=JUMPY+Box+%28BS_%2C+BT_%2C+BY_%2C+BZ_%29&foto=parbriz-citroen.png
Uwielbiając informacje na tej stronie, wykonałeś świetną robotę przy artykułach.
Sprawdź ranking na chwilówka online i weź szybką pożyczkę internetową lub kredyt przez internet.
Necesita urgentemente un préstamo hipotecario o un gran préstamo de inversión, consulte nuestras listas de todas las ofertas de préstamos.
I’m just commenting to let you be aware of of the fabulous discovery my cousin’s daughter experienced going through your site. She even learned lots of issues, most notably how it is like to have a very effective giving mindset to have many others just gain knowledge of chosen grueling matters. You really did more than our desires. Many thanks for distributing the important, healthy, educational and as well as easy thoughts on the topic to Gloria.
I enjoy you because of all of your efforts on this site. Ellie enjoys doing internet research and it’s easy to see why. All of us hear all concerning the compelling manner you deliver functional ideas by means of the blog and as well cause contribution from other people about this concept plus my daughter is being taught a lot. Enjoy the remaining portion of the new year. You’re the one carrying out a first class job.
I am glad for writing to make you know what a magnificent encounter my wife’s girl had studying your web site. She mastered several pieces, which include what it’s like to possess an awesome helping nature to let other individuals very easily know precisely some multifaceted subject matter. You truly exceeded people’s expectations. Thank you for presenting these powerful, trusted, informative and in addition fun tips about this topic to Kate.
I am glad for writing to make you know what a incredible encounter my friend’s girl gained studying your web page. She learned plenty of pieces, which include what it’s like to possess an awesome helping mood to let other folks smoothly know just exactly several multifaceted subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie.