英文:
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 "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!
答案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 -> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论