简体   繁体   中英

Using C#, troubleshooting my Flight Computer

The goal of this database is to create an exe that lets the user type in the airport by the three digit code, utilizing a .txt file. It was working before, but now does not seem to work. I will highlight the errors I have, around the .PolltheData , and .Dataset . I

First is a Project called Engine with two classes: Engine & Locations, from which I have no issues

Engine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Engine
{
    public class Engine
    {
        public const double EarthRadiusInMiles = 3959.0;
        public const double EarthRadiusInKilometers = 6371.0;
        public List<Location> Route;

        /// <summary>
        /// The Engine constructor
        /// </summary>
        public Engine()
        {
            Route = new List<Location>();
        }

        public void Add(Location Loc)
        {
            Route.Add(Loc);
        }

        /// <summary>
        /// GetRouteLength Calculates the length of all the Location objects in the Route Container
        /// </summary>
        /// <returns></returns>
        public double GetRouteLength()
        {
            double temp = 0;



            for (int i = 0; i < Route.Count; ++i)
            {
                if (Route.Count == 1)
                {
                    return 0;
                }
                if (i != this.Route.Count - 1)
                {
                    double view = this.Distance(this.Route[i], this.Route[i + 1], 'K');
                    temp = temp + this.Distance(this.Route[i], this.Route[i + 1], 'K');
                }

            }

            return temp;
        }

        /// <summary>
        /// Prints out all the elements in the Route.
        /// </summary>
        public void PrintRoute()
        {
            if (this.Route.Count != 0)
            {
                Console.WriteLine("Current Route----- {0} Locations ", this.Route.Count);
                foreach (Location Loc in this.Route)
                {
                    Console.WriteLine("{0} Lat, {1} Long,", Loc.GetLatitude(), Loc.GetLongitude());

                }
                Console.WriteLine("Total Distance = {0} {1}", this.GetRouteLength(), this.Route[0].Unit);
            }

            else
            {
                Console.WriteLine("Route is empty");
            }
        }

        /// <summary>
        /// Calculates the distance between two location objects
        /// </summary>
        /// <param name="loc1"></param>
        /// <param name="loc2"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public double Distance(Location loc1, Location loc2, char unit)
        {
            double R;
            if (unit == 'K')
            {
                R = EarthRadiusInKilometers;
            }
            else
            {
                R = EarthRadiusInMiles;
            }

            double dLat = deg2rad(loc2.GetLatitude()) - deg2rad(loc1.GetLatitude());
            double dLon = deg2rad(loc2.GetLongitude()) - deg2rad(loc1.GetLongitude());
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(deg2rad(loc1.GetLatitude())) * Math.Cos(deg2rad(loc2.GetLatitude())) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double distance = c * R;

            return Math.Round(distance, 2);
        }

        /// <summary>
        /// Calculates the distance between two sets of latitudes and longitudes
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lng1"></param>
        /// <param name="lat2"></param>
        /// <param name="lng2"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public double Distance(double lat1, double lng1, double lat2, double lng2, char unit)
        {
            double R;
            if (unit == 'K')
            {
                R = EarthRadiusInKilometers;
            }
            else
            {
                R = EarthRadiusInMiles;
            }

            double dLat = deg2rad(lat2) - deg2rad(lat1);
            double dLon = deg2rad(lng2) - deg2rad(lng1);
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat1)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double distance = c * R;

            return Math.Round(distance, 2);
        }

        /// <summary>
        /// Calculates the bearing between two location objects
        /// </summary>
        /// <param name="loc1"></param>
        /// <param name="loc2"></param>
        /// <param name="lat1"></param>
        /// <returns></returns>
        public double BearingTo(Location loc1, Location loc2)
        {
            double dlat1 = deg2rad(loc1.GetLatitude());
            double dlat2 = deg2rad(loc2.GetLatitude());
            double dLon = deg2rad(loc1.GetLongitude()) - deg2rad(loc2.GetLongitude());

            double y = Math.Sin(dLon) * Math.Cos(dlat2);
            double x = Math.Cos(dlat1) * Math.Sin(dlat2) - Math.Sin(dlat1) * Math.Cos(dlat2) * Math.Cos(dLon);
            double brng = Math.Atan2(y, x);
            return (rad2deg(brng) + 360) % 360;
        }

        /// <summary>
        /// Calculates bearing between two sets of latitudes and longitudes
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lon1"></param>
        /// <param name="lat2"></param>
        /// <param name="lon2"></param>
        /// <returns></returns>
        public double BearingTo(double lat1, double lon1, double lat2, double lon2)
        {
            double dlat1 = deg2rad(lat1);
            double dlat2 = deg2rad(lat2);
            double dLon = deg2rad(lon1) - deg2rad(lon2);

            double y = Math.Sin(dLon) * Math.Cos(dlat2);
            double x = Math.Cos(dlat1) * Math.Sin(dlat2) - Math.Sin(dlat1) * Math.Cos(dlat2) * Math.Cos(dLon);
            double brng = Math.Atan2(y, x);
            return (rad2deg(brng) + 360) % 360;
        }

        /// <summary>
        /// Calculates a location object based on a location object start, a distance and a bearing
        /// </summary>
        /// <param name="loc1"></param>
        /// <param name="dist"></param>
        /// <param name="brng"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public Location DestinationPoint(Location loc1, double dist, double brng, char unit)
        {
            double R;
            if (unit == 'K')
            {
                R = EarthRadiusInKilometers;
            }
            else
            {
                R = EarthRadiusInMiles;
            }

            dist = dist / R;
            brng = Math.PI * brng / 180;
            double lat1 = deg2rad(loc1.GetLatitude());
            double lon1 = deg2rad(loc1.GetLongitude());
            double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(dist) + Math.Cos(lat1) * Math.Sin(dist) * Math.Cos(brng));
            double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(dist) * Math.Cos(lat1), Math.Cos(dist) - Math.Sin(lat1) * Math.Sin(lat2));
            lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
            lat2 = rad2deg(lat2);
            lon2 = rad2deg(lon2);
            Location temp = new Location(lat2, lon2);
            return temp;
        }

        /// <summary>
        /// Converts degrees to radians
        /// </summary>
        /// <param name="deg"></param>
        /// <returns></returns>
        private double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        /// <summary>
        /// Converts radians to degrees
        /// </summary>
        /// <param name="rad"></param>
        /// <returns></returns>

        private double rad2deg(double rad)
        {
            return (rad / Math.PI * 180.0);
        }
    }
}

Location

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Engine
{
    public class Location
    {
        private double Latitude;
        private double Longitude;
        private double Elevation;
        public char Unit;
        public String AirportCode;
        public double GetLongitude()
        {
            return Longitude;
        }

        public double GetLatitude()
        {
            return Latitude;
        }
        public double GetElevation()
        {
            return Elevation;
        }
        public void SetLongitude(double value)
        {
            Debug.Assert(value <= 180);
            Debug.Assert(value >= -180);
            Longitude = value;
        }

        public void SetLatitude(double value)
        {
            Debug.Assert(value <= 90);
            Debug.Assert(value >= -90);
            Latitude = value;
        }

        public Location()
        {


            Latitude = 0;
            Longitude = 0;
            Elevation = 0;
            Unit = 'K';
        }

        public Location(double Lat, double Long, double Elevate)
        {
            Debug.Assert(Lat <= 90);
            Debug.Assert(Lat >= -90);
            Debug.Assert(Long <= 180);
            Debug.Assert(Long >= -180);
            Latitude = Lat;
            Longitude = Long;
            Elevation = Elevate;
            Unit = 'K';
        }

        public Location(double Lat, double Long)
        {
            Debug.Assert(Lat <= 90);
            Debug.Assert(Lat >= -90);
            Debug.Assert(Long <= 180);
            Debug.Assert(Long >= -180);

            Latitude = Lat;
            Longitude = Long;
            Elevation = 0;
            Unit = 'K';
        }

        public Location(String Code, int deglat, int minlat, int seclat, String parrallel, int deglong, int minlong, int seclong, String meridian, double Elevate)
        {
            //I am giving you this code and hiding it in the new version of this class to make coding the project a bit easier. Time is against us.
            //   Debug.Assert(meridian.ToUpper() == "E" || meridian.ToUpper() == "W");
            //   Debug.Assert(parrallel.ToUpper() == "N" || parrallel.ToUpper() == "S");
            double Long = Convert.ToDouble(deglong) + (Convert.ToDouble(minlong) / 60.00) + (Convert.ToDouble(seclong) / 60.00);
            double Lat = Convert.ToDouble(deglat) + (Convert.ToDouble(minlat) / 60.00) + (Convert.ToDouble(seclat) / 60.00);

            if (meridian.ToUpper() == "E")
            {

            }
            else if (meridian.ToUpper() == "W")
            {
                Long = Long * -1;
            }
            else
            {
                Code = "ERR";

            }


            if (parrallel.ToUpper() == "N")
            {

            }
            else if (parrallel.ToUpper() == "S")
            {
                Lat = Lat * -1;
            }
            else
            {
                AirportCode = "ERR";
            }
            if (Lat > 90 || Lat < -90 || Long > 180 || Long < -180 || AirportCode == "ERR")
                {
                Lat = 0;
                Long = 0;
                Elevate = 0;
                Code = "ERR";
                }


            Debug.Assert(Lat <= 90);
            Debug.Assert(Lat >= -90);
            Debug.Assert(Long <= 180);
            Debug.Assert(Long >= -180);

            AirportCode = Code;
            Latitude = Lat;
            Longitude = Long;
            Elevation = Elevate;
            Unit = 'K';

        }

    }
}

In flight console I am having issues in both classes. In Airports I am getting an error under DataSet in this.DataSet.Add(item)

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using FlightConsole;


namespace FlightConsole
{
    public class Airports
    {

        public Airports()
        {

            try
            {
                string currentDirectory = Directory.GetCurrentDirectory();
                List<string> list = File.ReadLines(currentDirectory + "\\GlobalAirportDatabase.txt").ToList();
                this.DataSet = new List<Location>();
                foreach (string item2 in list)
                {
                    string[] array = item2.Split(':');
                    Location item = new Location(array[1].ToString(), int.Parse(array[5]), int.Parse(array[6]), int.Parse(array[7]), array[8], int.Parse(array[9]), int.Parse(array[10]), int.Parse(array[11]), array[12], double.Parse(array[13]));
                    this.DataSet.Add(item);
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("The file is missing.");
                throw new FileNotFoundException();
            }
            catch (Exception ex2)
            {
                Console.WriteLine("Something went wrong.");
                throw ex2;
            }
            finally
            {
            }
        }

    }
}

And in program i am having an issue under PollTheData in the areas where airports.PollTheData(text) are in place.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using FlightConsole;
using Engine;


namespace FlightConsole
{



    internal class Program
    {
        private static void RefreshConsole(Engine.Engine inputEngine)
        {
            Console.Clear();
            Console.WriteLine("Van Moorsel Flight Computer Version 1.0");
            Console.WriteLine("");
            if (inputEngine.Route.Count > 0)
            {
                Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", "Start", "Destination", "Distance", "Bearing");
                Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", inputEngine.Route[0].AirportCode, "", "", "");
                for (int i = 1; i < inputEngine.Route.Count; i++)
                {
                    Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", inputEngine.Route[i - 1].AirportCode, inputEngine.Route[i].AirportCode, inputEngine.Distance(inputEngine.Route[i - 1], inputEngine.Route[i], 'K'), Math.Round(inputEngine.BearingTo(inputEngine.Route[i], inputEngine.Route[i - 1]), 2));
                }
                Console.WriteLine("Total Number of Waypoints: {0}  Total Distance: {1}", inputEngine.Route.Count, inputEngine.GetRouteLength());
            }
            else
            {
                Console.WriteLine("");
            }
        }

        private static void Main(string[] args)
        {
            bool flag = true;
            bool flag2 = true;
            try
            {
                Engine.Engine engine = new Engine.Engine();
                Airports airports = new Airports();
                while (flag)
                {
                    Program.RefreshConsole(engine);
                    flag2 = true;
                    Console.WriteLine("Enter your next destination, or quit pressing Q");
                    while (flag2)
                    {
                        string text = Console.ReadLine();
                        if (airports.PollTheData(text) != null)
                        {
                            engine.Route.Add(airports.PollTheData(text));
                            flag2 = false;
                        }
                        else
                        {
                            flag2 = false;
                        }
                        if (text.ToUpper() == "Q")
                        {
                            flag2 = false;
                            flag = false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Whoops something went wrong");
                Console.WriteLine("Press any key to end program.");
                Console.ReadLine();
                flag2 = false;
                flag = false;
            }
        }
    }
}

The errors state:

Error CS1061 'Airports' does not contain a definition for 'DataSet' and no extension method 'DataSet' accepting a first argument of type 'Airports' could be found (are you missing a using directive or an assembly reference?)

FlightConsole C:\\Users\\Desktop\\COMP6034_ProgrammingBasics\\FinalPlag\\FMS Student\\FlightConsole\\Airports.cs

and are in both the airport and program files.

The "this" in line "this.DataSet = new List();" refers to the containing class, which is Airports. As you've listed it, there is no property or field within Airports that has that name - did you accidentally delete some lines perhaps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM