Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Created September 21, 2024 21:50
Show Gist options
  • Save shopglobal/c51840cd604ef682f0fc15d1b589c70a to your computer and use it in GitHub Desktop.
Save shopglobal/c51840cd604ef682f0fc15d1b589c70a to your computer and use it in GitHub Desktop.
Paint job estimator

To implement this new scenario in the Windows Forms application, we will adjust the logic to calculate the required gallons of paint, hours of labor, and related costs. Let's break it down:

Updated Requirements

  • For every 15 square feet of wall space:
    • 1 gallon of paint is required.
    • 8 hours of labor are needed.
  • Labor costs $20.00 per hour.
  • User Inputs:
    • Total square feet of wall space.
    • Price per gallon of paint.

Output:

  • The number of gallons of paint required.
  • The hours of labor required.
  • The cost of the paint.
  • The labor charges.
  • The total cost of the paint job.

Step 1: Design the Form

Add the following controls to your form:

  • Labels:
    • lblSquareFeet: "Total Wall Space (sq. ft.):"
    • lblPaintPrice: "Price per Gallon of Paint:"
  • TextBoxes:
    • txtSquareFeet
    • txtPaintPrice
  • Button:
    • btnCalculate: "Calculate"
  • Labels or TextBoxes to display results:
    • lblGallonsRequired
    • lblHoursRequired
    • lblPaintCost
    • lblLaborCost
    • lblTotalCost

Step 2: Code Implementation

In the Form1.cs file, here’s the updated code logic:

using System;
using System.Windows.Forms;

namespace PaintingJobCalculator
{
    public partial class Form1 : Form
    {
        // Constants for the calculations
        private const double SQUARE_FEET_PER_GALLON = 15.0;
        private const double HOURS_PER_GALLON = 8.0;
        private const double LABOR_COST_PER_HOUR = 20.0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                // Get user inputs
                double squareFeet = double.Parse(txtSquareFeet.Text);
                double paintPricePerGallon = double.Parse(txtPaintPrice.Text);

                // Calculate the number of gallons required
                double gallonsRequired = Math.Ceiling(squareFeet / SQUARE_FEET_PER_GALLON);

                // Calculate the labor hours required
                double laborHours = gallonsRequired * HOURS_PER_GALLON;

                // Calculate the cost of the paint
                double paintCost = gallonsRequired * paintPricePerGallon;

                // Calculate the labor cost
                double laborCost = laborHours * LABOR_COST_PER_HOUR;

                // Calculate the total cost of the job
                double totalCost = paintCost + laborCost;

                // Display the results
                lblGallonsRequired.Text = $"Gallons of Paint Required: {gallonsRequired}";
                lblHoursRequired.Text = $"Hours of Labor Required: {laborHours}";
                lblPaintCost.Text = $"Cost of Paint: {paintCost:C}";
                lblLaborCost.Text = $"Labor Charges: {laborCost:C}";
                lblTotalCost.Text = $"Total Cost of the Paint Job: {totalCost:C}";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please enter valid numeric values.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

Explanation of the Code:

  1. Constants:

    • SQUARE_FEET_PER_GALLON: 15 square feet per gallon.
    • HOURS_PER_GALLON: 8 hours of labor per gallon of paint.
    • LABOR_COST_PER_HOUR: Labor costs $20.00 per hour.
  2. Calculation:

    • gallonsRequired: We calculate the number of gallons by dividing the total square feet by 15. The result is rounded up using Math.Ceiling to ensure we have enough paint even if the division leaves a remainder.
    • laborHours: This is calculated by multiplying the number of gallons by 8 (since 8 hours of labor are needed per gallon).
    • paintCost: Multiply the number of gallons by the price per gallon.
    • laborCost: Multiply the number of labor hours by the hourly labor rate.
    • totalCost: Sum of the paint cost and labor cost.
  3. Displaying the Results:

    • The results are displayed in the appropriate labels or textboxes for the user to view the output.

Step 3: Form Design

Make sure your form has appropriate labels for the outputs. For example:

  • Labels for output:
    • lblGallonsRequired: Displays the gallons of paint required.
    • lblHoursRequired: Displays the hours of labor required.
    • lblPaintCost: Displays the cost of the paint.
    • lblLaborCost: Displays the labor cost.
    • lblTotalCost: Displays the total cost of the paint job.

Example Outputs:

If the user inputs:

  • Square feet: 100
  • Price per gallon: $30.00

The program would calculate and display:

  • Gallons of Paint Required: 7 (since 100/15 = 6.67, rounded up)
  • Hours of Labor Required: 56 (7 gallons * 8 hours)
  • Cost of Paint: $210.00 (7 gallons * $30.00)
  • Labor Charges: $1,120.00 (56 hours * $20.00/hour)
  • Total Cost of the Paint Job: $1,330.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment