0 Replies - 726 Views - Last Post: 20 February 2015 - 10:28 PM

#1 fucode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-September 14

if statement bug

Posted 20 February 2015 - 10:28 PM

Hi!, in this program I/m trying to do the following if the Class Number (e.g CS 1301) already exists, it should not be added and the error message, “Not added. Course already exists.” should be displayed. For example, “CS 1301-4” is in the list initially. if the user typed in: “CS 1301” for the class number and “4” for the credits (or “1”, or “2”, etc), then this course would not be added. Similarly, Hint: you need to loop through the available course, extract the course name and compare it to what the user typed in. Next, loop through the registered list and do the same.
my program works for the elements on default but if a add a new class for example "CS 1550-2" it does gets added when i click the "make available button" but if i click to add that class one more time a message saying this class is already available should display,but this doesn't happen and the class gets added again like if it was a new class, so I'm assuming the array of the listBox doesn't get updated on time or something like that. the line of code were my bug starts is in line 216

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Patricio Vargas
public partial class _default : System.Web.UI.Page
{
    int hours = 0;
    int cost = 0;
    int limit = 0;
    String newCourse = "";
    int credits = 0;
    ListItem newClass;
    protected void Page_Load(object sender, EventArgs e)
    {
        buildAvailableCourseList().ToArray<ListItem>();
        if (!Page.IsPostBack)        //For initial page creation
        {
            ListItem[] availableCourses = buildAvailableCourseList();
            lbxAvailableClasses.DataSource = availableCourses;
            lbxAvailableClasses.DataTextField = "Text";
            lbxAvailableClasses.DataValueField = "Value";
            lbxAvailableClasses.DataBind();
        }
    }
    private ListItem[] buildAvailableCourseList()
    {
        ListItem[] tempList = { new ListItem("CS 1301-4", "CS 1301-4"),
                                new ListItem("CS 1302-4", "CS 1302-4"),
                                new ListItem("CS 1303-4", "CS 1303-4"),
                                new ListItem("CS 2202-2", "CS 2202-2"),
                                new ListItem("CS 2224-2", "CS 2224-2"),
                                new ListItem("CS 3300-3", "CS 3300-3"),
                                new ListItem("CS 3301-1", "CS 3301-1"),
                                new ListItem("CS 3302-1", "CS 3302-1"),
                                new ListItem("CS 3340-3", "CS 3340-3"),
                                new ListItem("CS 4321-3", "CS 4321-3"),
                                new ListItem("CS 4322-3", "CS 4322-3")
                              };
        return tempList;
    }
    protected void addBtn_Click(object sender, EventArgs e)
    {
        
        lblHours.Visible = true;
        lblCost.Visible = true;
        String[] textD = new String[3];
        String[] text = new String[3];
        String[] temp1 = new String[3];
        String[] textC = new String[3];
        String[] textz = new String[3];
        String[] temp2 = new String[3];
        var selected = new List<ListItem>();
        int myhours = Convert.ToInt32(lblHours.Text);
        foreach (ListItem item in lbxAvailableClasses.Items)
        {
            if (item.Selected)
            {
                textC = item.ToString().Split(' ');
                temp2 = textC[1].Split('-');
                myhours += int.Parse(temp2[1]);
                if (myhours <= 19)
                {
                    selected.Add(item);
                    lbxRegisterClasses.Items.Add(item);
                }
                else
                {
                    lblLimit.Visible = true;
                }
            }

        }
        foreach (ListItem item in selected)
        {
            lbxAvailableClasses.Items.Remove(item);
        }

        foreach (ListItem item in lbxRegisterClasses.Items)
        {
            textD = item.ToString().Split(' ');
            temp1 = textD[1].Split('-');
            hours += int.Parse(temp1[1]);
            cost = (hours * 1000);

        }
        if (checkB1.Checked)
        {
            cost += 1000;
        }
        if (checkB2.Checked)
        {
            cost += 500;
        }
        if (checkB3.Checked)
        {
            cost += 50;
        }
        lblCost.Text = cost.ToString();
        lblHours.Text = hours.ToString();
    }
    public void labels()
    {
        lblHours.Text = hours.ToString();
        lblCost.Text += cost.ToString();
    }
    protected void removetbtn_Click(object sender, EventArgs e)
    {
        ArrayList q = new ArrayList();
        var selected = new List<ListItem>();

        foreach (ListItem item in lbxRegisterClasses.Items)
        {

            if (item.Selected)
            {

                selected.Add(item);
                lbxAvailableClasses.Items.Add(item);
            }
        }
        foreach (ListItem item in selected)
        {
            lbxRegisterClasses.Items.Remove(item);
        }
        foreach (ListItem item in lbxRegisterClasses.Items)
        {
            String[] textD = new String[3];
            String[] text = new String[3];
            String[] temp1 = new String[3];
            textD = item.ToString().Split(' ');
            temp1 = textD[1].Split('-');
            hours -= int.Parse(temp1[1]);
            cost = hours * 1000;
        }
        if (checkB1.Checked)
        {
            cost += 1000;
        }
        if (checkB2.Checked)
        {
            cost += 500;
        }
        if (checkB3.Checked)
        {
            cost += 50;
        }
        lblCost.Text = cost.ToString();
        lblHours.Text = hours.ToString();
    }
    protected void resetbtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/default.aspx");
    }
    protected void checkB2_CheckedChanged(object sender, EventArgs e)
    {

        if (checkB2.Checked)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += 500 + Convert.ToInt32(lblCost.Text);
            lblCost.Text = cost.ToString();

        }
        else if (checkB2.Checked == false)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += Convert.ToInt32(lblCost.Text) - 500;
            lblCost.Text = cost.ToString();

        }
    }
    protected void checkB1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkB1.Checked)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += Convert.ToInt32(lblCost.Text) + 1000;
            lblCost.Text = cost.ToString();

        }
        else if (checkB1.Checked == false)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += Convert.ToInt32(lblCost.Text) - 1000;
            lblCost.Text = cost.ToString();

        }
    }
    protected void checkB3_CheckedChanged(object sender, EventArgs e)
    {
        if (checkB3.Checked)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += Convert.ToInt32(lblCost.Text) + 50;
            lblCost.Text = cost.ToString();

        }
        else if (checkB3.Checked == false)
        {
            lblHours.Visible = true;
            lblCost.Visible = true;
            cost += Convert.ToInt32(lblCost.Text) - 50;
            lblCost.Text = cost.ToString();

        }
    }
    protected void makeAvailable_Click(object sender, EventArgs e)
    {
        String temp1 = "";
        newCourse = TextBox1.Text;
        String tempA = "";
        if (lbxRegisterClasses.Items.Count > 0)
        {
            foreach (ListItem i in lbxRegisterClasses.Items)
            {
                temp1 = i.Text;
                tempA = temp1.Substring(0, temp1.Length - 2);
                if (tempA.Equals(newCourse))
                {
                    Label4.Visible = true;
                    Label4.Text = "Course is already available";
                    break;
                }
                else
                {
                    foreach (ListItem item in lbxAvailableClasses.Items)
                    {
                        temp1 = item.Text;
                        tempA = temp1.Substring(0, temp1.Length - 2);
                        if (tempA.Equals(newCourse))
                        {
                            Label4.Visible = true;
                            Label4.Text = "Course is already available";
                            break;
                        }
                        else
                        {
                            newCourse = TextBox1.Text;
                            credits = Convert.ToInt32(TextBox2.Text);
                            newClass = new ListItem(newCourse + "-" + credits, newCourse + "-" + credits);
                            lbxAvailableClasses.Items.Add(newClass);
                            Label4.Visible = false;
                            break;
                        }
                    }
                }
                
            }
        }
        else
        {
            newCourse = TextBox1.Text;
            foreach (ListItem x in lbxAvailableClasses.Items)
            {
                String temp2 = x.Text;
                String tempB = temp2.Substring(0, temp2.Length - 2);
                if (!tempB.Equals(newCourse))
                {
                    newCourse = TextBox1.Text;
                    credits = Convert.ToInt32(TextBox2.Text);
                    newClass = new ListItem(newCourse + "-" + credits, newCourse + "-" + credits);
                    lbxAvailableClasses.Items.Add(newClass);
                    Label4.Visible = false;
                    break;
                    
                }
                else
                {
                    Label4.Visible = true;
                    Label4.Text = "Course is already available";
                    break;
                }
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        String temp1 = "";
        newCourse = TextBox1.Text;
        String tempA = "";
        if (lbxRegisterClasses.Items.Count > 0)
        {
            foreach (ListItem i in lbxRegisterClasses.Items)
            {
                temp1 = i.Text;
                tempA = temp1.Substring(0, temp1.Length - 2);
                if (tempA.Equals(newCourse))
                {
                    Label4.Visible = true;
                    Label4.Text = "Course is registered";
                    break;
                }
                else
                {
                    foreach (ListItem item in lbxAvailableClasses.Items)
                    {
                        temp1 = item.Text;
                        tempA = temp1.Substring(0, temp1.Length - 2);
                        if (tempA.Equals(newCourse))
                        {
                            lbxAvailableClasses.Items.Remove(item);
                            break;
                        }
                        else
                        {
                            Label4.Text = "Course not found";
                        }
                    }
                }
                
            }
        }
        else
        {
            foreach (ListItem item in lbxAvailableClasses.Items)
            {
                temp1 = item.Text;
                tempA = temp1.Substring(0, temp1.Length - 2);
                if (tempA.Equals(newCourse))
                {
                    lbxAvailableClasses.Items.Remove(item);
                    break;
                }
                else
                {
                    Label4.Text = "Course not found";
                }
            }

        }

    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1