Back

Code Challenge: Receipt Line

Difficulty  

divider

Objective

Store the details of a single order in variables, then print them as a readable summary. Every value on the receipt comes from a variable — not one of them is typed directly into a Console.WriteLine() call.

That restriction is the point. A program where the data lives in variables can be changed in one place; a program where the data is baked into the output has to be rewritten.

Skills to Practice

  • Declaring variables with explicit types
  • Choosing the right data type for each value
  • camelCase naming
  • Printing a label and a variable together with interpolation
  • Calculating with variables rather than literal numbers

Start From This

Copy these four lines into your program exactly as written, then build the rest below them.

Receipt Line
string itemName = "Mechanical Keyboard";
int quantity = 2;
double unitPrice = 89.99;
bool inStock = true;
// Your code goes below this line.

Notice that three different data types are already in use here. Before you write anything, name them: which variable is a string, which is a number, and which is a bool?


Tasks

  • Create a new program named cc-receipt-line.
  • Print the heading line shown in the sample output.
  • Print the item name, the quantity, the unit price, and whether it is in stock — each on its own line, each with a label.
  • Declare a new variable named lineTotal and set it to the quantity times the unit price. Do not type 179.98 anywhere.
  • Print the line total.
  • Print a blank line before the in-stock line.
Then prove it works
  • Change quantity to 5 and run the program again. The line total must update on its own.
  • If it did not, your line total is a typed-in number rather than a calculation. Fix it before you submit.
Challenge (optional)
  • Add a variable for a discount amount and print a second total with the discount subtracted. Keep the original line total on the receipt as well.

Sample Output

Sample Output
--- Order Summary ---
Item: Mechanical Keyboard
Quantity: 2
Unit price: 89.99
Line total: 179.98
In stock: True

The line total shows as 179.98 here because 2 × 89.99 happens to come out clean. You are not being asked to control the number of decimal places yet — and bool prints as True or False, capitalized, whether you write it that way or not.


Submit

Submit your program file, plus a one-line answer to this:

What was on the screen after you changed the quantity to 5? Give the line total you saw.

That number tells me whether your receipt is calculating or just reciting, which is the entire difference between this challenge and the last one.

Commence Challenge