So iam trying to make a Dhond’t Polling system . This explains what i am trying to make : https://www.bbc.co.uk/news/uk-politics-27187434 (pretty quick read) However I cant find a way in c# to be able to keep track of the original object property value while also being able to change the value after doing calculations on it.I am happy for anyone to tell me a different way of approaching this if I’ve done it wrong, I am very new to c#.
MAIN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Voting_System
{
class Program
{
static void Main(string() args)
{
// Establish the file path
string filepath = @"C:UsersmatheOneDriveDocumentsAll AssignmentsVoting Systeminputdata.txt";
// Store values in a list of string
List<string> file = File.ReadAllLines(filepath).ToList();
// Puts each party into a list of Party and display Name + Votes
List<Party> partys = new List<Party>();
foreach (string line in file)
{
string() items = line.Split(',');
Party p = new Party(items(0), Convert.ToInt32(items(1)));
partys.Add(p);
}
// Ask user for thresh hold and also calculate total votes
Console.WriteLine("What is the threshold for partys (%) ?");
double threshold = Convert.ToDouble(Console.ReadLine());
// Find total votes
int totalVotes = 0;
foreach (Party p in partys)
{
totalVotes += p.Votes;
}
Console.WriteLine($"nTotal No of votes combined for all partys 2020 : {totalVotes}");
// Displays percent of votes for each party
foreach (Party p in partys)
{
if (p.PercentOfVotes(totalVotes) > threshold)
{
Console.WriteLine(p);
Console.WriteLine(p.PercentOfVotes(totalVotes));
}
}
// Finds party with highest votes
Party biggestVotes = partys.Aggregate((v1,v2) => v1.Votes > v2.Votes ? v1 : v2);
Console.WriteLine(biggestVotes);
biggestVotes.Seats = 1;
// Keeps console open
Console.ReadLine();
}
}
}
PARTY CLASS
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Voting_System
{
class Party
{
// properties for each party
public string Name { get; set; }
public int Votes { get; set; }
public int Seats { get; set; }
// Constructor for party class
public Party (string name,int votes,int seats=0)
{
Name = name;
Votes = votes;
Seats = seats;
}
// Returns percentage of votes for your party
public double PercentOfVotes(double totalVotes) => (Votes / totalVotes) * 100;
// When ever you print the object of this class return this
public override string ToString()
{
return $"Name-{Name} Votes-{Votes} Seats-{Seats}";
}
}
}
VALUES IN THE DATA FILE
Brexit Party,452321,BP1,BP2,BP3,BP4,BP5;
Liberal Democrats,203989,LD1,LD2,LD3,LD4,LD5;
Labour,164682,LAB1,LAB2,LAB3,LAB4,LAB5;
Conservative,126138,CON1,CON2,CON3,CON4,CON5;
Green,124630,GR1,GR2,GR3,GR4,GR5;
UKIP,58198,UKP1,UKP2,UKP3,UKP4,UKP5;
Change UK,41117,CUK1,CUK2,CUK3,CUK4,CUK5;
Independent Network,7641,INET1,INET2,INET3,INET4,INET5;
Independent,4511,IND1;