Lọc bảng
Giờ khi bạn đã tải được danh mục sản phẩm cơ bản, startup muốn tập trung vào các sản phẩm cao cấp cho khóa đào tạo nâng cao. Bạn cần xác định sản phẩm nào được coi là "premium" (giá trên $800) và tạo một tập dữ liệu đã lọc cho đội marketing.
Các lớp Table, DoubleColumn và Selection đã được import sẵn cho bạn.
Bài tập này là một phần của khóa học
Nhập dữ liệu trong Java
Hướng dẫn bài tập
- Tạo một
Selectioncho"Price"lớn hơn800. - Lọc bảng sản phẩm bằng
Selectionđó. - In số hàng của các sản phẩm premium.
- In giá trung bình của các sản phẩm premium.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
public class ProductCatalog {
public static void main(String[] args) {
try {
Table products = Table.read().csv("products.csv");
// Create a Selection for Price greater than 800
Selection premiumSelection = products.doubleColumn("Price")
.____(____);
// Filter the products table using the Selection
Table premiumProducts = products.____(____);
// Print the row count of premium products
System.out.println("Number of premium products: " + premiumProducts.____());
// Print the mean price of premium products
DoubleColumn premiumPrices = premiumProducts.doubleColumn("Price");
System.out.println("Average price of premium products: $" + premiumPrices.____());
} catch (Exception e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
}
}