Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:00:49+00:00 2026-05-27T02:00:49+00:00

I have this homework, I have only 1 problem, and I don’t know the

  • 0

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;
           //  }


        }
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:00:50+00:00Added an answer on May 27, 2026 at 2:00 am

    Use LINQ to query over the collection of Keys.

    //Throws an error if none or more than one object has the same name.
    var beer = beers.Keys.Single(b => b.Name == beerName);
    
    beers[beer] = ...;
    
    // -or -
    
    //Selects the first of many objects that have the same name.
    //Exception if there aren't any matches.
    var beer = beers.Keys.First(b => b.Name == beerName);
    
    beers[beer] = ...;
    
    // -or -
    
    //Selects the first or default of many objects.
    var beer = beers.Keys.FirstOrDefault(b => b.Name == beerName);
    
    //You'll need to null check
    if (beer != null)
    {
        beers[beer] = ...;
    }
    
    // etc...
    

    Update: NON-LINQ Alternative

    Beer myBeer;
    
    foreach (var beer in beers.Keys)
    {
        if (beer.Name == beerName)
        {
            myBeer = beer;
            break;
        }
    }
    
    if (myBeer != null)
    {
        beers[myBeer] = ...;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you don't like helping others in their homework but I have to
I have this homework problem where I need to use regex to remove every
The following is a homework problem for an assembly language class. We're to create
I have this insane homework where I have to create an expression to validate
Considering this is only for my homework I don't expect much help but I
This is not a homework problem. I am only going over a freely available
(this is indirectly a part of a much larger homework assignment) I have something
I have this idea for a free backup application. The largest problem I need
I have a problem with classes. I made my own class called Person. Which
This is not a homework question, but rather my intention to know if this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.