简体   繁体   English

总值java数组列表

[英]total value java arraylist

So I'm learning how to deal with Java, and here I have a question;所以我正在学习如何处理 Java,这里我有一个问题; My goal is to make Order class, which will store the order data: order value, date and login of the person placing the order.我的目标是制作 Order 类,它将存储订单数据:订单值、日期和下订单者的登录名。 Next, create a Shop class that will hold a collection of unique orders.接下来,创建一个 Shop 类,该类将保存唯一订单的集合。

The newly created class should have the following functionalities:新创建的类应具有以下功能:

adding a new order, returning a list of orders within a range of two dates, picking orders based on the transferred scope (lowest and highest order value), return the number of orders, adding up the value of all orders.添加新订单,返回两个日期范围内的订单列表,根据转移的范围(最低和最高订单值)挑选订单,返回订单数量,将所有订单的价值相加。

Here's what I've got so far;这是我到目前为止所得到的;

public class Order {

    private double orderValue;
    private double date;
    private String customerLogin;

    public Order(double orderValue, double date, String customerLogin) {
        this.orderValue = orderValue;
        this.date = date;
        this.customerLogin = customerLogin;
    }

    public double getOrderValue() {
        return orderValue;
    }

    public double getDate() {
        return date;
    }

    public String getCustomerLogin() {
        return customerLogin;
    }

    @Override
    public String toString() {
        return "Order{" +
                "orderValue=" + orderValue +
                ", date=" + date +
                ", customerLogin='" + customerLogin + '\'' +
                '}';
    }
}
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Shop {

    LocalDate current = LocalDate.now();
    LocalDate twoYearsAgo = current.minusYears(2);
    int max = 0;
    int min = 30;

    private List<Order> orders = new ArrayList<>();

    public void addOrder(Order order) {
        this.orders.add(order);
    }

    public void between(LocalDate current, LocalDate twoYearsAgo) {
        if (current.isBefore(twoYearsAgo)) {
           return;
        }
    }
  
    public void getMaxMinValue () {
        int a = 0;
        if (a < min) {
            a = min;
        } else if (a > max) {
            a = max;
        }
    }

    public int getSize() {
        return this.orders.size();
    }
   
    public double getTotalValue() {
        int sum=0;
        for(int i=0; i<orders.size(); i++) {
            sum+=orders.get(i);
        }
        return sum;

    }
}

As you can see, I'm missing the method that would return total value of all orders, I would really use some help with that, thanks!如您所见,我缺少返回所有订单总价值的方法,我真的会为此提供一些帮助,谢谢!

You could just do :你可以这样做:

public double getTotalValue() {
    return orders.stream().mapToDouble(o -> o.getOrderValue()).sum();
}

Change your line:更改您的线路:

sum+=orders.get(i);                   // you are getting an Order object here

to

sum+=orders.get(i).getOrderValue();  // you are getting the actual value of the retrieved Order object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM