Problem overview
The cart has a subtotal, a flat shipping_cost and a free_shipping_threshold. If the subtotal reaches the threshold, shipping is free. Otherwise the customer pays the shipping cost. The program prints a three-line receipt: the subtotal, the shipping and the total.
With a subtotal of 75, a shipping cost of 10 and a threshold of 100, the cart is below the threshold, so shipping is 10 and the total is 85:
Subtotal: $75
Shipping: $10
Total: $85
With a subtotal of 120 the cart qualifies. The lines are Subtotal: $120, Shipping: $0 and Total: $120. The shipping line is always printed, and it shows $0 when shipping is free.
Approach: decide the shipping first
The total depends on the shipping, and the shipping depends on a comparison, so the order of work is fixed. First decide shipping with an if/else. Then compute total. Then build the three lines and join them.
The comparison is subtotal >= free_shipping_threshold. A cart that is exactly on the threshold ships free: a subtotal of 100 gives Shipping: $0. With > instead of >=, that cart would pay 10.
The decision sets a variable rather than printing, because the shipping value is needed in two places later: in the total and in the second line of the receipt.
Step by step
- Declare
shippingwithout a value.int shipping;creates the variable. One of the two branches will assign it. - Write the
if. Its condition issubtotal >= free_shipping_threshold. - Free case. Inside the
if, setshippingto 0. - Add the
else. It covers every cart under the threshold. - Paid case. Inside the
else, setshippingtoshipping_cost. - Compute the total.
totalissubtotal + shipping. When shipping is free it is 0, so the total equals the subtotal. - Build the three lines.
line1,line2andline3are each a label such asSubtotal: $joined to a number with+. - Join them into
result. Three lines need two newline characters: one between line 1 and line 2, one between line 2 and line 3. - Print
resultwithConsole.WriteLine.
The solution in C#
int subtotal = 75;
int shipping_cost = 10;
int free_shipping_threshold = 100;
int shipping;
if (subtotal >= free_shipping_threshold) {
shipping = 0;
} else {
shipping = shipping_cost;
}
int total = subtotal + shipping;
string line1 = "Subtotal: $" + subtotal;
string line2 = "Shipping: $" + shipping;
string line3 = "Total: $" + total;
string result = line1 + "\n" + line2 + "\n" + line3;
Console.WriteLine(result);
The C# details worth a closer look:
- Declaring without a value is allowed, reading before assigning is not. C# checks that a local variable is assigned on every path before it is read. Both branches of the
if/elseassignshipping, so the lineint total = subtotal + shipping;is accepted. If theelsewere missing, the compiler would reject that line with Use of unassigned local variable 'shipping'. - The same decision in one line. The ternary operator can give
shippingits value directly:int shipping = subtotal >= free_shipping_threshold ? 0 : shipping_cost;. The lesson usesif/elsebecause it is the construct being taught, and because it extends to more complicated decisions. +joins text and numbers. With astringon one side, C# turns theinton the other into text. The$in"Subtotal: $"is an ordinary character in a normal string literal."\n"is one character. The backslash sequence is the newline character.Console.WriteLineadds its own line ending after the last line, soresultdoesn't need a newline at its end.- Names with underscores.
shipping_costandfree_shipping_thresholdare valid C# names. The usual C# habit is camelCase for locals, but the lesson keeps the names from the statement.
Another way to build the lines
C# also has interpolated strings, written with a leading $ and values in braces. The whole receipt can be one expression: $"Subtotal: ${subtotal}\nShipping: ${shipping}\nTotal: ${total}". For a subtotal of 120 it gives the same three lines as the step-by-step version. The $ at the very front marks the whole string as interpolated; every other $ is plain text that happens to sit before a value in braces. The lesson uses + because each line is built and named separately.
Edge cases and common mistakes
- Exactly on the threshold. A subtotal of 100 prints
Shipping: $0andTotal: $100. - One under the threshold. A subtotal of 99 prints
Shipping: $10andTotal: $109. The customer pays more in total than for a subtotal of 100, which follows from the rule. - Adding inside the string.
"Total: $" + subtotal + shippingdoesn't add the numbers. The expression is evaluated left to right, so it joinsTotal: $75to10and printsTotal: $7510. Computingtotalas anintfirst, or writing"Total: $" + (subtotal + shipping), gives 85. - Leaving out the free line. The receipt always has three lines. An
ifthat prints the shipping line only when it is non-zero would drop it for free shipping. - Extra newline at the end. Adding
"\n"afterline3would give a blank line after the receipt, becauseWriteLinealready ends the line. - Whole dollars only. The variables are
int, so the program can't represent cents. A price with cents would needdecimal.
Testing
- A subtotal of 75 gives
Subtotal: $75,Shipping: $10,Total: $85. - A subtotal of 120 gives
Subtotal: $120,Shipping: $0,Total: $120. - A subtotal of 100 gives
Subtotal: $100,Shipping: $0,Total: $100. - A subtotal of 99 gives
Subtotal: $99,Shipping: $10,Total: $109.
