0

To start off I am new to C# and I am in need of some help. I a class that contains a list. I can set the items in the list from the application but not from the class(where it needs to be done). I am also needing to move an event to class as well. This works in the app as well. Any help with this is greatly appreciated. Below is the code from my class:

    namespace CarRace
{
class Cars
{
    public string Name { get; set; }
    public int StartPOS { get; set; }
    public int Speed { get; set; }
    public int CurPOS { get; set; }
    public double Location { get; set; }
    public Cars(string Name, int StartPOS, int Speed, int CurPOS, long Location)
    {
        this.Name = Name;
        this.StartPOS = StartPOS;
        this.Speed = Speed;
        this.CurPOS = 0;
        this.Location = 0;
    }
    public static int CompareCurrentLocation(Cars c1, Cars c2)
    {
        return c2.Location.CompareTo(c1.Location);
    }
    }
}

I am needing to add this to the class:

if (File.Exists("NewRace.xml"))
            {
                XDocument doc = XDocument.Load("NewRace.xml");
                var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0)
                {
                    Name = x.Attribute("Name").Value,
                    StartPOS = int.Parse(x.Attribute("StartPOS").Value),
                    Speed = int.Parse(x.Attribute("Speed").Value),
                    CurPOS = 0
                });
            }

And this:

int p = 1;
        int prevPOS = 1;
        DateTime? PrevTime;
        double dist = 0;
        double PrevLoc = 0;
        if (i == 0)
        {
            return;
        }
        foreach (var car in RaceList)
        {
            dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
            car.Location = car.Location + dist;
        }
        Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
        RaceList.Sort(compLoc);
        PrevTime = DateTime.Now;
        foreach (var car in RaceList)
        {
            if (car.Location != PrevLoc)
            {
                car.CurPOS = p;
            }
            else
            {
                car.CurPOS = prevPOS;
            }
            prevPOS = car.CurPOS;
            PrevLoc = car.Location;
            p++;
        }

Thanks

5
  • 1
    Where is this class that contains a list? I don't see a list anywhere. Commented Jul 7, 2012 at 14:43
  • 1
    Seems like you need multiple classes, each with a different responsibility. One to keep track of the cars and their positions; one to modify those positions; and one to read initial values from an XML file. They don't all have to be done in one class: Each class should have one responsibility (and only one responsibility). Commented Jul 7, 2012 at 14:44
  • Your code "(((car.Speed * 1609.344) * 1) / 1609.344) / 3600" is the same as "car.Speed/3600". Not sure what you're trying to do there but whatever it is, that's not doing it. Commented Jul 7, 2012 at 15:21
  • @MichaelGraczyk From the number 1609.344, something to do with Miles to Metres and back again. Commented Jul 7, 2012 at 16:18
  • Yeah it appears he is trying to go from m/s to mph but that isn't right. Commented Jul 7, 2012 at 23:59

2 Answers 2

1

Thanks for all of your replies. I tried the car.speed/3600 but only get back 0 for that so I did the long way. I did get everything working the way I needed it to.

That's because you are doing an integer division (car.Speed is of type int) so the fractional part of the result is discarded. Since the car's speed is less than 3600 this would hence always result in zero. You can avoid this by casting car.Speed to a double first - this should produce the result you want:

dist = (double)car.Speed / 3600;
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for all of your replies. I tried the car.speed/3600 but only get back 0 for that so I did the long way. I did get everything working the way I needed it to. I am a rookie at this and any tips are greatly appreciated. Below is the code I have updated to.

namespace CarRace
{
class Cars
{
    public string Name { get; set; }
    public int StartPOS { get; set; }
    public int CurPOS { get; set; }
    public int Speed { get; set; }
    public double Location { get; set; }
    public string Make { get; set; }
    private static List<Cars> CarList;
    private static string ProgPart;
    public Cars(string Name, int StartPOS, int CurPOS, int Speed, double Location, string Make)
    {
        this.Name = Name;
        this.StartPOS = StartPOS;
        this.CurPOS = CurPOS;
        this.Speed = Speed;
        this.Location = Location;
        this.Make = Make;
    }
    public static List<Cars> FillList()
    {
        return CarList;
    }
    public static void Main()
    {
        try
        {
            bool Prog = false;
            //Get Part to display
            while (!Prog)
            {
                Console.WriteLine("Select the part you would like to test.(1 or 2)");
                ProgPart = Console.ReadLine();
                if (ProgPart == "1" || ProgPart == "2")
                {
                    Prog = true;
                }
            }
            Console.WriteLine("----------------------------------------");
            //Read XML File and set the CarList
            if (File.Exists("NewRace.xml"))
            {
                XDocument doc = XDocument.Load("NewRace.xml");
                var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0, "")
                {
                    Name = x.Attribute("Name").Value,
                    StartPOS = int.Parse(x.Element("StartPOS").Value),
                    CurPOS = 0,
                    Speed = int.Parse(x.Element("Speed").Value),
                    Location = double.Parse(x.Element("Location").Value),
                    Make = x.Attribute("Make").Value
                });
                CarList = new List<Cars>();
                foreach (var car1 in nCars)
                {
                    if (ProgPart == "1")
                    {
                        CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, car1.Speed, car1.Location, car1.Make));
                    }
                    else
                    {
                        CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, 0, car1.Location, car1.Make));
                    }
                }
            }
            else
            {
                Console.WriteLine("File Not Found!");
                Console.ReadKey();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    public static int CompareCurrentLocation(Cars c1, Cars c2)
    {
        return c2.Location.CompareTo(c1.Location);
    }
    public static Boolean UpdateLeaderBoard(long i)
    {
        try
        {
            int p = 1;
            int prevPOS = 1;
            DateTime? PrevTime;
            double dist = 0;
            double prevLocation = 0;
            if (i == 0)
            {
                return false;
            }
            foreach (var car in CarList)
            {
                if (ProgPart == "2")
                {
                    switch (car.Make)
                    {
                        case "300ZX":
                            //Slow continuous gain of speed by percentage after 60 with top speed of 320.
                            if (i <= 15)
                            {
                                car.Speed = car.Speed + (60 / 10);
                            }
                            else if (car.Speed < 320)
                            {
                                double percent = (double)(car.Speed * .1);
                                if ((Convert.ToInt32(car.Speed + percent)) > 320)
                                {
                                    car.Speed = 320;
                                }
                                else
                                {
                                    car.Speed = Convert.ToInt32(car.Speed + (percent));
                                }
                            }
                            break;
                        case "Camero":
                            //0-60 4 seconds 60-220 20 seconds Top 220
                            if (i <= 4)
                            {
                                car.Speed = car.Speed + (60 / 4);
                            }
                            else if (i <= 20)
                            {
                                car.Speed = car.Speed + 10;
                            }
                            break;
                        case "Mustang":
                            //0-top(210) 25 seconds
                            if (car.Speed <= 200)
                            {
                                car.Speed = car.Speed + (200 / 20);
                            }
                            break;
                        case "VW Bug":
                            //Constant Speed
                            car.Speed = 165;
                            break;
                        default:
                            Console.WriteLine("Make not found");
                            break;
                    }
                }
                //Set Cars Current Location
                dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
                car.Location = car.Location + dist;
            }
            //Sort list by location
            Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
            CarList.Sort(compLoc);
            PrevTime = DateTime.Now;
            //Set the Current Position
            foreach (var car in CarList)
            {
                if (car.Location != prevLocation)
                {
                    car.CurPOS = p;
                }
                else
                {
                    car.CurPOS = prevPOS;
                }
                prevPOS = car.CurPOS;
                prevLocation = car.Location;
                p++;
            }
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.