In this week, we did a coding kata, the subject is to select the top two teams of football group match results.

The requirements are listed below.

  • The number of team is not limited.
  • The score will be +3 when it wins, the score will be 0 if lose, otherwise the score is 1.
  • Rule 1: The advanced team are the top two highest score teams.
  • Rule 2: if the score is same, the advanced team is that who has the higher net goal.
  • Rule 3: if the score and net goal are same, the advanced team is the more goal team.
  • Rule 4: select two teams of group to advance

Below is my draft implementation without any comments and unit test after kata as it was not a success solution during the kata, just for record.

#ifndef TEAM_H_
#define TEAM_H_ #include <string> namespace HW
{
/**
* @class TeamC
* @brief
*/
class TeamC
{
public:
/**
* @brief Constructor
*/
TeamC();
TeamC(std::string iName); /**
* @brief Destructor
*/
~TeamC() = default; void AddMatch(uint8_t iScore, uint8_t iGoal, uint8_t iLose);
std::string GetName(void);
int GetScore(void);
int GetNetGoal(void); bool operator<(const TeamC& iValue); private:
std::string mName;
int mScore;
uint8_t mGoal;
uint8_t mLose;
}; } // end of namespace HW #endif /* TEAM_H_ */
#include "Team.h"

namespace HW
{ TeamC::TeamC():mName(""), mScore(), mGoal(), mLose()
{ }
TeamC::TeamC(std::string iName):mName(iName), mScore(), mGoal(), mLose()
{ } bool TeamC::operator<(const TeamC& iValue)
{
bool rt = false; //the first rule is to compare score
if (mScore != iValue.mScore)
{
rt = mScore > iValue.mScore;
}
else
{
//if the score is equal, then compare the net goal when net goals are not identical
if (mGoal - mLose != iValue.mGoal - iValue.mLose)
{
rt = mGoal - mLose > iValue.mGoal - iValue.mLose;
}
//only compare the goal
else
{
rt = mGoal > iValue.mGoal;
}
} return rt;
} int TeamC::GetScore(void)
{
return mScore;
} int TeamC::GetNetGoal(void)
{
return mGoal - mLose;
} void TeamC::AddMatch(uint8_t iScore, uint8_t iGoal, uint8_t iLose)
{
mScore += iScore;
mGoal += iGoal;
mLose += iLose;
} std::string TeamC::GetName(void)
{
return mName;
} } // end of namespace HW
#ifndef TEAMSELECTOR_H_
#define TEAMSELECTOR_H_ #include <iostream>
#include <vector>
#include <string>
#include "Team.h" namespace HW
{
/**
* @class ScoreC
* @brief
*/
class TeamSelectorC
{
public:
/**
* @brief Constructor
*/
TeamSelectorC(); /**
* @brief Destructor
*/
~TeamSelectorC() = default; void AddMatch(std::string iTeam1, std::string iTeam2, uint8_t iGoal1, uint8_t iGoal2);
std::vector<std::string> Select(void); /**
* @brief Set copy constructor as delete to prevent unintentional creation
*/
TeamSelectorC(const TeamSelectorC& iValue) = delete; /**
* @brief Set copy assignment as delete to prevent unintentional creation
*/
const TeamSelectorC& operator=(const TeamSelectorC& iValue) = delete;
private:
std::vector<TeamC> mTeams;
private:
TeamC& GetTeam(std::string iTeam);
}; } // end of namespace HW #endif /* TEAMSELECTOR_H_ */
namespace HW
{ TeamSelectorC::TeamSelectorC()
{
} void TeamSelectorC::AddMatch(std::string iTeam1, std::string iTeam2, uint8_t iGoal1, uint8_t iGoal2)
{
TeamC& team1 = GetTeam(iTeam1);
TeamC& team2 = GetTeam(iTeam2); if (iGoal1 > iGoal2)
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
else if (iGoal1 == iGoal2)
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
else
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
} std::vector<std::string> TeamSelectorC::Select(void)
{
std::sort(mTeams.begin(), mTeams.end()); //debug only
for (auto& team: mTeams)
{
std::cout << team.GetName() << "_" << team.GetScore() << "_" << team.GetNetGoal() << std::endl;
} std::vector<std::string> results;
if (mTeams.size() > )
{
results.push_back(mTeams[].GetName());
} if (mTeams.size() > )
{
results.push_back(mTeams[].GetName());
}
return results;
} TeamC& TeamSelectorC::GetTeam(std::string iTeam)
{
for (auto& team: mTeams)
{
if (team.GetName() == iTeam)
{
return team;
}
} TeamC tmp(iTeam);
mTeams.push_back(tmp);
return mTeams[mTeams.size() - ];
} } // end of namespace HW

The test code

void TeamSelectTest(void)
{
HW::TeamSelectorC selector;
selector.AddMatch("A", "B", 2, 1);
selector.AddMatch("A", "C", 2, 1);
selector.AddMatch("A", "D", 2, 1);
selector.AddMatch("B", "C", 3, 1);
selector.AddMatch("B", "D", 2, 1);
selector.AddMatch("C", "D", 3, 1); std::vector<std::string> results = selector.Select();
for (auto result:results)
{
std::cout << result << std::endl;
}
}

最新文章

  1. arcgis将图片转成shp地图
  2. Zabbix日志监视的汇总报警(更新发送邮件脚本)
  3. C++静态代码分析PreFast
  4. STL之list容器用法
  5. Azure 云助手正式发布
  6. Redis链接上不的问题
  7. __attribute__((unused))
  8. ajax+jsp实现三级联动下拉框
  9. JAVA学习课第五十三届 — IO流程(七)File打靶 &amp;amp; Properties设置
  10. 第1章 PHP快速入门
  11. esp8266 SDK开发之GPIO中断
  12. SpringMVC架构的项目,js,css等静态文件导入有问题
  13. Ruby开发入门
  14. (转载) python3: beautifulsoup的使用
  15. [Leet code 2]Two Sum
  16. @ContextConfiguration的意思
  17. cookie -- 添加删除
  18. 微信小程序开发,服务器端获取不到请求参数
  19. 20145326蔡馨熠 实验三 &quot;敏捷开发与XP实践&quot;
  20. ArcGIS Server 基于Token安全验证

热门文章

  1. loadrunner json中文无法识别问题
  2. Java语法基础学习DayThree
  3. 数据分析常用的python工具和SQL语句
  4. linux内核工作队列使用总结
  5. WIFI探针 搞定
  6. springboot (spring mvc)集成swagger
  7. 栈溢出原理与 shellcode 开发
  8. Linux 下各个目录的作用及内容
  9. SQL注入之Sqli-labs系列第十一关(基于单引号的万能密码注入)
  10. [转]一文读懂《梁宁&#183;产品思维30讲》最精华内容(含全套PPT)