1. Create a Windows Forms order-total calculator that accepts quantity and unit price and displays the subtotal.
Requirements: Use a TextBox for quantity, a TextBox for unit price, a Calculate button, numeric validation, and a Label for output.
Reference solution:
private void btnCalculate_Click(object sender, EventArgs e)
{
if (!int.TryParse(txtQuantity.Text, out int quantity) || quantity < 1)
{
MessageBox.Show("Enter a quantity of at least 1.");
return;
}
if (!decimal.TryParse(txtUnitPrice.Text, out decimal price) || price < 0m)
{
MessageBox.Show("Enter a valid nonnegative unit price.");
return;
}
decimal subtotal = quantity * price;
lblSubtotal.Text = subtotal.ToString("C");
}
Rationale: The task assesses event handling, input validation, numeric data and output formatting.
2. Write a method that returns a shipping charge: $0 for orders of at least $75, otherwise $6.95.
Requirements: Create a decimal-returning method and call it from a button handler.
Reference solution:
private decimal CalculateShipping(decimal orderTotal)
{
if (orderTotal >= 75m)
return 0m;
return 6.95m;
}
Rationale: A focused method isolates business logic and the inclusive threshold is handled correctly.
3. Create a menu selection using switch that displays a message for options 1, 2, 3 and an invalid choice.
Requirements: Use an integer selection, a switch statement and a default branch.
Reference solution:
switch (choice)
{
case 1: lblMessage.Text = "New order"; break;
case 2: lblMessage.Text = "Search orders"; break;
case 3: lblMessage.Text = "Reports"; break;
default: lblMessage.Text = "Select 1, 2, or 3"; break;
}
Rationale: This assesses multiway selection and a safe fallback for unexpected input.