C# :: Aufgabe #55

3 Lösungen Lösungen öffentlich

Aktuellen Aktienkurs auslesen und ausgeben

Fortgeschrittener - C# von Gustl - 08.11.2013 um 19:57 Uhr
Schreibe ein Konsolenprogramm welches den aktuellen Aktienkurs von eine Aktien deiner Wahl ausgibt.
Benutze hierfür eine API. Als kleine Anregung: yahoo finance API

Lösungen:

vote_ok
von pocki (4190 Punkte) - 11.11.2013 um 15:58 Uhr
Quellcode ausblenden C#-Code
async void Main()
{
	System.Console.Write("Geben sie das Kürzel ein oder mehrerer Aktien ein (z.B.: \"MSFT,GOOG\" für Microsoft + Google): ");
	string aktie = Console.ReadLine();
	const string url = "http://download.finance.yahoo.com/d/quotes.csv?s={0}&f=nl1&e=.csv";
	
	//Abrufen der Daten
	WebClient wc = new WebClient();
	var data = await wc.DownloadStringTaskAsync(string.Format(url, aktie));
	
	if (!string.IsNullOrEmpty(data))
	{
		//Aufteilen in die einzelnen Aktien
		foreach (var item in data.Trim().Split('\n'))
		{
			System.Console.WriteLine("Der aktuelle Kurs von {0}:\t{1}$", item.Trim().Split(','));
		}
	}
	Console.ReadLine();
}
1x
vote_ok
von eulerscheZhl (5230 Punkte) - 15.12.2014 um 17:22 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Net;

namespace trainYourProgrammer
{
	class MainClass
	{
		private static void readValues(string company) {
			WebClient client = new WebClient ();
			string[] vals = client.DownloadString ("http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI," + company + "&f=nsl1op&e=.csv")
				.Split(',');
			Console.WriteLine ("Aktienkurs von " + vals [0] + " (" + vals[1] + ")");
			Console.WriteLine ("    Aktueller Kurs " + vals [2]);
			Console.WriteLine ("    Eröffnungswert " + vals [3]);
			Console.WriteLine ("    Schlusswert Vortag: " + vals [4] + "\n");
		}

		static void Main(string[] args)
		{
			string[] companies = {"GOOG", "IBM", "MSFT"};
			foreach (string s in companies)
				readValues(s);
		}
	}
}
1x
vote_ok
von aheiland (650 Punkte) - 13.03.2015 um 16:13 Uhr
Quellcode ausblenden C#-Code
    class Program
    {
        static void Main(string[] args)
        {
            Random zufall = new Random();
            int found = 0;
            do
            {
                var symbol = "" + (char)zufall.Next('A', 'Z' + 1) + (char)zufall.Next('A', 'Z' + 1);
                var pf = new Finance(symbol).portfolio;
                if (pf != null)
                {
                    found++;
                    Console.WriteLine("Name: " + pf.name);
                    Console.WriteLine("Ask: " + pf.ask + " Bid: " + pf.bid);
                    Console.WriteLine("Open: " + pf.open);
                }
            } while (found < 5);
            Console.WriteLine("Finished");
            Console.ReadKey();
        }
    }

    class Finance
    {
        private string symbol;

        public Finance(string symbol)
        {
            this.symbol = symbol;
        }

        private Portfolio _portfolio;
        public Portfolio portfolio
        {
            get
            {
                if (_portfolio == null) _portfolio = getPortfolio();
                return _portfolio;
            }
        }

        private Portfolio getPortfolio()
        {
            Portfolio pf = new Portfolio();
            try
            {
                var deS = getJsonReader();
                do
                {
                    if (deS.TokenType == JsonToken.PropertyName && deS.ValueType == typeof(String))
                    {
                        var value = Portfolio.Fields.FirstOrDefault(v => v.Equals(deS.Value));
                        if (value != null)
                        {
                            deS.Read();
                            if (value.Equals(Portfolio.Fields.First()) && deS.Value == null)
                            {
                                break;
                            }
                            pf.set(value, (string)deS.Value);
                        }
                    }
                } while (deS.Read());
                deS.Close();
            }
            catch (Exception e) { }
            if (pf.name == null) return null;
            return pf;
        }

        private JsonTextReader getJsonReader()
        {
            var uri = new Uri("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol=%22" + symbol + "%22&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys");
            return new JsonTextReader(new StreamReader(new WebClient().OpenRead(uri)));
        }

    }    

    class Portfolio
    {
        public static string[] Fields = { "Name", "Ask", "Bid", "Open" };
        public string name { get; private set; }
        public string ask { get; private set; }
        public string bid { get; private set; }
        public string open { get; private set; }

        public void set(string value, string p)
        {
            if (Fields[0].Equals(value))
            {
                name = p;
            }
            if (Fields[1].Equals(value))
            {
                ask = p;
            }
            if (Fields[2].Equals(value))
            {
                bid = p;
            }
            if (Fields[3].Equals(value))
            {
                open = p;
            }
        }
    }
2097981

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.