-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathManagmentCart.java
More file actions
66 lines (57 loc) · 2.21 KB
/
Copy pathManagmentCart.java
File metadata and controls
66 lines (57 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example.project162_food.Helper;
import android.content.Context;
import android.widget.Toast;
import com.example.project162_food.Domain.Foods;
import java.util.ArrayList;
public class ManagmentCart {
private Context context;
private TinyDB tinyDB;
public ManagmentCart(Context context) {
this.context = context;
this.tinyDB=new TinyDB(context);
}
public void insertFood(Foods item) {
ArrayList<Foods> listpop = getListCart();
boolean existAlready = false;
int n = 0;
for (int i = 0; i < listpop.size(); i++) {
if (listpop.get(i).getTitle().equals(item.getTitle())) {
existAlready = true;
n = i;
break;
}
}
if(existAlready){
listpop.get(n).setNumberInCart(item.getNumberInCart());
}else{
listpop.add(item);
}
tinyDB.putListObject("CartList",listpop);
Toast.makeText(context, "Added to your Cart", Toast.LENGTH_SHORT).show();
}
public ArrayList<Foods> getListCart() {
return tinyDB.getListObject("CartList");
}
public Double getTotalFee(){
ArrayList<Foods> listItem=getListCart();
double fee=0;
for (int i = 0; i < listItem.size(); i++) {
fee=fee+(listItem.get(i).getPrice()*listItem.get(i).getNumberInCart());
}
return fee;
}
public void minusNumberItem(ArrayList<Foods> listItem,int position,ChangeNumberItemsListener changeNumberItemsListener){
if(listItem.get(position).getNumberInCart()==1){
listItem.remove(position);
}else{
listItem.get(position).setNumberInCart(listItem.get(position).getNumberInCart()-1);
}
tinyDB.putListObject("CartList",listItem);
changeNumberItemsListener.change();
}
public void plusNumberItem(ArrayList<Foods> listItem,int position,ChangeNumberItemsListener changeNumberItemsListener){
listItem.get(position).setNumberInCart(listItem.get(position).getNumberInCart()+1);
tinyDB.putListObject("CartList",listItem);
changeNumberItemsListener.change();
}
}