2014-12-02 65 views
0

我是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()); 
     } 

    } 
} 
+2

您需要將ITicket定義添加到您的示例中。 – martijn 2014-12-02 15:35:57

+1

看起來很有趣,祝你好運!跟上你的進步。 – 2014-12-02 15:35:57

+2

當您已經在PrintTickets中使用票據文本時,您是否總結了所有票據的費用? – nvoigt 2014-12-02 15:37:00

回答

2

假設有在ITicket一個Cost屬性,你可以使用LINQ(添加using System.Linq在你的文件的頂部):

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

甚至乾脆:

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

爲什麼'tickets.Sum(t => t.Cost)'? – decPL 2014-12-02 15:37:05

+1

@decPL僅僅因爲我不記得這個超載存在! – 2014-12-02 15:38:16

0

那麼您需要一種方法來查找所有您擁有的門票,並逐一查找總計。

如果你看看你已經聲明的變量(當你調用AddTickets時正在使用),你認爲什麼是合適的?

public List<ITicket> tickets;因爲它擁有我們所有的門票清單

然後,我們需要使用一個迭代器(的東西,將依次查看每個對象)來加起來我們所有的總數。我們在代碼的其他地方做過這些嗎?

看在我們的打印方法 - 它通過我們的收藏反覆使用foreach循環 foreach (ITicket ticket in tickets) { ticketInfo += ticket.Print(); }

然後,您可以只結合本與您的ITicket的成本屬性獲得運行總和,例如

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

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,並使其分開。
    • 你甚至可以與業務邏輯擴展ITicketAddDiscount(Decimal percent)
  • 而不是使用,如果你應該看看switch語句中AddTickets語句。這是一個決定,或者是一種決定,但是對於枚舉它們通常更清晰。
  • NextId相同,TotalCost會更簡潔,重命名爲GetTotalCost()

附加題:

  • 看看你的打印方法,使用StringBuilder
  • 查看用於創建ITicket類型的工廠模式。
相關問題