0 Replies - 730 Views - Last Post: 21 February 2015 - 11:10 AM

#1 fucode  Icon User is offline

  • New D.I.C Head

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

foreach loop only uses first object

Posted 21 February 2015 - 11:10 AM

Hi!, I'm trying to complete my assignment but this following step is getting me stock 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.

when i check if CS 1301 exists it return "Course already available" ,but if i try CS 1302 that is the second one in the list and it already exist it add the element instead of showing that it is already available. Also, if i add for example 1307 it gets added and if i try to added right the way one more time it displays the message that it already exist but if i add another course and try to add 1307 again it gets added like if it didnt exist on the list

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);
                tempArraysss(item);
                tempArray();
                lbxAvailableClasses.DataSource = tempArr;
                lbxAvailableClasses.DataBind();
                lbxAvailableClasses.DataBind();
            }
        }
        foreach (ListItem item in selected)
        {
            lbxRegisterClasses.Items.Remove(item);
            lbxAvailableClasses.DataBind();
        }
        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();

        }
    }
    ArrayList tempArr = new ArrayList();
    public void tempArray()
    {
        foreach(ListItem i in lbxAvailableClasses.Items)
        {
            tempArr.Add(i);
        }
    }
    public void tempArraysss(ListItem newItem)
    {
        tempArr.Add(newItem);  
    }
    void BindData()
    {
        lbxAvailableClasses.DataSource = null;
        lbxAvailableClasses.DataSource = tempArr;
        lbxAvailableClasses.DataTextField = "Text";
        lbxAvailableClasses.DataValueField = "Value";
        lbxAvailableClasses.DataBind();
    }

    protected void makeAvailable_Click(object sender, EventArgs e)
    {
        lbxAvailableClasses.DataBind();
        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";
                    lbxAvailableClasses.DataBind(); 
                    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";
                            lbxAvailableClasses.DataBind();
                            break;
                        }
                        else
                        {
                            credits = Convert.ToInt32(TextBox2.Text);
                            newClass = new ListItem(newCourse + "-" + credits, newCourse + "-" + credits);
                            tempArraysss(newClass);
                            tempArray();
                            lbxAvailableClasses.DataSource = tempArr;
                            lbxAvailableClasses.DataBind();
                            Label4.Visible = false;
                            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";
                        lbxAvailableClasses.DataBind();
                        
                        break;
                    }
                    else
                    {
                        credits = Convert.ToInt32(TextBox2.Text);
                        newClass = new ListItem(newCourse + "-" + credits, newCourse + "-" + credits);
                        tempArraysss(newClass);
                        tempArray();
                        lbxAvailableClasses.DataSource = tempArr;
                        lbxAvailableClasses.DataBind();
                        Label4.Visible = false;
                        
                        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";
                }
            }

        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        tempArray();
    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1