我是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());
}
}
}
您需要將ITicket定義添加到您的示例中。 – martijn 2014-12-02 15:35:57
看起來很有趣,祝你好運!跟上你的進步。 – 2014-12-02 15:35:57
當您已經在PrintTickets中使用票據文本時,您是否總結了所有票據的費用? – nvoigt 2014-12-02 15:37:00