Answer the question
In order to leave comments, you need to log in
How to find a class object in a sheet by one or more of its fields?
I have this code:
class Garage
{
private List<Car> cars;
public void AddCar(string carModel, string color, double speed, int yearOfIssue)
{
Car car = new Car(carModel, color, speed, yearOfIssue);
cars.Add(car);
}
public void DeleteCar(string carModel, string color, double speed, int yearOfIssue)
{
}
}
class Car
{
public Car()
{
}
public Car(string carModel, string color, double speed, int yearOfIssue)
{
this.carModel = carModel;
this.color = color;
this.speed = speed;
this.yearOfIssue = yearOfIssue;
}
private string carModel;
private string color;
private double speed;
private int yearOfIssue;
}
Answer the question
In order to leave comments, you need to log in
Somewhere like this.
public class Garage
{
public Garage()
{
_cars = new List<Car>();
}
private readonly List<Car> _cars;
public void AddCar(string carModel, string color, double speed, int yearOfIssue)
{
var car = new Car(carModel, color, speed, yearOfIssue);
_cars.Add(car);
}
public void DeleteCar(string carModel, string color = "", double speed = 0, int yearOfIssue = 0)
{
var query = from c in _cars
where c.CarModel.StartsWith(carModel)
select c;
if (color != string.Empty)
query = query.Where(cl => cl.Color == color);
if (speed > 0)
query = query.Where(sp => Math.Abs(sp.Speed - speed) < 0.2);
if (yearOfIssue > 0)
query = query.Where(y => y.YearOfIssue == yearOfIssue);
var results = query.Select(f => f).ToList();
foreach (var car in results) _cars.Remove(car);
}
}
public class Car
{
public Car()
{
}
public Car(string carModel, string color, double speed, int yearOfIssue)
{
this._carModel = carModel;
this._color = color;
this._speed = speed;
this._yearOfIssue = yearOfIssue;
}
private string _carModel;
public string CarModel => _carModel;
private string _color;
public string Color => _color;
private double _speed;
public double Speed => _speed;
private int _yearOfIssue;
public int YearOfIssue => _yearOfIssue;
}
You gave him a hard time, of course. list.Remove(FirstOrDefault(lambdaExpression)) (I'm writing from my phone, I might be wrong)
where the lambda should be compared. It's better to pass a delegate to a function.
Using LINQ:
CarCollection.Where(x => x.color == "red").First();
Maybe it would be better that you create a universal identifier for each machine? It will be much easier to work with them and the execution result will be as accurate as possible (because it is possible that exactly the same car in the garage will be more than 2, and when you call the function, your system may delete the wrong car at all).
And this is the answer to your question
public void DeleteCar(string carModel, string color, double speed, int yearOfIssue)
{
Car car = cars.FirstOrDefault(c => c.CarModel == carModel && c.Color == color && c.Speed == speed && c.YearOfIssue == yearOfIssue);
cars.Remove(car);
}
1) You do an iteration of the sheet
2) On each index of the car, you make requests for attributes through getcarModel () from the car class. Geters and seters, of course, must be added to the Car class
As a result, inside the for(foreach) loop, by type:
cars[indexNr].getcarModel()
3) You check for compliance with the saved parameters that the user entered. Since you have 0..n, then I would do something like
"if the string is not empty", then compare cars[indexNr].getcarModel () from point 2 with the string entered by the user.
4) If at least 1 of the parameters inside for loop'e does not match, then continue.
5) If all parameters were found in any of the objects, then
cars.RemoveAt(Actual index from the for loop);
But you can do everything much easier and more efficiently. For example, in each machine, make a unique index that would be displayed to the user. For example, the car it is looking for = id 12. And id 12 is nothing more than a position in the list. Then one line
cars.RemoveAt(Index); in the DeleteCar function
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question