总价值 Java ArrayList

huangapple go评论67阅读模式
英文:

total value java arraylist

问题

以下是您提供的代码的翻译部分:

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;
    }
}

请注意,您提供的代码存在一些问题,特别是在Shop类中的方法实现。例如,between方法和getMaxMinValue方法的实现都不完整,getTotalValue方法中的计算也存在错误。如果需要更详细的帮助,请随时提问。

英文:

So I'm learning how to deal with Java, and here I have a question;
My goal is to make
Order class, which will store the order data: order value, date and login of the person placing the order. Next, create a Shop class that will hold a collection of unique orders.

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 &quot;Order{&quot; +
                &quot;orderValue=&quot; + orderValue +
                &quot;, date=&quot; + date +
                &quot;, customerLogin=&#39;&quot; + customerLogin + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
}
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&lt;Order&gt; orders = new ArrayList&lt;&gt;();

    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 &lt; min) {
            a = min;
        } else if (a &gt; max) {
            a = max;
        }
    }

    public int getSize() {
        return this.orders.size();
    }
   
    public double getTotalValue() {
        int sum=0;
        for(int i=0; i&lt;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!

答案1

得分: 2

你可以这样做:

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

You could just do :

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

答案2

得分: 2

Sure, here's the translated content:

将您的代码行更改为:

sum += orders.get(i).getOrderValue();  // 您在此处获取到了订单对象的实际值
英文:

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

huangapple
  • 本文由 发表于 2020年10月5日 23:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定