2

We are working on a school assignment where we are supposed to do the page which must be able to show a full list of all the customers and also be able to show a list of the names of all from Jutland, a list of the names of all customers from Funen and a list of the names of all customers from Zealand.

Here is the screenshot

Problem:

Instead of showing customers that are specific to one region (e.g Jutland), the system will add both the previous and the new customer (who is from a different region) into the same field, which represents the different region. Thus, the system cannot make a distinction between the individual regions of the customers.

In the screenshot above, the name Stanislav is supposed to be the only one in the ListBox. However, Sebastian, who belongs to another region, hence the other list box, is added as well. We believe it might have something to do with the foreach loop, although we are not entirely sure.

Any assistance will be greatly appreciated!

namespace _2ndHandinCsharp
{
    public partial class register : System.Web.UI.Page
    {
        private CustomerTank thetank;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["SharedCustomerTank"]== null)
            {
                Application["SharedCustomerTank"] = new CustomerTank();
            }

            thetank = (CustomerTank)Application["SharedCustomerTank"];
        }

        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            thetank = (CustomerTank)Application["SharedCustomerTank"];
            UpdateCustomerListView();
        }
        private void UpdateCustomerListView()
        {
            ListBox1.Items.Clear();
            List<Customer> customerInTheTank = thetank.TheCustomers();
            foreach(Customer c in customerInTheTank)
            {
                ListBox1.Items.Add(c.ToString());

            }
        }
        private void UpdateCustomerListViewJutland()
        {

            List<Customer> customerInTheTank = thetank.TheCustomers();

                for (int i = 0; i < customerInTheTank.Count; i++)
                {
                        if (DropDownListRegion.SelectedValue == "Jutland")
                        {
                            ListBoxJutland.Items.Add(customerInTheTank[i].Name);
                        }
                }
        }
        private void UpdateCustomerListViewFunen()
        {

            List<Customer> customerInTheTank = thetank.TheCustomers();


                for (int i=0; i<customerInTheTank.Count;i++)
                {
                        if (DropDownListRegion.SelectedValue == "Funen")
                        {
                    ListBoxFunen.Items.Add(customerInTheTank[i].Name);
                        }
                }

        }

        protected void ButtonAddCustomer_Click(object sender, EventArgs e)
        {
            Customer c = new Customer(
                TextBoxName.Text,
                TextBoxPassword.Text,
                int.Parse(TextBoxAge.Text),
                TextBoxZip.Text,
                DropDownListRegion.Text);

            Application.Lock();
            thetank = (CustomerTank)Application["SharedCustomerTank"];
            thetank.AddCustomer(c);
            Application["SharedCustomerTank"] = thetank; 
            Application.UnLock();
            UpdateCustomerListView();
            UpdateCustomerListViewJutland();
            UpdateCustomerListViewFunen();




        }




}

}`

0

2 Answers 2

2

Looks like you're using a wrong condition:

Instead of this,

for (int i = 0; i < customerInTheTank.Count; i++)
{
    if (DropDownListRegion.SelectedValue == "Jutland")
    {
        ListBoxJutland.Items.Add(customerInTheTank[i].Name);
    }
}

you should rather look at the actual location of the customer, not at the current selection in the region box:

ListBoxJutland.Items.Clear();
for (int i = 0; i < customerInTheTank.Count; i++)
{
    if (customerInTheTank[i].Region == "Jutland")
    {
        ListBoxJutland.Items.Add(customerInTheTank[i].Name);
    }
}

The name of the region property on the customer class is not included in your code, I assumed Region.

Note that I added a Clear() statement before the loop, which will prevent customer names from being added again and again, if the function is called repeatedly.

Sign up to request clarification or add additional context in comments.

Comments

1

You're right. The problem is with your for-each loops. When you're checking to see whether the region is/isn't Funen or Jutland you're forgetting to check whether the customer you're adding is also from that region. You just need an additional IF statement condition. Something like this should do

            for (int i=0; i<customerInTheTank.Count;i++)
            {
                    if (DropDownListRegion.SelectedValue == "Funen" && customerInTheTank[i].Region == "Funen")
                    {
                        ListBoxFunen.Items.Add(customerInTheTank[i].Name);
                    }
            }

4 Comments

From OP description and screenshot, I take that DropDownListRegion is used to specify the region of new customers, and should not affect the distribution of customers into the regional lists at all.
Yeah I think you're right. In that case your answer is more correct and they need only check the customers' region instead of the value of DropDownListRegion
Thank you very much it was helpful but we still facing issue when we are trying to add more "Customers" it repeats the previous customers when adding the new customer. like its here imgur.com/a/uZ6tUGw
Similar to how you clear the items in ListBox1 every time you click ButtonAddCustomer, be sure to clear ListBoxJutland and ListBoxFunen as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.