简体   繁体   中英

Trouble printing items from a List of Lists of Objects

I've got an assignment which requires me to create products and services and add them to a package (a list). each package will then be added to a list of packages.

I managed to create packages of one service and one product each but I'm having trouble printing the list as it will print all items within each package as many times as there are packages (ex if I add 3 packages it will print each product and service 3 times). I'm stumped as to what to do.

I ran the debugger and it shows that the q in p foreach is running 4 times instead of 2 but I have no idea how to fix this.

Here is my code from the Package class:

public class Package : AbstractProduct,IPackageble, IEnumerable, 
{

    public static List<AbstractProduct> package_elements = new List<AbstractProduct>();

    public bool canAddToPackage(Package package)
    {
        return false;
    }

    public void Add_element(AbstractProduct elem)
    {
        package_elements.Add(elem);
    }

 public override string Description()
    {
        foreach (AbstractProduct f in package_elements)
        {
            String var = " ";
            var = prod.Description();
        }
        return var;
    }

And the AbstractProductManager class:

public abstract class AbstractProductMgr
{
    //The general list

    protected static List<Package> elements = new List<Package>();

    public static void WriteToConsole()
    {
        foreach(Package p in elements)
        {
          foreach (AbstractProduct q in p)
            {
                Console.WriteLine(q.Description());
            }
        }
    }

AbstractProduct is an abstract class which defines the attributes for a product/service and a virtual Description method which lists them. The class is inherited by a Service and a Product classes, which override the Description method with any specific attributes.

The products/services are read from the keyboard in their own manager classes. A PackageMgr class calls the insertion methods for products/services and adds them to the Package list then adds the Package list to the Elements list.

You have:

public static List<AbstractProduct> package_elements = new List<AbstractProduct>();

which means that package_elements are shared between all instances of the Package class. You probably want to loose the static modifier:

public List<AbstractProduct> package_elements = new List<AbstractProduct>();

I ran the debugger and it shows that the q in p foreach is running 4 times instead of 2 but I have no idea how to fix this.

Remove the inner loop because the Package class will go though all its items and print them. I commented it out.

foreach(Package p in elements)
{
        // foreach (AbstractProduct q in p)
        //{
        Console.WriteLine(p.Description());
      //  }
}

You should also make the package_elements private or else things can be added to it from outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM