Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Created September 24, 2024 19:47
Show Gist options
  • Save shopglobal/e9dbee57b5396b39c9923be9f8afb06a to your computer and use it in GitHub Desktop.
Save shopglobal/e9dbee57b5396b39c9923be9f8afb06a to your computer and use it in GitHub Desktop.
Workshop selector

To build this application, here's a step-by-step approach using C# with Windows Forms or any similar desktop framework:

1. User Interface Design:

  • Create two ListBox controls:
    • One for selecting the Workshop.
    • One for selecting the Location.
  • Add a Button for calculation.
  • Add Label controls to display the Registration Cost, Lodging Cost, and Total Cost.

2. Workshop and Location Data:

Store the workshop details and location information using a data structure like dictionaries or arrays for easy lookup.

Example Code:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WorkshopSelector
{
    public partial class Form1 : Form
    {
        // Workshop details: {Workshop name, (days, registration fee)}
        Dictionary<string, (int days, decimal fee)> workshops = new Dictionary<string, (int, decimal)>
        {
            { "Handling Stress", (3, 1000) },
            { "Time Management", (3, 800) },
            { "Supervision Skills", (3, 1500) },
            { "Negotiation", (5, 1300) },
            { "How to Interview", (1, 500) }
        };

        // Location details: {Location, lodging fee per day}
        Dictionary<string, decimal> locations = new Dictionary<string, decimal>
        {
            { "Austin", 150 },
            { "Chicago", 225 },
            { "Dallas", 175 },
            { "Orlando", 300 },
            { "Phoenix", 175 },
            { "Raleigh", 150 }
        };

        public Form1()
        {
            InitializeComponent();

            // Add workshops to ListBox
            foreach (var workshop in workshops.Keys)
            {
                listBoxWorkshops.Items.Add(workshop);
            }

            // Add locations to ListBox
            foreach (var location in locations.Keys)
            {
                listBoxLocations.Items.Add(location);
            }
        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            // Get selected workshop and location
            string selectedWorkshop = listBoxWorkshops.SelectedItem?.ToString();
            string selectedLocation = listBoxLocations.SelectedItem?.ToString();

            if (selectedWorkshop != null && selectedLocation != null)
            {
                // Get workshop details
                var workshop = workshops[selectedWorkshop];
                int days = workshop.days;
                decimal registrationFee = workshop.fee;

                // Get lodging fee per day
                decimal lodgingFeePerDay = locations[selectedLocation];

                // Calculate total lodging cost
                decimal lodgingCost = days * lodgingFeePerDay;

                // Calculate total cost
                decimal totalCost = registrationFee + lodgingCost;

                // Display the costs
                labelRegistrationCost.Text = $"Registration: ${registrationFee}";
                labelLodgingCost.Text = $"Lodging: ${lodgingCost}";
                labelTotalCost.Text = $"Total: ${totalCost}";
            }
            else
            {
                MessageBox.Show("Please select a workshop and a location.");
            }
        }
    }
}

3. Explanation:

  • Data Setup: The workshops and locations are stored in dictionaries for quick lookups.
  • UI Interaction:
    • When the button is clicked, the application retrieves the selected workshop and location, looks up their values, and performs the cost calculation.
    • The results are displayed in labels on the form.

4. Handling Edge Cases:

  • Ensure the user selects both a workshop and a location before calculating.
  • Handle any invalid inputs or empty selections with an appropriate message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment