如何在 C# 中计算和返回总值
本文关键字:返回 计算 | 更新日期: 2023-09-27 18:33:20
我正在尝试添加一个方法Takings
,该方法计算并返回此节目所有预订的总价值。如果有人能带领我走向正确的方向,那就太好了:)
namespace GCUShows
{
public class Show
{
private string title;
private Dictionary<int, Booking> bookings;
public string Title
{
get { return title; }
set { title = value; }
}
public Show(string title)
{
this.title = title;
this.bookings = new Dictionary<int, Booking>();
}
public void AddBooking(int bookingID, Booking booking)
{
this.bookings.Add(bookingID, booking);
}
public void RemoveBooking(int bookingID, Booking booking)
{
this.bookings.Remove(bookingID);
}
public Takings
{
}
}
namespace GCUShows
{
public enum TicketType
{
Adult,
Child,
Family
}
public class Booking : ITicket
{
private const int LIMIT = 6;
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)
{
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()
{
decimal totalCost;
foreach (ITicket ticket in tickets)
{
totalCost += ticket.Fee;
}
return totalCost;
}
public override string ToString()
{
return string.Format("{0}: Total Cost={1:c}", bookingID, TotalCost());
}
}
}
假设Bookings
包含一个属性int Val
,它可能看起来像:
public int Takes()
{
return bookings.Values.Sum(b => b.Val);
}
如果预订具有成本属性,则可以执行以下操作:
var total = bookings.Sum(x => x.Value.Cost);
或者,
对于其他Linq答案,您可以使用foreach
执行相同的操作
public decimal Takings()
{
decimal runningTotal = 0;
foreach (KeyValuePair<int, Booking> kvp in bookings) {
runningTotal += kvp.Value.TotalCost();
}
return runningTotal;
}
如果你是编程新手,这可能看起来不那么"神奇"。
这是我的预订舱位
命名空间 GCUShows{ 公共枚举 票证类型 { 成人 孩子 家庭 }
public class Booking : ITicket
{
private const int LIMIT = 6;
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)
{
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()
{
decimal totalCost;
foreach (ITicket ticket in tickets)
{
totalCost += ticket.Fee;
}
return totalCost;
}
public override string ToString()
{
return string.Format("{0}: Total Cost={1:c}", bookingID, TotalCost());
}
}
}