Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Last active August 20, 2024 16:19
Show Gist options
  • Save shopglobal/3761e64130dccec54ac2cedf7d30a120 to your computer and use it in GitHub Desktop.
Save shopglobal/3761e64130dccec54ac2cedf7d30a120 to your computer and use it in GitHub Desktop.
C# paint job calculator

To create a C# application that calculates the cost of a paint job based on the square feet of wall space, the price of the paint per gallon, and labor costs, you can follow the steps below:

Step 1: Create a New C# Console Application

Open Visual Studio, create a new C# Console Application, and name it appropriately (e.g., PaintJobCalculator).

Step 2: Write the Code

Here is the code that performs the calculations based on user input:

using System;

namespace PaintJobCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Constants
            const double SquareFeetPerGallon = 15;
            const double HoursPerGallon = 8;
            const double LaborCostPerHour = 20.00;

            // Input: Get the square feet and price per gallon from the user
            Console.Write("Enter the square feet of wall space to be painted: ");
            double squareFeet = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter the price of the paint per gallon: ");
            double pricePerGallon = Convert.ToDouble(Console.ReadLine());

            // Calculations
            double gallonsRequired = Math.Ceiling(squareFeet / SquareFeetPerGallon);
            double hoursRequired = gallonsRequired * HoursPerGallon;
            double paintCost = gallonsRequired * pricePerGallon;
            double laborCharges = hoursRequired * LaborCostPerHour;
            double totalCost = paintCost + laborCharges;

            // Output: Display the calculated data
            Console.WriteLine("\n----- Paint Job Cost Estimate -----\n");
            Console.WriteLine($"Gallons of paint required: {gallonsRequired}");
            Console.WriteLine($"Hours of labor required: {hoursRequired}");
            Console.WriteLine($"Cost of the paint: ${paintCost:F2}");
            Console.WriteLine($"Labor charges: ${laborCharges:F2}");
            Console.WriteLine($"Total cost of the paint job: ${totalCost:F2}");
        }
    }
}

Step 3: Explanation of the Code

  • Constants:

    • SquareFeetPerGallon: Represents how many square feet can be covered by one gallon of paint.
    • HoursPerGallon: Represents how many hours of labor are required per gallon of paint.
    • LaborCostPerHour: The cost of labor per hour.
  • Input:

    • The user is prompted to enter the square feet of wall space and the price per gallon of paint.
  • Calculations:

    • gallonsRequired: The number of gallons needed, calculated by dividing the total square feet by SquareFeetPerGallon and using Math.Ceiling to round up to the nearest whole number.
    • hoursRequired: The total hours required for the job, which is calculated as the product of gallonsRequired and HoursPerGallon.
    • paintCost: The total cost of the paint, calculated by multiplying gallonsRequired by pricePerGallon.
    • laborCharges: The total labor charges, calculated by multiplying hoursRequired by LaborCostPerHour.
    • totalCost: The total cost of the job, which is the sum of paintCost and laborCharges.
  • Output:

    • The program then outputs the gallons of paint required, the hours of labor, the cost of the paint, the labor charges, and the total cost of the paint job.

Step 4: Run the Application

  • When you run the application, it will prompt the user to input the square feet of wall space and the price per gallon of paint.
  • It will then display the required information based on the calculations.

This basic application serves as a simple tool to estimate the cost of a paint job.

To run the C# console application that you just created, follow these steps:

Step 1: Set Up the Development Environment

  1. Install Visual Studio: If you haven't already, you'll need to install Visual Studio. You can download it from here.
    • During installation, make sure to select the ".NET Desktop Development" workload, which includes the tools needed to build and run C# applications.

Step 2: Create a New C# Console Application

  1. Open Visual Studio:

    • Open Visual Studio from your Start menu or desktop.
  2. Create a New Project:

    • Click on "Create a new project".
    • In the project type selection, choose "Console App" under C#.
    • Click Next.
  3. Configure Your Project:

    • Name your project (e.g., PaintJobCalculator).
    • Choose a location to save the project.
    • Click Create.

Step 3: Write the Code

  1. Open the Program.cs File:

    • After creating the project, Visual Studio will open the Program.cs file by default. This is where you'll write your C# code.
  2. Paste the Code:

    • Replace the existing code in Program.cs with the code I provided earlier.
  3. Save the Code:

    • Save your work by pressing Ctrl + S or by going to File > Save All.

Step 4: Run the Application

  1. Build the Application:

    • Before running the application, ensure the code compiles correctly. You can build the application by pressing Ctrl + Shift + B or by going to Build > Build Solution.
    • If there are any errors, Visual Studio will highlight them, and you'll need to fix those before proceeding.
  2. Run the Application:

    • To run the application, press F5 or click the green "Start" button at the top of Visual Studio.
    • The console window will appear, and the program will prompt you to enter the square feet of wall space and the price per gallon of paint.
  3. Enter the Input:

    • Input the required values as prompted by the console.
    • Press Enter after each input.
  4. View the Output:

    • The application will display the number of gallons of paint required, hours of labor required, cost of the paint, labor charges, and the total cost of the paint job.
  5. Close the Application:

    • After the output is displayed, you can close the console window by pressing any key or simply by closing the window.

Step 5: Modify and Re-Run as Needed

  • If you need to make changes to the code, you can edit the Program.cs file and run the application again.

That's it! You've successfully created and run your first C# console application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment