计算列表字典中所有对象的总成本

时间:2014-12-02 15:33:29

标签: c# list

我是C#的编程学生,我被要求编写一个应用程序,但我无法弄清楚如何获取所有对象并计算总价格。

如果您可以将我引荐到另一页或回答

,那么任何帮助都会很好

感谢public decimal TotalCost()

namespace GCUShows
{
    public class Booking
    {
        private const int LIMIT = 6;

        // TODO: This class should include the following:
        // instance variable show which is a reference to a Show object

        public Show show;
        private int bookingID;
        public List<ITicket> tickets;

        public int BookingID
        {
            get { return bookingID; }
            set { bookingID = value; }
        }

        public Booking(Show show)
        {
            this.BookingID = BookingIDSequence.Instance.NextID;
            this.show = show;
            show.AddBooking(this);
            this.tickets = new List<ITicket>();
        }

        public void AddTickets(int number, TicketType type, decimal fee)
        {
            // TODO:this method should instantiate the specified number of tickets of the
            // specified type and add these to the list of tickets in this booking
            if (type == TicketType.Adult)
            {
                for(int i =0; i < number; i++)
                {
                    tickets.Add(new AdultTicket(show.Title, fee));
                }
            }
                else if (type == TicketType.Child)
                {
                    for(int i=0; i< number; i++)
                    {
                        tickets.Add(new ChildTicket(show.Title));
                     }
                }
            else if (type == TicketType.Family)
            {
                for (int i = 0; i < number; i++)
                {
                    tickets.Add(new FamilyTicket(show.Title, fee));
                }
             }
        }

        public string PrintTickets()
        {
            string ticketInfo = "Booking " + bookingID.ToString() + "\n";
            foreach (ITicket ticket in tickets)
            {
                ticketInfo += ticket.Print();
            }
            return ticketInfo;
        }

        public decimal TotalCost()
        {
            // TODO: this method should return the total cost of the tickets in this booking
        }

        public override string ToString()
        {
            return string.Format("{0}: Total Cost={1:c}", bookingID, TotalCost());
        }

    }
}

3 个答案:

答案 0 :(得分:2)

假设Cost中有ITicket个属性,您可以使用LINQ(在文件顶部添加using System.Linq):

tickets.Select(x => x.Cost).Sum();

甚至简单地说:

tickets.Sum(x => x.Cost);

答案 1 :(得分:0)

嗯,你需要一种方法来找到你拥有的所有门票并逐一查看它们以找到一个总计。

如果查看已声明的变量(并且在调用AddTickets时使用),您认为哪种变量适合?

  

您的public List<ITicket> tickets;因为它包含我们所有门票的列表

然后我们需要使用迭代器(依次查看每个对象的东西)来累加所有总计。我们在代码中的其他任何地方都这样做了吗?

  

查看我们的Print方法 - 它使用foreach循环迭代地遍历我们的集合    foreach (ITicket ticket in tickets)  {  ticketInfo += ticket.Print();  }

然后,您可以将其与ITicket上的成本属性结合使用,以获得总计,例如

  

Decimal totalCost;  foreach (ITicket ticket in tickets)  {  totalCost += ticket.Fee;  }  return totalCost;

答案 2 :(得分:0)

重点:

您已经在打印方法中迭代了票证。使用类似的东西来增加所有费用并返回结果 1

  

decimal totalPrice = 0.0m;
 foreach (ITicket ticket in tickets)
 {
 totalPrice += ticket.Fee;
 }
 return totalPrice;

一些建设性的批评:

  • BookingIDSequence singelton确实没有必要,除非你在Booking课程之外使用它。如果您只保留预订ID的计数,请考虑将其设为Booking类的静态属性,并在Booking构造函数中指定/递增它。
    • 如果您决定保留它,虽然这可能是语义的,但最好使用带动词的方法来明确其目的是什么(例如.GetNextId()超过.NextId然后任何使用该课程的人都能理解正在发生的事情。
  • 应将门票费用迁移到门票类本身。当您添加子票证时,fee参数变得无用(但仍然需要)。此外,门票和费用是1:1,有意义的是将它们分开。
    • 您甚至可以使用ITicket等商业逻辑扩展AddDiscount(Decimal percent)
  • 您应该查看switch语句,而不是在AddTickets中使用if语句。这是一种或者某种决定,但对于枚举而言,它们通常看起来更清晰。
  • NextId一样,TotalCost会更简洁地重命名为GetTotalCost()

额外信用:

  • 在打印方法中使用StringBuilder
  • 查找工厂模式以创建ITicket类型。
相关问题