有三个客户对象,将三个客户对象存入集合中,并将其按照消费总额从高到低排序

区块链毕设网qklbishe.com为您提供问题的解答

有三个客户对象,将三个客户对象存入集合中,并将其按照消费总额从高到低排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Customer customer1 = new Customer("小明",scanner.nextInt());
        Customer customer2 = new Customer("小军",scanner.nextInt());
        Customer customer3 = new Customer("小红",scanner.nextInt());
        List<Customer> customers = new ArrayList<>();
        customers.add(customer1);
        customers.add(customer2);
        customers.add(customer3);

        //write your code here……
        Collections.sort(customers);

        System.out.println(customers);

    }
}

class Customer implements Comparable<Customer>{
    private String name;
    private int consumption;

    public Customer(String name, int consumption) {
        this.name = name;
        this.consumption = consumption;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "name=’" + name + ”’ +
                ", consumption=" + consumption +
                ‘}’;
    }

    //write your code here……
    public int compareTo(Customer c){
        return c.consumption- this.consumption;
    }

}

26:42

以上就是关于问题有三个客户对象,将三个客户对象存入集合中,并将其按照消费总额从高到低排序的答案

欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。

区块链NFT链游项目方科学家脚本开发培训

从业7年-专注一级市场


微信:btc9767
TELEGRAM :https://t.me/btcok9

具体资料介绍

web3的一级市场千万收益的逻辑


进群点我



qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台-javagopython毕设 » 有三个客户对象,将三个客户对象存入集合中,并将其按照消费总额从高到低排序