Git Product home page Git Product logo

csharp-book-1-chapter-5-common-types-dictionaries-stocks's Introduction

CSharp-Book-1-Chapter-5-Common-Types-Dictionaries-Stocks

Exercise to learn about common c # data types. Dictionaries.

Practice: Stock Purchase Dictionaries Setup mkdir -p ~/workspace/csharp/exercises/dictionaries && cd $_ dotnet new console References C# dictionaries Dictionary in C# Interactive C# Dictionaries Instructions A block of publicly traded stock has a variety of attributes, we'll look at a few of them. A stock has a ticker symbol and a company name. Create a simple dictionary with ticker symbols and company names in the Main method.

Example Dictionary<string, string> stocks = new Dictionary<string, string>(); stocks.Add("GM", "General Motors"); stocks.Add("CAT", "Caterpillar"); // Add a few more of your favorite stocks To find a value in a Dictionary, you can use square bracket notation much like JavaScript object key lookups.

string GM = stocks["GM"]; <--- "General Motors" Next, create a list to hold stock purchases by an investor. The list will contain dictionaries.

List<Dictionary<string, double>> purchases = new List<Dictionary<string, double>>(); Then add some purchases.

Example purchases.Add (new Dictionary<string, double>(){ {"GE", 230.21} }); purchases.Add (new Dictionary<string, double>(){ {"GE", 580.98} }); purchases.Add (new Dictionary<string, double>(){ {"GE", 406.34} });

// Add more purchases for each stock Create a total ownership report that computes the total value of each stock that you have purchased. This is the basic relational database join algorithm between two tables.

Helpful Links: ContainsKey, Add

/* Define a new Dictionary to hold the aggregated purchase information. - The key should be a string that is the full company name. - The value will be the total valuation of each stock

From the three purchases above, one of the entries
in this new dictionary will be...
    {"General Electric", 1217.53}

Replace the questions marks below with the correct types.

*/ Dictionary stockReport = new Dictionary();

/* Iterate over the purchases and record the valuation for each stock. */ foreach (Dictionary<string, double> purchase in purchases) { foreach (KeyValuePair<string, double> stock in purchase) { // Does the full company name key already exist in the stockReport?

    // If it does, update the total valuation

    /*
        If not, add the new key and set its value.
        You have the value of "GE", so how can you look
        the value of "GE" in the `stocks` dictionary
        to get the value of "General Electric"?
    */
}

} Now that the report dictionary is populated, display the final results.

foreach(KeyValuePair item in stockReport) { Console.WriteLine($"The position in {display the key} is worth {display the value}"); }

csharp-book-1-chapter-5-common-types-dictionaries-stocks's People

Contributors

loetek avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.