-2

My code is pretty long but I will include the relevant snippet. I am creating a GUI with elements of a store. I have a textfield called popularItemsTxt that contains the 3 most popular items of my list.

for(int i =0; i<3;i++) {
    if(electronicStore.stock[i] != null) {
    popularTxt.getItems().add(electronicStore.stock[i].toString());
    }
}

However I have a button that computes the items in the final cart and when this button is pressed I want to be able to find the 3 most common items that have been added to that list. Any help/direction would be greatly appreciated.

 btnComplete.setOnAction(new EventHandler<ActionEvent>() {
         public void handle(ActionEvent e) {

            //if the list has a minimum of 1 item
             if (currentCartTxt.getItems().size() > 0) {
              for(int i =0; i<3;i++) {
                            if(electronicStore.stock[i] != null) {
                                popularTxt.getItems().add(electronicStore.stock[i].toString());
                                List<ElectronicStore> topThree = Arrays.stream(electronicStore.stock)
                                         .sorted(Comparator.comparing(ElectronicStore::createStore).reversed())
                                         .limit(3)
                                         .collect(Collectors.toList());
                            }
                        }
    }        
});

Right now the output is simply the first three items in my arraylist. I get an error when comparing

coding101
  • 27
  • 5
  • If you already have the list of items selected and a means of determining popularity for each item, the simplest way is to sort the bought items by popularity and only look at the first three. Am I missing something or would that work? – kingkupps Mar 11 '19 at 01:48
  • That's exactly what I was thinking however I wasn;t sure how to sort the most frequent items – coding101 Mar 11 '19 at 01:49

2 Answers2

2

Assuming stock is an Array of objects that has an int field such as views that you are using to judge the popularity, you can (java 8+) use Streams to accomplish this:

Class[] topThree =  Arrays.stream(stock)
                          .sorted(Comparator.comparingInt(Class::getViews).reversed())
                          .limit(3)
                          .toArray(Class[]::new);

Where getViews is the method that retrieves the int property you are comparing, and Class is the type of the object stored in stock

Or if you want a List<Class>:

List<Class> topThree = Arrays.stream(stock)
                             .sorted(Comparator.comparingInt(Class::getViews).reversed())
                             .limit(3)
                             .collect(Collectors.toList());

Or looking at your example, it looks as if you just want to add the Stringrepresentation of the objects to popularTxt:

Arrays.stream(stock)
      .sorted(Comparator.comparingInt(Class::getViews).reversed())
      .limit(3)
      .map(Class::toString)
      .forEach(popularTxt.getItems()::add);
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • Hi okay so I implemented your method however I'm pretty new to JAVA so I've never seen the Comparator tool used before. I am getting an error for the (Class::getRating), any idea why? I've updated my code – coding101 Mar 11 '19 at 02:09
  • @coding101 It's hard to say without seeing the code for the classes. `Class`, and `getViews` were place holder names for your class name and method name. – GBlodgett Mar 11 '19 at 02:12
  • okay well what method would I be using? because my getStock() method simply return a list of all my stock – coding101 Mar 11 '19 at 02:14
  • @coding101 How are you determining the popularity of the objects? – GBlodgett Mar 11 '19 at 02:34
0

Each item in the store (assuming items are objects) should have a property to indicate its popularity (an int member variable). Every time an item is getting added to the cart, increment the popularity indicator variable by 1, also when the item gets removed for some reason reduce the value by 1. This can be done inside the 'add' and 'remove' button action listener methods.

So finally, when the cart(assuming this is also list of objects) is complete(that is, when btnComplete is pressed), you can sort list of objects inside the cart, based on the popularity value in descending order, and get the first three items after sorting.