java programming Analyze the spending power of various customer groups,

java程式設計

分析各種客戶族群的消費能力


 資訊系統經常需要分析各種客戶族群的消費能力,請以 C++或 Java 實作以下功能。

寫作一個儲存客戶資訊的 class,稱為 Customer,內含客戶的 ID(身分證字號;

string)、age(年齡;int)、points(消費累積點數;int)等屬性(attributes 或 member

variables)。注意:寫作 Customer 時請作適當的封裝,並提供適當的建構元

(Constructor)。(12 分)

寫作一個副程式計算某年齡範圍的客戶,其點數平均值,格式如下:

// 以下為 C++之 function prototype,若使用 Java,請自行作適當的修改

// input: customers 為一個 vector (或 ArrayList),儲存所有客戶的資訊

// minAge, maxAge: 年齡範圍

// return: 回傳所有年齡介於年齡範圍的客戶之點數平均值

double averagePoints(vector<Customer> &customers, int minAge, int maxAge)

注 意 : 寫 作 averagePoints() 時 請 提 供 適 當 的 include/import , 不 用 寫 呼 叫

averagePoints()的主程式或其他副程式。(13 分

以下是基於Java的 `Customer` 類和 `averagePoints` 方法的實作:


```java

import java.util.ArrayList;


class Customer {

    private String id;

    private int age;

    private int points;


    // Constructor

    public Customer(String id, int age, int points) {

        this.id = id;

        this.age = age;

        this.points = points;

    }


    // Getter methods

    public String getId() {

        return id;

    }


    public int getAge() {

        return age;

    }


    public int getPoints() {

        return points;

    }

}


public class CustomerAnalysis {


    // Method to calculate average points for customers within a specified age range

    public static double averagePoints(ArrayList<Customer> customers, int minAge, int maxAge) {

        int totalPoints = 0;

        int customerCount = 0;


        for (Customer customer : customers) {

            int age = customer.getAge();

            if (age >= minAge && age <= maxAge) {

                totalPoints += customer.getPoints();

                customerCount++;

            }

        }


        // Avoid division by zero

        return (customerCount > 0) ? (double) totalPoints / customerCount : 0;

    }


    public static void main(String[] args) {

        // Example usage

        ArrayList<Customer> customerList = new ArrayList<>();

        customerList.add(new Customer("A123456789", 25, 100));

        customerList.add(new Customer("B987654321", 35, 150));

        customerList.add(new Customer("C456789012", 40, 200));


        int minAge = 30;

        int maxAge = 45;


        double avgPoints = averagePoints(customerList, minAge, maxAge);

        System.out.println("Average points for customers aged between " + minAge + " and " + maxAge + ": " + avgPoints);

    }

}

```


這個程式包含一個 `Customer` 類,該類封裝了客戶的資訊,以及一個 `averagePoints` 方法,該方法計算特定年齡範圍的客戶點數的平均值。在 `main` 方法中,有一個示例用法來展示如何創建 `Customer` 對象並調用 `averagePoints` 方法。


補充

除數不能為零

在數學中,除數不能為零,因為除法的定義涉及將一個數分割成多個相等的部分。當除數為零時,這個分割變得沒有意義。因此,88/0 在數學中是未定義的,無法得到確定的結果。


在程式語言中,當我們嘗試進行除法操作且除數為零時,通常會引發例外或錯誤。這是因為除以零是一種不合理的操作,可能導致不可預測的行為。編程語言設計這樣的規則是為了防止程序出現錯誤並提供一種可控的行為。


如果在程式中進行除法操作時除數為零,通常會引發「ArithmeticException」或類似的例外,開發者可以通過處理這些例外來避免程序崩潰或產生錯誤結果。



Comments

Popular posts from this blog

How to write data into a excel file using vbscript

Format date as yyyy-mm-dd using vbscript