开发者

Create a list of objects and display them in the console through another class

For a homework assignment i need to create a List<T>, add objects to it, and display the objects toString. Here is what i have:

public class Order
{
    ...
      public List<OrderLine> orderLines;
      public Order(User user1)
      {
          ...
          orderLines = new List<OrderLine>();
      }

      public void AddOrderLine(OrderLine newOrderLine)
      {
          orderLines.Add(newOrderLine);
      }
}

public class OrderLine
{
   public OrderLine(Product p1, int q1)
   {
      ...
   }
   public override string ToString() //Required by teacher
   {
      string myOrderLine;
      myOrderLine = String.Format("[Product name: {0}; Price: {1}; Quantity: {2}]", 
         product.name, product.price, quantity);
      return myOrderLi开发者_开发技巧ne;
   }
}

When adding oerderLines.Add(newOrderLine) new OrderLine is an object created in another class that has a working toString. How do i display all the newOrderLines from the Program.CS?


I assume your main method looks something like this:

{    
   User user = new User();    
   Order order = new Order(user);   
   OrderLine orderLine1 = new OrderLine();   
   order.AddOrderLine(orderLine1); 
}

If I understand what you say correctly, OrderLine already overrides ToString(), so that when you call ToString(), it returns something "human-meaningful". In that case, the only thing you need to do is something along the lines of

foreach (var orderLine in order.orderLines)
{
    Console.WriteLine(orderLine.ToString());
}

Note that it is considered good practice not to publicly expose a field, like orderLines. Better make it private, and create a public property like:

public IEnumerable<OrderLine> OrderLines
{
   get { return this.orderLines; }
}


Assuming you have the working .ToString() for the OrderLine class. If you just want to display them to the console, you can do a simple:

foreach(OrderLine orderline in orderLines)
   System.Console.WriteLine(orderline.ToString());

edit:

Two simple ways to do this is either the way Mathias outlined quite nicely or put my code inside your Order class. For example, add a new method like this:

public void PrintOrders()
{
    foreach(OrderLine orderline in this.orderLines)
       System.Console.WriteLine(orderline.ToString());  
}

You would use it like:

User user = new User();    
Order order = new Order(user);   
OrderLine orderLine = new OrderLine();
order.AddOrderLine(orderLine);
order.PrintOrders();

Please note that it is generally not considered good practice to have classes printing to the console. I just wanted to show you a different way of approaching the problem.


if you have something like this in your Program.cs to populate your Order object

var order = new Order();
order.AddOrderLine(...);

//this is how you can display each item ToString in the collection
foreach(var o in order.orderLines)
{
   Console.WriteLine(o.ToString());
}

just check for any typo or syntax error as I only typed this here inline


Is this what you need?

public static void Main(string[] args)
{
    Order order = new Order();

    OrderLine line1 = new OrderLine(...);
    OrderLine line2 = new OrderLine(...);
    OrderLine line3 = new OrderLine(...);

    order.AddOrderLine(line1);
    order.AddOrderLine(line2);
    order.AddOrderLine(line3);

    foreach (OrderLine line in order.orderLines)
    {
        Console.WriteLine(line.ToString());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜