Compare commits

...

1 Commits

Author SHA1 Message Date
0d06c8a009 Laba_2 2024-10-02 19:33:03 +04:00
2 changed files with 118 additions and 1 deletions

117
lib/ApplicationAuto.dart Normal file
View File

@ -0,0 +1,117 @@
import 'dart:async';
enum CarType { sedan, suv, truck, coupe, hatchback }
class Car {
final String brand;
final CarType type;
final double price;
Car(this.brand, this.type, this.price);
void displayInfo() {
print('Car: $brand, Type: ${carTypeToString(type)}, Price: \$${price.toStringAsFixed(2)}');
}
static String carTypeToString(CarType type) {
switch (type) {
case CarType.sedan:
return 'Sedan';
case CarType.suv:
return 'SUV';
case CarType.truck:
return 'Truck';
case CarType.coupe:
return 'Coupe';
case CarType.hatchback:
return 'Hatchback';
default:
return 'Unknown';
}
}
}
extension CarExtension on Car {
String get detailedInfo => 'Detailed Info: Brand: $brand, Type: ${Car.carTypeToString(type)}, Price: \$${price.toStringAsFixed(2)}';
}
class AutoCenter<T extends Car> {
final List<T> cars = [];
void addCar(T car) {
cars.add(car);
}
List<T> findCarsByType(CarType type) {
return cars.where((car) => car.type == type).toList();
}
void showAllCars() {
for (var car in cars) {
car.displayInfo();
}
}
}
class PriceFilter {
static List<T> filterByPrice<T extends Car>(List<T> cars, double minPrice, double maxPrice) {
return cars.where((car) => car.price >= minPrice && car.price <= maxPrice).toList();
}
}
class ApplicationAuto {
final AutoCenter<Car> autoCenter = AutoCenter<Car>();
Future<List<Car>> fetchCarsFromDatabase() async {
return Future.delayed(Duration(seconds: 2), () {
return [
Car('Toyota', CarType.sedan, 25000),
Car('Ford', CarType.suv, 35000),
Car('Chevrolet', CarType.truck, 40000),
Car('BMW', CarType.coupe, 55000),
Car('Volkswagen', CarType.hatchback, 20000),
];
});
}
Future<void> loadCars() async {
List<Car> carsFromDB = await fetchCarsFromDatabase();
carsFromDB.forEach((car) => autoCenter.addCar(car));
}
void displayAllCars() {
print('All cars in the auto center:');
autoCenter.showAllCars();
}
void displayCarsByType(CarType type) {
print('\nFiltered cars by type (${Car.carTypeToString(type)}):');
var carsByType = autoCenter.findCarsByType(type);
carsByType.forEach((car) => car.displayInfo());
}
void displayCarsByPriceRange(double minPrice, double maxPrice) {
print('\nFiltered cars by price range (\$$minPrice - \$$maxPrice):');
var filteredCars = PriceFilter.filterByPrice(autoCenter.cars, minPrice, maxPrice);
filteredCars.forEach((car) => car.displayInfo());
}
void displayDetailedInfoForAllCars() {
print('\nDetailed Information for All Cars:');
autoCenter.cars.forEach((car) => print(car.detailedInfo)); // Использование метода расширения detailedInfo()
}
}
void main() async {
var app = ApplicationAuto();
await app.loadCars();
app.displayAllCars();
app.displayCarsByType(CarType.sedan);
app.displayCarsByPriceRange(20000, 40000);
app.displayDetailedInfoForAllCars();
}

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
runApp(const MyApp()); //runApp(const MyApp());
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {