PIbd-33_Dyakonov_R_R_PMD/code.txt
dyakonovr 219ce4f589 lab2
2024-09-11 15:44:41 +04:00

72 lines
1.7 KiB
Plaintext

// Classes +
// Methods +
// Enums +
// Loops +
// Generics (можно использовать List<>) +-
// Anonymous functions +
// Future +
// extension +
enum Category { PHONE, COMPUTER, TV }
extension ProductX on Product {
double get discountedPrice => price * 0.9; // Применяем 10% скидку
String get fullname => '${name} (${category.name}) - \$${price}';
}
class Product {
String name;
double price;
int quantity;
Category category;
// Constructor
Product(this.name, this.price, this.quantity, this.category);
}
class Cart {
final List<Product> items = [];
void push(Product p) {
items.add(p);
}
double getTotalPrice() {
double price = 0;
for (int i = 0; i < items.length; i++) {
price += items[i].quantity * items[i].price;
}
return price;
}
int getItemsCount() {
return items.fold(0, (total, item) => total + item.quantity);
}
Future<List<Product>> getItemsAsync() async {
// Как бы делаем запрос к бэку
await Future.delayed(Duration(seconds: 3));
return items;
}
}
void main() {
Cart cart = Cart();
Product product = Product('Laptop', 1000.0, 1, Category.COMPUTER);
cart.push(product);
cart.push(Product('Smartphone', 500.0, 2, Category.PHONE));
cart.push(Product('TV', 800.0, 1, Category.TV));
print('Total price: \$${cart.getTotalPrice()}');
print('Total items count: ${cart.getItemsCount()}');
print('Product with discount: ${product.discountedPrice}');
print('Product fullname: ${product.fullname}');
Future.delayed(Duration(seconds: 3)).then((_) => print(product.name));
// List<Product> products = await cart.getItemsAsync();
// print('Number of elements received from async method: ${products.length}');
}