get set help using a structure and arraylist - c#

enter link description hereI am trying to make (private int vr;) to work in the loop so I could change it easily, however I have no idea how to make a get / set for this, or If i have to use get set at all.
I hope this is enough information if further info in necessary please leave a comment saying so.
struct mp3players
{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
private static ArrayList mps;
static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
}
static void mutteer()
{
int i = 1;
int a;
startloop:
while (i == 1)
{
Console.WriteLine("Wat is het Id van de mp3 speler?");
try
{
a = Convert.ToInt16(Console.ReadLine());
}
catch
{
Console.WriteLine("Dat is geen nummer waarde!");
goto startloop;
}
if (a == 1)
{
Console.WriteLine("Wat is het nieuwe voorraad");
try
{
as you can see I wrote vr here what obviously doesn't work,
I need to use the set and get here in my understanding but how do I get a get and set to work with it?
VR = Convert.ToInt32(Console.ReadLine());
i = 2;
}
catch
{
Console.WriteLine("Dat is geen nummer waarde!");
goto startloop;
}
}
}
}

Related

Why is the order of the fields in datagridview wrong? (Reading file in datagridview)

Here is my primary abstract class Citizen containing essential attributes for the derived classes:
abstract class Citizen
{
public string firstname;
private string lastname;
private int age;
public string Name
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string Surname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public int Year
{
get
{
return age;
}
set
{
age = value;
}
}
}
And here is my derived class warrior:
class Warrior : Citizen
{
string weapon;
double height;
double weight;
public string Weapon
{
get
{
return weapon;
}
set
{
weapon = value;
}
}
public double Height
{
get
{
return height;
}
set
{
height = value;
}
}
public double Weight
{
get
{
return height;
}
set
{
height = value;
}
}
}
Program is rying to read the file and write it into the datagrid:
public partial class Form1 : Form
{
List<Warrior> values = new List<Warrior>();
Form2 form2 = new Form2(0);
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
int N = 0;
try
{
openFileDialog1.ShowDialog();
}
catch { MessageBox.Show("You have not chosen any files."); }
try
{
StreamReader read = new StreamReader(openFileDialog1.FileName,Encoding.Default);
do
{
read.ReadLine();
N++;
}
while (!read.EndOfStream);
Warrior[] warrior = new Warrior[N];
string[] temp;
read = new StreamReader(openFileDialog1.FileName, Encoding.Default);
for (int i = 0; i < N; i++)
{
temp = read.ReadLine().Split(' ');
warrior[i] = new Warrior();
warrior[i].Surname = temp[0];
warrior[i].Name = temp[1];
warrior[i].Year = int.Parse(temp[2]);
warrior[i].Weapon = temp[3];
values.Add(warrior[i]);
}
catch
{
MessageBox.Show("The file is either filled wrongly or corrupted.");
Application.Exit();
}
bindingSource1.DataSource = values;
dataGridView1.DataSource = bindingSource1;
this.Size = new Size(dataGridView1.Size.Width + 10, this.Size.Height);
}
And when I launch the program I get this:
So why have I got additional fields like weapon, height etc coming first? How to make it display name,surname and age first?

That name doesn"t exist in the current context error

In public static void ammount() I get an error saying:
"The name 'mps' does not exist in the current context"
How do I fix this error?
struct mp3players{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
ArrayList mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
This is where the error occurs.
I tried a couple of ways but did not find what I was searching for.
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
I am not very good in C# so an explanation what I did wrong is also very welcome!
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
this code has knowledge of mps.
you would add a parameter the method so public static void ammount() looks like public static void ammount(mp3players[] mps). that will fix this error but cause another
you will have to pass mps into the method.
mps is a local variable inside mp3() method. It is not a global variable, so its not accessible in ammount() method.
I got your point , you are getting compilation error The name 'mps' does not exist in the current context. So to fix this, you have to make mps as global variable and set that value in mp3() method, and use that in other methods as well.
struct mp3players
{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
private static ArrayList mps;
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
The variable 'mps' declared inside the function public static void mp3() and you are trying to access the same from another function public static void ammount() which can't be feasible due to the scope.
To access the 'mps' you have to remove the declaration from the mp3() function and move it directly uder you so defined class:
public class MySongColl
{
private static ArrayList mps = new ArrayList();
struct mp3players
{
....
....
}
public static void mp3()
{
....
....
}
public static void ammount()
{
....
....
}
}
NOTE:
Use proper Naming convention while you write code. e.g: Speeling mistake in Function name ammount() should be amount(). And use camel case while declaring the names like struct mp3players should be Mp3Players etc. These are not necessary to build the code but essential for a developer.

A already defines a member called B with the same parameter - stuck [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to make a Simon Says game for Windoes Phone 8, and I've hit a wall and can't figure out what's wrong... I'd greatly appreciate some help.
The problems are with the "continueGame" and "initializeColorSequence" paramenters.
Cheers.
{
namespace SimonSez
{
public sealed partial class MainPage : PhoneApplicationPage
{
#region Variables
//Variables
const int colours_amount = 4;
const int colour1 = 1;
const int colour2 = 2;
const int colour3 = 3;
const int colour4= 4;
const int max_seq_count = 100; // TODO: Make 100 for release version
const String startgame_click = "Click to begin!";
const String win = "YOU WIN!";
int currentLengthOfSequence = 0;
Random randomNumber;
int[] correctColorSequence;
int[] playerColorSequence;
int correctColorSequenceIndex = 0;
int playerColorSequenceIndex = 0;
Boolean isPlayerTurn = true;
Boolean isSequenceCorrect = false;
Boolean isGameStarted = false;
Boolean isDoingAnimation = false;
long timeStart, timeEnd, timeDifference;
#endregion
// Constructor
public MainPage()
{
this.InitializeComponent();
randomNumber = new Random();
correctColorSequence = new int[max_seq_count];
playerColorSequence = new int[max_seq_count];
initializeColorSequence();
textBlockRoundNumber.Opacity = 0;
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void startGame()
{
currentLengthOfSequence = 0;
//textBlockCenter.Text = "Round " + roundNumber;
textBlockRoundNumber.Text = currentLengthOfSequence.ToString();
textBlockRoundNumber.Foreground = new SolidColorBrush(Colors.White);
textBlockRoundNumber.Opacity = 100;
textBlockStartGame.Opacity = 0;
isGameStarted = true;
playerColorSequenceIndex = 0;
initializeColorSequence();
isPlayerTurn = false;
continueGame();
}
private void continueGame()
{
isSequenceCorrect = true;
for (int i = 0; i < playerColorSequenceIndex; i++)
{
if (correctColorSequence[i] == playerColorSequence[i])
{
// do nothing
}
else
{
isSequenceCorrect = false;
break;
}
}
}
private void initializeColorSequence()
{
for (int i = 0; i < max_seq_count; i++)
{
correctColorSequence[i] = randomNumber.Next(1, colours_amount);
playerColorSequence[i] = 0;
}
}
#region Grip Tapping
private void Rect1_Tap(object sender, GestureEventArgs e)
{
if (isPlayerTurn)
{
//storyboardRectangle0Player.Begin();
//audioPiano12.Volume = 0.5;
//audioPiano12.Play();
if (isGameStarted)
{
playerColorSequence[playerColorSequenceIndex++] = colour1;
isPlayerTurn = false;
continueGame();
}
}
}
private void Rect2_Tap(object sender, GestureEventArgs e)
{
if (isPlayerTurn)
{
//storyboardRectangle1Player.Begin();
//audioPiano12.Volume = 0.5;
//audioPiano12.Play();
if (isGameStarted)
{
playerColorSequence[playerColorSequenceIndex++] = colour2;
isPlayerTurn = false;
continueGame();
}
}
}
private void Rect3_Tap(object sender, GestureEventArgs e)
{
if (isPlayerTurn)
{
//storyboardRectangle2Player.Begin();
//audioPiano12.Volume = 0.5;
//audioPiano12.Play();
if (isGameStarted)
{
playerColorSequence[playerColorSequenceIndex++] = colour3;
isPlayerTurn = false;
continueGame();
}
}
}
private void Rect4_Tap(object sender, GestureEventArgs e)
{
if (isPlayerTurn)
{
//storyboardRectangle3Player.Begin();
//audioPiano12.Volume = 0.5;
//audioPiano12.Play();
if (isGameStarted)
{
playerColorSequence[playerColorSequenceIndex++] = colour4;
isPlayerTurn = false;
continueGame();
}
}
}
private void StartGame_Tap(object sender, GestureEventArgs e)
{
if (textBlockStartGame.Text.Equals(startgame_click))
{
if (textBlockStartGame.Opacity != 0)
{
startGame();
}
}
else if (textBlockStartGame.Text.Equals(win))
{
endGame(startgame_click);
}
else
{
// Do nothing
}
}
#endregion
private void continueGame()
{
isSequenceCorrect = true;
for (int i = 0; i < playerColorSequenceIndex; i++)
{
if (correctColorSequence[i] == playerColorSequence[i])
{
// do nothing
}
else
{
isSequenceCorrect = false;
break;
}
}
if (isSequenceCorrect)
{
if (playerColorSequenceIndex >= currentLengthOfSequence)
{
currentLengthOfSequence++;
if (currentLengthOfSequence < max_seq_count)
{
//audioShinyDing.Volume = 0.5;
//audioShinyDing.Play();
textBlockRoundNumber.Text = currentLengthOfSequence.ToString();
}
else // roundNumber == MAX_SEQUENCE_COUNT
{
// audioApplause.Volume = 0.5;
//audioApplause.Play();
endGame(win);
}
}
else // playerColorSequenceIndex < roundNumber
{
//playerColorSequenceIndex++;
isPlayerTurn = true;
}
}
else // isSquenceCorrect == false
{
endGame(startGame);
}
}
private void endGame(String endGameMessage)
{
isGameStarted = false;
textBlockStartGame.Text = endGameMessage;
textBlockStartGame.Opacity = 100;
textBlockRoundNumber.Foreground = new SolidColorBrush(Colors.Gray);
textBlockRoundNumber.Opacity = 40;
isPlayerTurn = true;
}
#region Helper Functions
private void initializeColorSequence()
{
for (int i = 0; i < max_seq_count; i++)
{
correctColorSequence[i] = randomNumber.Next(1, colours_amount);
playerColorSequence[i] = 0;
}
}
private void waitMilliseconds(int waitTime) // Not used
{
timeStart = DateTime.Now.Ticks;
long totalWaitTimeInTicks = waitTime * 10000;
do
{
timeEnd = DateTime.Now.Ticks;
timeDifference = timeEnd - timeStart;
}
while (timeDifference < totalWaitTimeInTicks);
}
#endregion
}
}
}
You seem to have two functions called "continueGame" and also two "initializeColorSequence", hence why it says a member is already defined.
You appear to have defined both of those methods twice in the code you've provided.

Linked List: Insert item btween current and current.next

Currently I am learning how to build my own linked list in C#. I have created a function called AddTrees that adds a tree to the end of the list. User create their trees through textboxes input:tree_name, tree_height, tree_price and tree_instock. I am requesting help with my InsertTree which is not inserting a tree between current and current.next_tree? But instead of instead of inserting it between, it adds it to the top of the list.
namespace Tree_farm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class TheTrees
{
private string tree_type = " ";
private int tree_height = 0;
public double tree_price = 0;
private int tree_instock = 0;
public TheTrees next_tree;
public TheTrees(string newtree, int newheight, int newinstock, double newprice)
{
tree_type = newtree;
tree_height = newheight;
tree_price = newprice;
tree_instock = newinstock;
next_tree = null;
}
public override string ToString()
{
return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock;
}
}
public class ListForTrees
{
public TheTrees first_tree;
public TheTrees last_tree;
public int count = 0;
public ListForTrees(TheTrees new_tree)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
public ListForTrees()
{
}
public void InsertTree(TheTrees new_tree)
{
TheTrees current = first_tree;
if (count == 0)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
else if (count != 0)
{
if (new_tree.tree_price <= first_tree.tree_price)
{
new_tree.next_tree = first_tree;
first_tree = new_tree;
}
else if (new_tree.tree_price >= last_tree.tree_price)
{
last_tree.next_tree = new_tree;
last_tree = new_tree;
}
else
{
while (new_tree.tree_price > current.next_tree.tree_price)
{
current.next_tree = current;
}
new_tree.next_tree = current.next_tree;
current.next_tree = new_tree;
}
count++;
}
}
public void AddTree(TheTrees new_tree)
{
TheTrees current = first_tree;
if (count == 0)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
else if (count != 0)
{
if (new_tree.tree_price <= first_tree.tree_price)
{
new_tree.next_tree = first_tree;
first_tree = new_tree;
}
else if (new_tree.tree_price >= last_tree.tree_price)
{
last_tree.next_tree = new_tree;
last_tree = new_tree;
}
else
{
while (new_tree.tree_price > current.next_tree.tree_price)
{
current = current.next_tree;
}
new_tree.next_tree = current.next_tree;
current.next_tree = new_tree;
}
count++;
}
}
public void ClearTrees()
{
first_tree = null;
count = 0;
}
}
ListForTrees mainlist = new ListForTrees();
private void Form1_Load(object sender, EventArgs e)
{
}
private void BtnInsertTree_Click(object sender, EventArgs e)
{
//Insert Code
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
TheTrees treeinsert = new TheTrees(TxtTreeName.Text, height, stock, price);
mainlist.InsertTree(treeinsert);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
private void BtnAddTree_Click(object sender, EventArgs e)
{
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
TheTrees treeadd = new TheTrees(TxtTreeName.Text, height, stock, price);
mainlist.AddTree(treeadd);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
}
}
First, I think your expected result is wrong. It appears you want the linked list sorted in order of price (lowest to highest). If so, Cypress should actually go at the end of your list, not in the middle (80.00 > 50.00 > 2.00).
Secondly, I believe the issue rests in your while loop.
while (new_tree.tree_price > current.next_tree.tree_price)
{
current.next_tree = current;
}
I believe what you're trying to do is walk down your linked list. What you're actually doing is mucking up your linked list by changing current.next_tree to point to itself. Changing it to the following is a positive first step towards correcting your algorithm:
while (new_tree.tree_price > current.next_tree.tree_price)
{
current = current.next_tree;
}
EDIT: It should be noted that I do not get the same error that you get in your original post. When I insert items in the same order that you claim to, I get the list in correct sorted order. This is due to the fact that Cypress goes to the end, as expected. How did you get the result that you claim in the question?

C# Class Method to loop through a List created in the same Class

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
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;
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;
}
}
}
}

Categories

Resources