© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4w ago•
2 replies
Bryce

How to make this more oop firendly?

beginner
namespace MTA_Transit;
// using https://github.com/MobilityData/gtfs-realtime-bindings/ package
using ProtoBuf;
using TransitRealtime;
using System.Net.Http;



/// <summary>
/// Represents a parser for MTA GTFS-realtime data feeds for multiple subway lines.
/// </summary>
/// <remarks>
/// This class fetches and parses GTFS-realtime feed data from the MTA API for specific subway lines:
/// Orange, Blue, Yellow, and Red/Green.  
/// It stores the feed URLs internally and provides asynchronous methods to fetch and process the data.  
/// Parsing focuses on FeedEntities, such as TripUpdates, which can then be used for further processing or display.
/// </remarks>
public class ParseMtaData
{
    private static readonly HttpClient Client = new HttpClient();
    public async Task FindTrainStatus(string line,string lineDirection, string api)
    {
        try
        {
            using HttpResponseMessage response = await Client.GetAsync(api);
            response.EnsureSuccessStatusCode();
            await using var stream = await response.Content.ReadAsStreamAsync();
            FeedMessage feed = Serializer.Deserialize<FeedMessage>(stream);
            foreach (FeedEntity entity in feed.Entities) {
                if (entity.Vehicle != null && ( entity.Vehicle.StopId.Contains(line) && entity.Vehicle.StopId.Contains(lineDirection)))
                {
                    Console.WriteLine(entity.Vehicle.CurrentStatus);
                }
            }
        }    
        catch (HttpRequestException e)
        {
  //redacted
        }

    }
    
}
namespace MTA_Transit;
// using https://github.com/MobilityData/gtfs-realtime-bindings/ package
using ProtoBuf;
using TransitRealtime;
using System.Net.Http;



/// <summary>
/// Represents a parser for MTA GTFS-realtime data feeds for multiple subway lines.
/// </summary>
/// <remarks>
/// This class fetches and parses GTFS-realtime feed data from the MTA API for specific subway lines:
/// Orange, Blue, Yellow, and Red/Green.  
/// It stores the feed URLs internally and provides asynchronous methods to fetch and process the data.  
/// Parsing focuses on FeedEntities, such as TripUpdates, which can then be used for further processing or display.
/// </remarks>
public class ParseMtaData
{
    private static readonly HttpClient Client = new HttpClient();
    public async Task FindTrainStatus(string line,string lineDirection, string api)
    {
        try
        {
            using HttpResponseMessage response = await Client.GetAsync(api);
            response.EnsureSuccessStatusCode();
            await using var stream = await response.Content.ReadAsStreamAsync();
            FeedMessage feed = Serializer.Deserialize<FeedMessage>(stream);
            foreach (FeedEntity entity in feed.Entities) {
                if (entity.Vehicle != null && ( entity.Vehicle.StopId.Contains(line) && entity.Vehicle.StopId.Contains(lineDirection)))
                {
                    Console.WriteLine(entity.Vehicle.CurrentStatus);
                }
            }
        }    
        catch (HttpRequestException e)
        {
  //redacted
        }

    }
    
}
My goal with this project is to learn the basics of oop with a real project. Here we have a class I made with the intention of being the object that parses the MTA (subway) data from their api. This is the first method i wrote just to get it started, I quickly realized I'd need to re-write the same portion of this code for each filter/method
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How to make this Faster?
C#CC# / help
2y ago
✅ How to make this loop
C#CC# / help
3y ago
Best way to do this OOP-wise???
C#CC# / help
2y ago
❔ Is there a way to make this more cleaner?
C#CC# / help
3y ago