场合
我的系统上有Races,它们有两种类型(继承自BaseRace的类),每种类型都有无数个实例:
我的系统中的成员有两种类型(继承自BaseMember的类),每种类型都有无数个实例::
我希望会员加入比赛,如果他们符合比赛要求(他们的类型和性别),
和能够离开他们加入的任何种族
我已经掌握了这方法,但它们非常基本。
基本上,当我的用户当前加入比赛时,参赛者的数量会增加1,虽然这对我的项目来说是“所有需要的”,但我希望更进一步。
问题
我希望得到一个连接每个种族的每个赛车(存储为对象)的列表。
例如,如果一名赛车参加了名为“测试赛”的比赛:
首先,它们会被添加到此Races成员列表中。
他们不会再次加入它(因为它们已经存在于列表中)
他们会能够离开(因为它们存在于列表中)
当他们离开时,他们的对象被移除,这意味着他们可以随时重新加入。
我的问题是,我不知道如何为每个拥有这些成员的竞赛实例制作单独的列表。
这是我目前的代码,这里有三个类,我的BaseRace以及我的Sprint和Race类。
抽象BaseRace,Sprint和Race代码
namespace HillRacingGraded
{
/// <summary>
/// BaseRace is the basic race in the system, other races of different types will extend BaseRace
/// </summary>
public abstract class BaseRace
{
//**RACE DETAILS DECLARING**
/// <summary>
/// the name of the race
/// </summary>
protected string nameRace;
/// <summary>
/// used to uniquely identify a race
/// </summary>
protected string RaceID;
/// <summary>
/// used to hold the starting location of a race
/// </summary>
protected string StartLoc;
/// <summary>
/// – used to hold the starting time of a race
/// </summary>
protected string StartTime;
/// <summary>
/// – used to hold the distance of the race
/// </summary>
protected string DistRace;
/// <summary>
/// – used to hold the climb of the race
/// </summary>
protected string ClimbRace;
/// <summary>
/// – used to hold a short description of the race
/// </summary>
protected string DescRace;
/// <summary>
/// – used to hold a snapshot image of the race, or a map
/// </summary>
protected string imgRace;
/// <summary>
/// – used to hold the terrain of the race
/// </summary>
protected string terRace;
/// <summary>
/// – used to hold the weather of the race
/// </summary>
protected string weaRace;
/// <summary>
/// – used to hold the information on how to travel to the race.
/// </summary>
protected string TravRace;
/// <summary>
/// – used to hold the previous races winners (if any)
/// </summary>
protected string oldWinner;
/// <summary>
/// – the record time of the race in minutes.
/// </summary>
protected int recordRace;
/// <summary>
/// – the average number of racers for this race currently.
/// </summary>
protected string runnerRace;
/// <summary>
/// – used to hold the date of the race
/// </summary>
protected string dateRace;
/// <summary>
/// – used to hold the last signup date of race
/// </summary>
protected string dateRaceClose;
/// <summary>
/// – member type permitted to attend race (junior, senior)
/// </summary>
protected string permRace;
/// <summary>
/// gender type permitted to attend race (male, female)
/// </summary>
protected string genRace;
/// <summary>
/// – difficulty of the race.
/// </summary>
protected string diffRace;
/// <summary>
/// The limit of racers allowed to join the race.
/// </summary>
protected int limitRace;
/// <summary>
/// The creator of the race
/// </summary>
protected string creatorRace;
/// <summary>
///
/// </summary>
protected int currentRunners;
/// <summary>
///
/// </summary>
public string raceID { get { return RaceID; } set { RaceID = value; } }
/// <summary>
///
/// </summary>
public string namerace { get { return nameRace; } set { nameRace = value; } }
/// <summary>
///
/// </summary>
public string descrace { get { return DescRace; } set { DescRace = value; } }
/// <summary>
///
/// </summary>
public string permrace { get { return permRace; } set { permRace = value; } }
/// <summary>
///
/// </summary>
public string genrace { get { return genRace; } set { genRace = value; } }
/// <summary>
///
/// </summary>
public int limitrace { get { return limitRace; } set { limitRace = value; } }
/// <summary>
///
/// </summary>
public string wearace { get { return weaRace; } set { weaRace = value; } }
/// <summary>
///
/// </summary>
public string diffrace { get { return diffRace; } set { diffRace = value; } }
/// <summary>
///
/// </summary>
public string distrace { get { return DistRace; } set { DistRace = value; } }
/// <summary>
///
/// </summary>
public string creatorrace { get { return creatorRace; } set { creatorRace = value; } }
protected JuniorMember junior;
protected SeniorMember senior;
public DateTime returnDate;
/// <summary>
/// Base constructor for BaseRace, sets information to variables.
/// </summary>
/// <param name="rname">Name of the Race (Hamilton Hill Run, Glasgow Night Sprint, Motherwell Marathon, Dundee Dash etc.")</param>
/// <param name="rid">ID of the Race (RID123, RID250, RID102 etc.)</param>
/// <param name="rloc">Location of the Race (Hamilton, Inverness, Jupiter etc.)</param>
/// <param name="rtime">Time of the race (12:30pm, 1:00pm, 6:00am, 4:28am etc.)</param>
/// <param name="rdist">Distance of the race (500 yards, 5 miles, 100 metres etc) </param>
/// <param name="rclimb">Climb of the race (5 metres, 18 metres, 100 metres)</param>
/// <param name="rdesc">Description of the Race (Quick dash through the park, long run through the valleys)</param>
/// <param name="rimg">Image of the race **LINK** (http://imgur.com/0VPYzRE)</param>
/// <param name="rter">Terrain of the race (Dry dirt and Hilly, Muddy and Flat, grassy and hilly) </param>
/// <param name="rcon">Weather Condtions of the race (Rain, Hail, Sleet, Snow, Sunny)</param>
/// <param name="rdir">Directions to the Race (Front Entrance of Straven Green, Outside the County Building in Hamilton)</param>
/// <param name="rwin">Old Winners of Race (Jonathan Mccrum Won last year with a time of 2:39)</param>
/// <param name="rrecord">Record time of Race (David Mccrum won three years ago with a time of 1:22)</param>
/// <param name="rrace">Average number of races per annum (532, 912, 341)</param>
/// <param name="rdate">Date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rdatec">Closing date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rcat">Category of permitted racers (Junior, Senior)</param>
/// <param name="rgen">Gender of permitted racers (Male, Female)</param>
/// <param name="rdif">Difficulty of Race (Easy Peasy, Pretty tough, Hard, Mental)</param>
/// <param name="rlim">Limit of members allowed to enter the race</param>
/// <param name="rcre">Limit of members allowed to enter the race</param>
public BaseRace(string rname, string rid, string rloc, string rtime, string rdist, string rclimb, string rdesc, string rimg, string rter,
string rcon, string rdir, string rwin, int rrecord, string rrace, string rdate,string rdatec,string rcat,string rgen, string rdif, int rlim, string rcre)
{
//set all local parameters to the incoming paramater.
nameRace = rname;
RaceID = rid;
StartLoc = rloc;
StartTime = rtime;
DistRace = rdist;
ClimbRace = rclimb;
DescRace = rdesc;
imgRace = rimg;
terRace = rter;
weaRace = rcon;
TravRace = rdir;
oldWinner = rwin;
recordRace = rrecord;
runnerRace = rrace;
dateRace = rdate;
dateRaceClose = rdatec;
permRace = rcat;
genRace = rgen;
diffRace = rdif;
limitRace = rlim;
creatorRace = rcre;
}
public override string ToString()
{
return base.ToString();
}
//delcaring abstract joinRace method, to be overriden in "Junior" and "Senior" race types.
public abstract void joinSenior(SeniorMember sm);
public abstract void joinJunior(JuniorMember jm);
/// <summary>
/// LeaveRace checks firstly if the if the race contains the member that wants to leave the race (Checks join before allowing leave)
/// if the race cointains that Username on record, then it will allow the leave, else it will tell the user they aren't applied.
/// </summary>
public void leaveRace(string Username)
{
if (senior.FirstName == null || junior == null )
{
//race doesn't contain this username, so don't allow the leave.
throw new Exception(nameRace + " is currently not on loan, It cannot be returned");
}
else
{
//race contains this username, so allow the leave.
senior = null;
junior = null;
}
}
}
/// <summary>
/// Sprint extends BaseRace, Sprint is a type of Race.
/// </summary>
///
public class Sprint : BaseRace
{
string author;
List<SeniorMember> joinedMembers = new List<SeniorMember>();
/// <summary>
/// joinRace allows the Member instance to join a race as long as the member hasn't already joined the race
/// and that they meet the conditions of the race they're joining.
/// </summary>
/// <param name="sm">The Member that will join the Race</param>
public override void joinSenior(SeniorMember sm)
{
//if the members type doesnt equal the races type permission then they shouldn't be able to join.
if (sm.memType != permRace)
{
//throws an exception if the member doesn't have the correct type junior
throw new Exception(sm.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
}
//if the members gender isn't equal to the races gender requirement they shouldn't be able to join that race.
else if (sm.gender != genRace)
{
//throws an exception if the member doesn't have the correct gender type.
throw new Exception(sm.FirstName + " does not meet the requirements to join this race, must be a: " + genRace);
}
//if the member trying to join already exists on the race knock them back.
else if(joinedMembers.Contains(sm))
{
throw new Exception("This member has already joined this race");
}
else
{
//if all other conditions are met, then add this racer to the list of racers.
senior = sm;
joinedMembers = joinedMembers + sm;
currentRunners++;
if (currentRunners > limitRace)
{
int errorValue;
currentRunners = limitRace;
errorValue = limitRace + 1;
throw new Exception(sm.FirstName + " would be this races: " + errorValue + "th runner. This race can only take: " + limitRace);
}
returnDate = DateTime.Today.AddDays(21);
}
}
public override void joinJunior(JuniorMember jm)
{
if (jm != null)
{
//Increment the current members on the race by 1.
}
//if the members type doesnt equal the member join permission then they shouldn't be able to join.
if (jm.memType != permRace)
{
//throws an exception if the member doesn't have the correct type junior
throw new Exception(jm.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
}
//if the members gender isn't equal to the races gender requirement they shouldn't be able to join that race.
if (jm.gender != genRace)
{
//throws an exception if the member doesn't have the correct gender type.
throw new Exception(jm.FirstName + " does not meet the requirements to join this race, must be a: " + genRace);
}
else
{
//if all other conditions are met, and the member meets requirements, let the member join the race and add a return date of 21 days.
junior = jm;
currentRunners++;
if (currentRunners > limitRace)
{
int errorValue;
currentRunners = limitRace;
errorValue = limitRace + 1;
throw new Exception(jm.FirstName + " would be this races: " + errorValue + "th runner. This race can only take: " + limitRace);
}
returnDate = DateTime.Today.AddDays(3);
}
}
/// <summary>
/// JuniorRace extends the base race parameters.
/// </summary>
/// <param name="rname">Name of the Race (Hamilton Hill Run, Glasgow Night Sprint, Motherwell Marathon, Dundee Dash etc.")</param>
/// <param name="rid">ID of the Race (RID123, RID250, RID102 etc.)</param>
/// <param name="rloc">Location of the Race (Hamilton, Inverness, Jupiter etc.)</param>
/// <param name="rtime">Time of the race (12:30pm, 1:00pm, 6:00am, 4:28am etc.)</param>
/// <param name="rdist">Distance of the race (500 yards, 5 miles, 100 metres etc) </param>
/// <param name="rclimb">Climb of the race (5 metres, 18 metres, 100 metres)</param>
/// <param name="rdesc">Description of the Race (Quick dash through the park, long run through the valleys)</param>
/// <param name="rimg">Image of the race **LINK** (http://imgur.com/0VPYzRE)</param>
/// <param name="rter">Terrain of the race (Dry dirt and Hilly, Muddy and Flat, grassy and hilly) </param>
/// <param name="rcon">Weather Condtions of the race (Rain, Hail, Sleet, Snow, Sunny)</param>
/// <param name="rdir">Directions to the Race (Front Entrance of Straven Green, Outside the County Building in Hamilton)</param>
/// <param name="rwin">Old Winners of Race (Jonathan Mccrum Won last year with a time of 2:39)</param>
/// <param name="rrecord">Record time of Race (David Mccrum won three years ago with a time of 1:22)</param>
/// <param name="rrace">Average number of races per annum (532, 912, 341)</param>
/// <param name="rdate">Date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rdatec">Closing date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rcat">Category of permitted racers (Junior, Senior)</param>
/// <param name="rgen">Gender of permitted racers (Male, Female)</param>
/// <param name="rdif">Difficulty of Race (Easy Peasy, Pretty tough, Hard, Mental)</param>
/// <param name="rlim">Limit of members allowed to enter the race</param>
public Sprint(string rname, string rid, string rloc, string rtime, string rdist, string rclimb, string rdesc, string rimg, string rter,
string rcon, string rdir, string rwin, int rrecord, string rrace, string rdate,string rdatec,string rcat,string rgen, string rdif, int rlim, string rcre)
: base(rname,rid,rloc,rtime,rdist,rclimb,rdesc,rimg,rter,rcon,rdir,rwin,rrecord,rrace,rdate,rdatec,rcat,rgen,rdif,rlim,rcre)
{
nameRace = rname;
RaceID = rid;
}
/// <summary>
/// returns a string tellig the user the book, it's title, and whether or not it is on loan.
/// </summary>
/// <returns>toString strout</returns>
public override string ToString()
{
//string to be returned in to string
string strout;
strout = "";
//if the memebr is not null then the item is currently out on loan, tell the user this.
if (junior != null)
{
strout = "Sprint: '" + RaceID + "'\nName: '" + nameRace + "' by '" + creatorRace + "'\nLocation: '" + StartLoc + "'\nDistance: '" + DistRace + "'\nTerrain: '" + terRace + "'\nCurrent Members: '" + currentRunners + "'\n";
}
//item is not on loan currently, use the default without the loan detail.
else
{
strout = "Sprint: '" + RaceID + "'\nName: '" + nameRace + "' by '" + creatorRace + "'\nPermitted Members: '" + permRace + "'\nPermitted Genders: '" + genRace + "'\nLocation: '" + StartLoc + "'\nDistance: '" + DistRace + "'\nTerrain: '" + terRace + "'\nWeather: '" + weaRace + "\nClimb: '" + ClimbRace + "\nRace Difficulty: '" + diffRace + "'\nCurrent Members: '" + currentRunners + "'\nRacer Limit: '" + limitRace + "'\n";
}
return strout;
}
}
/// <summary>
/// Journal class extends Stock, Journal is a type of Stock.
/// </summary>
public class Race : BaseRace
{
//declaring int volume so It can a volume
int volume;
public override void joinSenior(SeniorMember sm)
{
//if member is not null then that means there's already a member in the race.
if (sm != null)
{
//Increment the current members on the race by 1.
}
//if the members type doesnt equal the member join permission then they shouldn't be able to join.
if (sm.memType != permRace)
{
//throws an exception if the member doesn't have the correct type junior
throw new Exception(sm.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
}
//if the members gender isn't equal to the races gender requirement they shouldn't be able to join that race.
if (sm.gender != genRace)
{
//throws an exception if the member doesn't have the correct gender type.
throw new Exception(sm.FirstName + " does not meet the requirements to join this race, must be a: " + genRace);
}
else
{
//if all other conditions are met, and the member meets requirements, let the member join the race and add a return date of 21 days.
senior = sm;
currentRunners++;
if (currentRunners > limitRace)
{
int errorValue;
currentRunners = limitRace;
errorValue = limitRace + 1;
throw new Exception(sm.FirstName + " would be this races: " + errorValue + "th runner. This race can only take: " + limitRace);
}
returnDate = DateTime.Today.AddDays(21);
}
}
public override void joinJunior(JuniorMember jm)
{
if (jm != null)
{
//Increment the current members on the race by 1.
}
//if the members type doesnt equal the member join permission then they shouldn't be able to join.
if (jm.memType != permRace)
{
//throws an exception if the member doesn't have the correct type junior
throw new Exception(jm.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
}
//if the members gender isn't equal to the races gender requirement they shouldn't be able to join that race.
if (jm.gender != genRace)
{
//throws an exception if the member doesn't have the correct gender type.
throw new Exception(jm.FirstName + " does not meet the requirements to join this race, must be a: " + genRace);
}
else
{
//if all other conditions are met, and the member meets requirements, let the member join the race and add a return date of 21 days.
junior = jm;
currentRunners++;
if (currentRunners > limitRace)
{
int errorValue;
currentRunners = limitRace;
errorValue = limitRace + 1;
throw new Exception(jm.FirstName + " would be this races: " + errorValue + "th runner. This race can only take: " + limitRace);
}
returnDate = DateTime.Today.AddDays(3);
}
}
/// <summary>
/// Book extends the base stock item parameters.
/// <summary>
/// <param name="rname">Name of the Race (Hamilton Hill Run, Glasgow Night Sprint, Motherwell Marathon, Dundee Dash etc.")</param>
/// <param name="rid">ID of the Race (RID123, RID250, RID102 etc.)</param>
/// <param name="rloc">Location of the Race (Hamilton, Inverness, Jupiter etc.)</param>
/// <param name="rtime">Time of the race (12:30pm, 1:00pm, 6:00am, 4:28am etc.)</param>
/// <param name="rdist">Distance of the race (500 yards, 5 miles, 100 metres etc) </param>
/// <param name="rclimb">Climb of the race (5 metres, 18 metres, 100 metres)</param>
/// <param name="rdesc">Description of the Race (Quick dash through the park, long run through the valleys)</param>
/// <param name="rimg">Image of the race **LINK** (http://imgur.com/0VPYzRE)</param>
/// <param name="rter">Terrain of the race (Dry dirt and Hilly, Muddy and Flat, grassy and hilly) </param>
/// <param name="rcon">Weather Condtions of the race (Rain, Hail, Sleet, Snow, Sunny)</param>
/// <param name="rdir">Directions to the Race (Front Entrance of Straven Green, Outside the County Building in Hamilton)</param>
/// <param name="rwin">Old Winners of Race (Jonathan Mccrum Won last year with a time of 2:39)</param>
/// <param name="rrecord">Record time of Race (David Mccrum won three years ago with a time of 1:22)</param>
/// <param name="rrace">Average number of races per annum (532, 912, 341)</param>
/// <param name="rdate">Date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rdatec">Closing date of the Race (2/3/16, 16/3/95, 18/4/16)</param>
/// <param name="rcat">Category of permitted racers (Junior, Senior)</param>
/// <param name="rgen">Gender of permitted racers (Male, Female)</param>
/// <param name="rdif">Difficulty of Race (Easy Peasy, Pretty tough, Hard, Mental)</param>
/// <param name="rlim">Limit of members allowed to enter the race</param>
/// <param name="rcre">The creator of the Race</param>
/// </summary>
public Race(string rname, string rid, string rloc, string rtime, string rdist, string rclimb, string rdesc, string rimg, string rter,
string rcon, string rdir, string rwin, int rrecord, string rrace, string rdate, string rdatec, string rcat, string rgen, string rdif, int rlim, string rcre)
: base(rname, rid, rloc, rtime, rdist, rclimb, rdesc, rimg, rter, rcon, rdir, rwin, rrecord, rrace, rdate, rdatec, rcat, rgen, rdif, rlim, rcre)
{
//set volume to vol.
//volume = vol;
}
/// <summary>
/// returns a string telling the user the Journal, it's title, and whether or not it is on loan.
/// </summary>
/// <returns>ToString Strout</returns>
public override string ToString()
{
//string to be returned in to string
string strout;
strout = "";
//if the race is not null (The race contains at least one racer)
if (junior != null)
{
strout = "Race: '" + RaceID + "'\nName: '" + nameRace + "' by '" + creatorRace + "'\nLocation: '" + StartLoc + "'\nPermitted Members: '" + permRace + "'\nCurrent Members: '" + currentRunners + "'\n";
}
//The Race doesn't have any racers.
else
{
strout = "Race: '" + RaceID + "'\nName: '" + nameRace + "' by '" + creatorRace + "'\nLocation: '" + StartLoc + "'\nPermitted Members: '" + permRace + "'\n"; ;
}
return strout;
}
}
}
非常感谢任何帮助。
答案 0 :(得分:2)
您的代码看起来非常复杂,无法实现您的目标。
但回到你的问题,创建一个名为Member
的新班级,由Junior和Senior继承。然后,您可以删除JoinSenior
和JoinJunior
方法,并使用通用JoinMember
方法(使用Member
作为参数类型)
public void JoinMember(Member member)
{
}
然后,您可以跟踪多个列表中的所有参赛者。
List<Member> Sprinters = new List<Members>();
List<Member> BaseRacers = new List<Members>();
然后,您可以将JuniorMember
或SeniorMember
个对象添加到同一列表中。
您可以使用LINQ
var racerExists = Sprinters.FirstOrDefault(r => r.RacerId == juniorMember.RacerId);
if (racerExists == null)
{
// Add the new racer
}
您可以将其分离出一种新方法,使其更清洁。
public bool MemberExists(Member member)
{
var memberExists = Sprinters.FirstOrDefault(m => m.RacerId == member.RacerId);
return memberExists != null;
}