Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. public class AggregateUtil {
  2. //static methods that do the calculations..
  3.  
  4.  
  5. static double getTheAveragePriceForEachCategory(List<Double> product){
  6. double average = getTotalSalesValueForEachCategory(product)/(double)getNumberOfProductsInEachCategory(product);
  7. return Math.round(average * 100D) / 100D;
  8. }
  9.  
  10. static double getTotalSalesValueForEachCategory(List<Double> product){
  11. double sum = 0.0;
  12. for(Double value : product){
  13. sum+=value;
  14. }
  15. return Math.round(sum * 100D) / 100D;
  16. }
  17.  
  18. static int getNumberOfProductsInEachCategory(List<Double> product){
  19. return (product.size());
  20. }
  21.  
  22. static double getTheMostExpensiveProductInEachCategory(List<Double> product){
  23. double highest = Double.MIN_VALUE;
  24. for(Double value : product){
  25. if(value>highest) highest = value;
  26. }
  27. return highest;
  28. }
  29. }
  30.  
  31. public class DisplayUtil {
  32. //prints the values under each product category..
  33. public static void DisplayProducts( Map<String, List<Double>> db){
  34. StringBuilder builder = new StringBuilder();
  35. for (Map.Entry<String,List<Double>> entry : db.entrySet()) {
  36. builder = new StringBuilder();
  37. System.out.println(entry.getKey());
  38. System.out.println("----------------------------------");
  39. builder.append("average_price = ");
  40. double avrg_price = AggregateUtil.getTheAveragePriceForEachCategory(entry.getValue());
  41. builder.append(avrg_price);
  42. builder.append("n");
  43. builder.append("total_sales_value = ");
  44. double total_sales = AggregateUtil.getTotalSalesValueForEachCategory(entry.getValue());
  45. builder.append(total_sales);
  46. builder.append("n");
  47. builder.append("number_of_products = ");
  48. double numberOfProducts = AggregateUtil.getNumberOfProductsInEachCategory(entry.getValue());
  49. builder.append(numberOfProducts);
  50. builder.append("n");
  51. builder.append("most_expensive = ");
  52. double mostExpensive = AggregateUtil.getTheMostExpensiveProductInEachCategory(entry.getValue());
  53. builder.append(mostExpensive);
  54. builder.append("n");
  55. System.out.println(builder + "n");
  56.  
  57. }
  58. }
  59. }
  60.  
  61. public static void ReadFile(String path, Map<String, List<Double>> db){
  62. try(BufferedReader br = new BufferedReader(new FileReader(path))){
  63. String line;
  64. if((line = br.readLine()) != null){
  65. //this is the first heading..and needed to be skipped
  66. }
  67. while ((line = br.readLine()) != null) {
  68. String[] keys = line.split(",");
  69. keys[1] = keys[1].replace(""", "");
  70. if(db.containsKey(keys[1])){
  71. List<Double> list = db.get(keys[1]);
  72. list.add(Double.valueOf(keys[2]));
  73. db.put(keys[1], list);
  74.  
  75. }
  76. else{
  77. List<Double> list = new ArrayList<>();
  78. list.add(Double.valueOf(keys[2]));
  79. db.put(keys[1],list);
  80. }
  81. }
  82.  
  83. }catch(IOException e){}
  84. }
  85.  
  86.  
  87. }
  88.  
  89. public class ControllerClass {
  90.  
  91. public static void main(String []args){
  92. Map<String, List<Double>> db = new HashMap<>();
  93. FileUtils.ReadFile("./Files/products.csv", db);
  94. DisplayUtil.DisplayProducts(db);
  95.  
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement