시작하기무료로 시작하기

Computing mean, min, max

You would like to analyze sales data for your coffee business. The data contains the date of each transaction, coffee name, payment method, quantity sold, and transaction amount, as shown in the sample below. Compute the mean, minimum, and maximum transaction amounts formatted to two decimal places.

date coffee_name payment_method quantity amount
3/1/24 Latte cash 1 $6.26
3/5/24 Hot Chocolate card 3 $32.51
3/6/24 Americano card 2 $18.70

The necessary packages such as LocalDate, Arrays, and List have been imported for you.

이 연습은 강의의 일부입니다

Cleaning Data in Java

강의 보기

연습 안내

  • Compute the mean transaction amount using stats.
  • Compute the min and max transaction amounts using stats.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

public class CoffeeSalesExample {
    private record CoffeeSales(LocalDate date, String coffeeName,
            String paymentMethod, int quantity, double amount) {
    }

    public static void main(String[] args) {
        List sales = Arrays.asList(
                new CoffeeSales(LocalDate.of(2024, 3, 1), "Latte", "cash", 1, 6.26),
                new CoffeeSales(LocalDate.of(2024, 3, 5), "Hot Chocolate", "card", 3, 32.51),
                new CoffeeSales(LocalDate.of(2024, 3, 6), "Americano", "card", 2, 18.70));

        DescriptiveStatistics stats = new DescriptiveStatistics();
        sales.forEach(sale -> stats.addValue(sale.amount()));

        System.out.printf("\nSales analyzed: %d%n", sales.size());
        
        // Compute the mean transaction amount
        System.out.printf("Average amount: $%.2f%n", ____.____());
        
        // Compute the min and max transaction amounts
        System.out.printf("Amount range: $%.2f - $%.2f%n",
                ____.____(), ____.____());
    }
}
코드 편집 및 실행