简体   繁体   中英

Java Map<String,Map<String,Integer> use to populate results

I want to create one report that you select product and storeName and returns all the sales per date (within a range).

The db view that helped me with other reports looks like this: product - store_name - date

So far my approach is to return all records from the db in a list and then I do the following:

public void salesReport(String product, String store, String date){
    List<RecordSalesInfo> salesResults = salesDao.getSales();
    Map<String, Integer> mapper = new HashMap();

    //calculation
        for (RecordSalesInfo record : salesResults) {
            StringBuilder key = new StringBuilder();
            key.append(record.getProduct()).append(":")
                        .append(record.getStoreName()).append(":")
                        .append(record.getWageredDate()).append(":");

            if (mapper.containsKey(key.toString())) {
                    Integer get = mapper.get(key.toString());
                    get++;
                    mapper.put(key.toString(), get);
            } else {
                    mapper.put(key.toString(), 1);
            }
       }

       for (String key : mapper.keySet()) {
           if(key.toString.equals("Pen:London:13June2016"){ 
               System.out.println("sales:" + mapper.get(key.toString);
           }
       }
}

the query in the salesDao(saving as "RecordSalesInfo") is:

SELECT
 rs.product AS product,
 rs.store_name AS store,
 rs.date AS date,
 rs.product_id AS productId
 FROM sales rs
 ORDER BY rs.product,rs.store_name,rs.date

The reason I didn't query "Count(blabla) where product='a' and store_name='b' and date='c' " is because the user changes the input using a jSlider very often (input=product,store,date), that means too many queries. So I thought it is better to take all the results from db and then display what the user needs.

A) Is there a better way to do this?

B) In the next phase the user will enter only the product and the store and I have to return the list of the sales by date, looking like this:

Pen - London (manual input) 12June2016 100 15June2016 30 19July2016 67

With what I have done so far, I can't get only the dates that I have sales, and I have to "search" from the hashMap for all dates(specific range). I think as a solution to change the key to "product:storeName" one the existing map and have as a value another map where the String will be the date and the Integer the amount of sales.

Again is there a better way on doing that?

What I would do is that I will not hold/keep a map, rather I will fetch the results as the query changes based on the JSlider etc.

You can do one thing that when a query is sent until its results are available you can disable your Jslider and show a spinner that notifies the user that processing is going on.

Once the result is available, enable the slider and hide the spinner.

To have all the data in the map does not seem a good idea. It will be a disconnected state of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM