E28 Ex7
This commit is contained in:
parent
ac4ac67414
commit
101d6c45c9
@ -172,10 +172,44 @@ namespace Exercises_C_Sharp.E28_Classes
|
||||
public List<Item> Items = new List<Item>();
|
||||
|
||||
//Schreiben Sie hier eine Methode, die die Anzahl aller einzelnen Elemente zurückgibt
|
||||
public int GiveAmountOfItems()
|
||||
{
|
||||
int counter = 0;
|
||||
foreach(var element in Items)
|
||||
counter += element.Amount;
|
||||
|
||||
return counter;
|
||||
|
||||
//ODER:
|
||||
return Items.Sum(k => k.Amount);
|
||||
}
|
||||
|
||||
//Schreiben Sie hier eine Methode, die die Anzahl der verschiedenen Produkte zurückgibt
|
||||
public int AmountOfProducts()
|
||||
{
|
||||
List<string> tempList = [];
|
||||
int sum = 0;
|
||||
foreach(var element in Items)
|
||||
if(!tempList.Contains(element.Product.Name))
|
||||
{
|
||||
sum += 1;
|
||||
tempList.Add(element.Product.Name);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
//Schreiben Sie hier eine Methode, die den Gesamtpreis der Rechnung zurück gibt
|
||||
public decimal GetWholePrice()
|
||||
{
|
||||
decimal d = 0;
|
||||
foreach(var element in Items)
|
||||
if(element != null && element.Product != null)
|
||||
d += (element.Amount * element.Product.Price);
|
||||
return d;
|
||||
|
||||
return Items.Sum(k => k.Product != null ? k.Amount * k.Product.Price : 0);
|
||||
}
|
||||
}
|
||||
class Item
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user