Skip to content

Instantly share code, notes, and snippets.

@mindshifter
Created October 28, 2022 14:29
Show Gist options
  • Save mindshifter/09baa45802846cea431a66a49ac6b975 to your computer and use it in GitHub Desktop.
Save mindshifter/09baa45802846cea431a66a49ac6b975 to your computer and use it in GitHub Desktop.
void main() {
Car car1 = Car("1", "Mazda", "CX-5");
Car car2 = Car("2", "Mazda", "CX-3");
Car car3 = Car("3", "Toyota", "Prius");
Car car4 = Car("4", "Toyota", "Rav4");
Car car5 = Car("5", "Ford", "Kuga");
Car car6 = Car("6", "Ford", "Puma");
Car car7 = Car("7", "Honda", "Civic");
Car car8 = Car("8", "Honda", "Accord");
Car car9 = Car("9", "BMW", "x1");
Car car10 = Car("10", "BMW", "x3");
CarDeliveryImpl cardelivery = CarDeliveryImpl();
cardelivery.addNewcar(car1);
cardelivery.addNewcar(car1);
cardelivery.addNewcar(car1);
cardelivery.addNewcar(car1);
cardelivery.addNewcar(car2);
cardelivery.addNewcar(car3);
cardelivery.addNewcar(car4);
cardelivery.addNewcar(car5);
cardelivery.addNewcar(car6);
cardelivery.addNewcar(car7);
cardelivery.addNewcar(car8);
cardelivery.addNewcar(car10);
print('Car id 100 deleted ${cardelivery.deleteCar("100").toString()}');
print('Car id 7 deleted ${cardelivery.deleteCar("100").toString()}');
print('Cars with model CX-5 ${cardelivery.listCarsByModel("CX-5")}');
print('Cars with name BMW ${cardelivery.listCarsByName("BMW")}');
}
class Car {
String id;
String name;
String model;
Car(this.id, this.name, this.model);
}
abstract class CarDelivery {
void addNewcar(Car car);
int deleteCar(String id);
List<String> listCarsByName(String searchQuery);
List<String> listCarsByModel(String searchQuery);
}
class CarDeliveryImpl extends CarDelivery {
List cars = [];
@override
void addNewcar(Car car) {
cars.add(car);
}
@override
int deleteCar(String id) {
var found = cars.firstWhere((car) => car.id == id, orElse: () => null);
if (found != null) {
bool removed = cars.remove(found);
return int.parse(found.id);
}
return -1;
}
@override
List<String> listCarsByModel(String searchQuery) {
final filteredThings =
cars.where((car) => car.model.contains(searchQuery)).toList();
return filteredThings.map((car) => car.model.toString()).toList();
}
@override
List<String> listCarsByName(String searchQuery) {
final filteredThings =
cars.where((car) => car.name.contains(searchQuery)).toList();
return filteredThings.map((car) => car.name.toString()).toList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment