Created basic RobotServicew
This commit is contained in:
1 parent
1ccf2b7b1e
commit
9ad1d509df
5 files changed
+169
-1
No files matched your search
@@ -0,0 +1,65 @@
|
||||
namespace RobotsWantedLeague.Test.Services;
|
||||
using RobotsWantedLeague.Models;
|
||||
using RobotsWantedLeague.Services;
|
||||
|
||||
[TestClass]
|
||||
public class RobotsServiceTest
|
||||
{
|
||||
|
||||
[TestMethod]
|
||||
public void TestCreateRobot()
|
||||
{
|
||||
RobotsService service = new RobotsService();
|
||||
Robot robot = service.CreateRobot("paul", 2, 3, "canada");
|
||||
Assert.AreEqual(robot.Name, "paul");
|
||||
Assert.AreEqual(robot.Weight, 2);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetRobotById()
|
||||
{
|
||||
RobotsService service = new RobotsService();
|
||||
Assert.IsNull(service.GetRobotById(98));
|
||||
Robot robot = service.CreateRobot("paul", 2, 3, "canada");
|
||||
Robot? ret = service.GetRobotById(robot.Id);
|
||||
Assert.IsNotNull(ret);
|
||||
Assert.AreEqual(robot.Name, ret.Name);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetRobotById_MultipleRobots()
|
||||
{
|
||||
RobotsService service = new RobotsService();
|
||||
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
|
||||
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
|
||||
Robot? ret = service.GetRobotById(robot2.Id);
|
||||
Assert.IsNotNull(ret);
|
||||
Assert.AreEqual(robot2.Name, ret.Name);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestListRobots()
|
||||
{
|
||||
RobotsService service = new RobotsService();
|
||||
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
|
||||
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
|
||||
Assert.AreEqual(2, service.Robots.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestDeleteRobotById()
|
||||
{
|
||||
RobotsService service = new RobotsService();
|
||||
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
|
||||
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
|
||||
Robot robot3 = service.CreateRobot("xu", 1, 9, "chine");
|
||||
|
||||
Assert.IsTrue(service.DeleteRobotById(robot2.Id));
|
||||
|
||||
Assert.AreEqual(2, service.Robots.Count);
|
||||
Assert.IsNull(service.GetRobotById(robot2.Id));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace RobotsWantedLeague.Models;
|
||||
|
||||
public class Robot{
|
||||
public int Id { get; set;}
|
||||
public string Name { get; set;}
|
||||
|
||||
/**
|
||||
* Weight in kg.
|
||||
*/
|
||||
public int Weight { get; set;}
|
||||
|
||||
/**
|
||||
* Height in m.
|
||||
*/
|
||||
public int Height { get; set;}
|
||||
|
||||
public string Country { get; set;}
|
||||
|
||||
public Robot(int Id, string Name, int Weight, int Height, string Country){
|
||||
this.Id = Id;
|
||||
this.Name = Name;
|
||||
this.Weight = Weight;
|
||||
this.Height = Height;
|
||||
this.Country = Country;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using RobotsWantedLeague.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllersWithViews();
|
||||
|
||||
builder.Services.AddSingleton<IRobotsService, RobotsService>();
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace RobotsWantedLeague.Services;
|
||||
using RobotsWantedLeague.Models;
|
||||
|
||||
public interface IRobotsService{
|
||||
public List<Robot> Robots { get; }
|
||||
|
||||
public Robot CreateRobot(string name,
|
||||
int weight,
|
||||
int height,
|
||||
string country);
|
||||
|
||||
public Robot? GetRobotById(int id);
|
||||
|
||||
public bool DeleteRobotById(int id);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
namespace RobotsWantedLeague.Services;
|
||||
|
||||
using RobotsWantedLeague.Models;
|
||||
|
||||
public class RobotsService: IRobotsService
|
||||
{
|
||||
private readonly List<Robot> robots;
|
||||
private int idGenerator = 0;
|
||||
public List<Robot> Robots { get => robots; }
|
||||
|
||||
public RobotsService()
|
||||
{
|
||||
robots = new List<Robot>();
|
||||
}
|
||||
|
||||
private int generateId(){
|
||||
idGenerator = idGenerator + 1;
|
||||
return idGenerator;
|
||||
}
|
||||
|
||||
public Robot CreateRobot(string name,
|
||||
int weight,
|
||||
int height,
|
||||
string country)
|
||||
{
|
||||
var robot = new Robot(generateId(), name, weight, height, country);
|
||||
robots.Add(robot);
|
||||
return robot;
|
||||
}
|
||||
|
||||
private int getIndexOfRobotById(int id){
|
||||
int idx = 0;
|
||||
foreach (Robot robot in robots){
|
||||
if (robot.Id == id){
|
||||
return idx;
|
||||
}
|
||||
idx ++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Robot? GetRobotById(int id){
|
||||
int indexToDelete = getIndexOfRobotById(id);
|
||||
if(indexToDelete == -1){
|
||||
return null;
|
||||
}
|
||||
return robots[indexToDelete];
|
||||
}
|
||||
|
||||
public bool DeleteRobotById(int id){
|
||||
int indexToDelete = getIndexOfRobotById(id);
|
||||
if(indexToDelete == -1){
|
||||
return false;
|
||||
}
|
||||
robots.RemoveAt(indexToDelete);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user