0

I'm struggling with the following:

I'm building an order system. I have an ArrayList "sales", the list contains some variables and a HashSet with the items that were sold as part of the order. There's a Part class with getters for the part name (getName) and part price (getPrice).

The "sales" ArrayList contains: (int orderNumber, LocalDate date, OrderItem orderItem)

And looks like this:

Order: 1, date: 2020-02-23, Items: [part: Monitor, quantity: 3, part: CPU, quantity: 2]

Order: 2, date: 2020-02-23, Items: [part: Headset, quantity: 1, part: Case, quantity: 1]

Order: 3, date: 2020-02-23, Items: [part: CPU, quantity: 10, part: Keyboard, quantity: 10]

Each OrderItem contains: (Part part, int quantity). The Part class contains (String partName, double price).

What I want to do is create a new collection that lists the total number of parts that were sold on a day, like this:

Monitor 3

CPU 12

Headset 1

Case 1

Keyboard 10

How can I do this?

TomZ
  • 1
  • 1

1 Answers1

0

I tried to write something for you using a HashMap<String, Integer>, which will contain a key-value pair list of your Parts and their corresponding sold quantities for the day.

Hope it helps:

public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    for (Orderitem item: listOfOrderItems) { // here you need to get a list of your ordered items for the day
        Part part = item.getPart(); // here you need some form of a getter to access the given part from the given Orderitem
        String partName = part.getName();
        if (!map.containsKey(partName)) {
            map.put(partName, item.getQuantity());
        } else {
            map.put(partName, map.get(partName) + item.getQuantity());
        }
    }
}
Petar Bivolarski
  • 1,599
  • 10
  • 19