Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class ProductDAO {
  5.  
  6. public List<Product> findAll() {
  7. // TODO Auto-generated method stub
  8.  
  9. Product p1 = new Product("apple", 6.01f);
  10. Product p2 = new Product("lemon", 5.05f);
  11. Product p3 = new Product("mango", 7.05f);
  12. Product p4 = new Product("grapes", 10.05f);
  13.  
  14. Product p5 = new Product("watermelon", 11.01f);
  15. Product p6 = new Product("kiwi", 5.05f);
  16. Product p7 = new Product("pineapple", 4.05f);
  17. Product p8 = new Product("pear", 3f);
  18.  
  19. List<Product> products = new ArrayList<>();
  20.  
  21. products.add(p1);
  22. products.add(p2);
  23. products.add(p3);
  24. products.add(p4);
  25. products.add(p5);
  26. products.add(p6);
  27. products.add(p7);
  28. products.add(p8);
  29.  
  30.  
  31. return products;
  32. }
  33.  
  34. public float getTotal() {
  35.  
  36. List<Product> products = this.findAll();
  37. float total = 0;
  38. for(Product p : products) {
  39. total = total + p.getPrice();
  40.  
  41. }
  42. return total;
  43. }
  44.  
  45. public float getAverage() {
  46.  
  47. float total = this.getTotal();
  48. int len = this.findAll().size();
  49. float average = total/len;
  50. return average;
  51.  
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement