Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

NotCoolNick

New Coder
Hello, I have the following problem:

"Create an app that filter rooms and pick the ones that fit the required number of participants.
As a result, the app should return the list of available slots for each of the rooms.
Constraints: a slot start at every 15th ( 0, 15, 30, 45).
Example: room is available between 10:00-17:00 and taken between 11:00-11:30 and 12:30-17:00, meeting is 30min long
the app returns 10:00-10:30, 10:30-11:00, 11:30-12:00, 12:00-12:30 as free spots."

It could be solved on JS, Java, etc., I'm solving in c#. I will appreciate tips on how to approach the problem.
 
Better late than never, I guess....

Here's what I came up with and.... I think it solves the problem? I'm 99% confident that it does, but perhaps I'm being too optimistic.

I am however 100% confident that a better programmer could have done a better job. :)

Room 1 = 1 - 5 people.
Room 2 = 1 - 10 people.

C#:
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int x;
int y;
    for (x = 1; x < 11; x++)
    {
        if (x == 1)
        {
        listBox1.Items.Add (x + " person");
        }
        else
        {
        listBox1.Items.Add (x + " people");
        }
    }
    string a;
    string b;
    int arraynumber = 0;
    int addtime = 10;
    for (x = 1; x < 3; x++)
    {
        for (y = 1; y < 3; y++)
        {
            if (y == 1)
            {
            a = ".00";
            b = ".30";
            globals.thetimes[arraynumber] = addtime + a + " - " + addtime + b;
            }
            else if (y == 2)
            {
            a = ".30";
            b = ".00";
            globals.thetimes[arraynumber] = addtime + a + " - " + (addtime + 1) + b;
            }
        arraynumber = arraynumber + 1;
        }
    addtime = addtime + 1;
    }
label4.Text = "Bookings" + "\n";
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x;
listBox2.Items.Clear();
listBox3.Items.Clear();
    if (listBox1.SelectedIndex < 5)
    {
        for (x = 1; x < 5; x++)
        {
            if (globals.roomtime1[x - 1] == "")
            {
                listBox2.Items.Add(globals.thetimes[x - 1]);
            }
            if (globals.roomtime2[x - 1] == "")
            {
                listBox3.Items.Add(globals.thetimes[x - 1]);
            }
        }
    }
    else if (listBox1.SelectedIndex > 4)
    {
        for (x = 1; x < 5; x++)
        {
            if (globals.roomtime2[x - 1] == "")
            {
                listBox3.Items.Add(globals.thetimes[x - 1]);
            }
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
int x;
    if (listBox1.SelectedIndex == -1)
    {
    MessageBox.Show ("Please select the number of people.");
    return;
    }
    if (listBox1.SelectedIndex > -1 && listBox1.SelectedIndex < 5)
    {
        if (listBox2.SelectedIndex == -1 && listBox3.SelectedIndex == -1)
        {
        MessageBox.Show ("Please select either Room 1 or Room 2's time.");
        return;
        }
        if (listBox2.SelectedIndex > -1 && listBox3.SelectedIndex > -1)
        {
        MessageBox.Show ("Please select either Room 1 or Room 2's time.");
        listBox2.SelectedIndex = -1;
        listBox3.SelectedIndex = -1;
        return;
        }
        if (listBox2.SelectedIndex > -1)
        {
            for (x = 1; x < 5; x++)
            {
                if (listBox2.SelectedItem == globals.thetimes[x - 1])
                {
                globals.roomtime1[x - 1] = globals.thetimes[x - 1];
                label4.Text = label4.Text  + "\n" + listBox1.SelectedItem + " --- Room 1 - " + globals.roomtime1[x - 1];
                }
            }
        globals.total = globals.total + 1;
        }
        else if (listBox3.SelectedIndex > -1)
        {
            for (x = 1; x < 5; x++)
            {
                if (listBox3.SelectedItem == globals.thetimes[x - 1])
                {
                globals.roomtime2[x - 1] = globals.thetimes[x - 1];
                label4.Text = label4.Text  + "\n" + listBox1.SelectedItem + " --- Room 2 - " + globals.roomtime2[x - 1];
                }
            }
        globals.total = globals.total + 1;
        }
    }
    else if (listBox1.SelectedIndex > 4 && listBox1.SelectedIndex < 10)
    {
        if (listBox3.SelectedIndex == -1)
        {
        MessageBox.Show ("Please select Room 2's time.");
        return;
        }
        for (x = 1; x < 5; x++)
        {
            if (listBox3.SelectedItem == globals.thetimes[x - 1])
            {
            globals.roomtime2[x - 1] = globals.thetimes[x - 1];
            label4.Text = label4.Text  + "\n" + listBox1.SelectedItem + " --- Room 2 - " + globals.roomtime2[x - 1];
            }
        }
    globals.total = globals.total + 1;
    }
    if (globals.total == 8)
    {
    listBox1.Items.Clear();
    listBox1.Enabled = false;
    listBox2.Enabled = false;
    listBox3.Enabled = false;
    button1.Enabled = false;
    label4.Text = label4.Text + "\n\n" + "No more rooms available.";
    }
listBox1.SelectedIndex = -1;
listBox2.Items.Clear();
listBox3.Items.Clear();
}

}

static class globals
{
public static string[] thetimes = {"", "", "", ""};
public static string[] roomtime1 = {"", "", "", ""};
public static string[] roomtime2 = {"", "", "", ""};
public static int total = 0;
}

}
 

Attachments

  • Screenshot.png
    Screenshot.png
    20.3 KB · Views: 2

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom