0

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!

3
  • sum+=orders.get(i).getOrderValue();? Commented Oct 5, 2020 at 15:28
  • Read this : baeldung.com/java-stream-sum Commented Oct 5, 2020 at 15:30
  • 3
    You almost close just call getOrderValue() of Order when summing like sum+=orders.get(i).getOrderValue(); Commented Oct 5, 2020 at 15:30

2 Answers 2

2

You could just do :

public double getTotalValue() {
    return orders.stream().mapToDouble(o -> o.getOrderValue()).sum();
}
Sign up to request clarification or add additional context in comments.

4 Comments

You can also use method reference : orders.stream().mapToDouble(Order::getOrderValue()).sum();
when I do that inteliJ shows me an error next to parentheses at the beginning of the method < public double getTotalValue () { > . How can I fix that?
While this is the way I would do it (including the method reference suggested by @Vinetos), it could easily be too advanced for the OP.
I misstyped my method reference : orders.stream().mapToDouble(Order::getOrderValue).sum(); is better.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.