Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. -------------------------CART Item----------------------
  2. package com.example.tuanh.tuanhapp.Model;
  3.  
  4. /**
  5. * Created by TuAnh on 4/20/2018.
  6. */
  7.  
  8. public class CartItem {
  9. private Flower flower;
  10. int count;
  11.  
  12. public CartItem(Flower flower, int count) {
  13. this.flower = flower;
  14. this.count = count;
  15. }
  16.  
  17. public Flower getFlower() {
  18. return flower;
  19. }
  20.  
  21. public void setFlower(Flower flower) {
  22. this.flower = flower;
  23. }
  24.  
  25. public int getCount() {
  26. return count;
  27. }
  28.  
  29. public void setCount(int count) {
  30. this.count = count;
  31. }
  32. }
  33. ------------------------------------CART----------------------------
  34. package com.example.tuanh.tuanhapp.Model;
  35.  
  36. import android.app.Application;
  37.  
  38. import java.util.ArrayList;
  39.  
  40. /**
  41. * Created by TuAnh on 4/17/2018.
  42. */
  43.  
  44. public class Cart extends Application {
  45. private static ArrayList<CartItem> cart ;
  46.  
  47. public static ArrayList<CartItem> getCart() {
  48. if(cart == null)
  49. cart = new ArrayList<>();
  50. return cart;
  51. }
  52.  
  53. public void setCart(ArrayList<CartItem> cart) {
  54. this.cart = cart;
  55. }
  56.  
  57. public static CartItem getFlower(int index){
  58. if(cart == null)
  59. {
  60. cart = new ArrayList<>();
  61. return null;
  62. }
  63. return cart.get(index);
  64. }
  65. public static void AddToCart(Flower fl){
  66. if(cart == null)
  67. cart = new ArrayList<>();
  68. CartItem c = Contains(fl);
  69. if(c == null)
  70. {
  71. CartItem cartItem = new CartItem(fl,1);
  72. cart.add(cartItem);
  73. }
  74. else c.setCount(c.getCount()+1);
  75. //Toast.makeText(g,"da them vao gio hang",Toast.LENGTH_SHORT).show();
  76. }
  77. public static int getCartSize(){
  78. return cart.size();
  79. }
  80.  
  81. /// Contains
  82. public static CartItem Contains(Flower fl){
  83. if(cart == null) return null;
  84. for (CartItem c : cart){
  85. if(c.getFlower().equals(fl))
  86. return c;
  87. }
  88. return null;
  89. }
  90. public static void Remove(int index){
  91. if(cart == null)
  92. cart = new ArrayList<>();
  93. cart.remove(index);
  94. }
  95. public static double Sum(){
  96. if (cart==null) return 0;
  97. double sum =0;
  98. for(CartItem c : cart){
  99. {
  100. Flower f = c.getFlower();
  101. double gia = Double.parseDouble(f.getGia());
  102. sum += (double)c.getCount()*gia;
  103. }
  104. }
  105. return sum;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement