I have this homework, I have only 1 problem, and I don’t know the solution. We have this class, and we musn’t create another variables or methods…
I have a beers dictionary with < Beer object, int income >. But the method has got only the Beer object’s name (prop), not the object.
And I don’t have another idea, how can I get the Beer object’s name from a Dictionary
I have only 2 idea, but these don’t work.
The first is I tried use a ContainsKey() method. The second is an foreach iteration
using System;
using System.Collections.Generic;
namespace PubBeer
{
public class Beer
{
string name;
int price;
double alcohol;
public string Name{ get { return name; } }
public int Price{ get; set; }
public double Alcohol{ get { return alcohol;} }
public Sör(string name, int price, double alcohol)
{
this.name= name;
this.price= price;
this.alcohol= alcohol;
}
public override bool Equals(object obj)
{
if (obj is Beer)
{
Beer other = (Beer)obj;
return this.name== other.name;
}
return false;
}
}
public class Pub
{
int income;
IDictionary<Beer, int> beers= new Dictionary<Beer, int>();
public int Income{ get; set; }
public int Sold(string beerName, int mug)
{
// Here the problem
beers; // Here I want to like this: beers.Contains(beerName)
// beers.ContainsKey(Object.Name==beerName) or someone like this
// foreach (var item in beers)
// {
// item.Key.Name== beerName;
// }
}
...
Use LINQ to query over the collection of Keys.
Update: NON-LINQ Alternative