Can now display the total price of the items from each store in shopping list

This commit is contained in:
Aaron Sun 2020-10-24 12:40:28 -07:00
parent 4eddf3ce2d
commit 6fe3110c29
2 changed files with 31 additions and 16 deletions

View File

@ -39,7 +39,7 @@ public class ListPage extends AppCompatActivity {
ArrayList<ListEntry> pListItemPair = new ArrayList<>();
Map<String, Integer> numItemsFromStore = new HashMap<>();
Map<String, Double> totalPriceByStore = new HashMap<>();
Requestor requestor;
@ -80,16 +80,16 @@ public class ListPage extends AppCompatActivity {
item = null;
}
if(item != null) {
if(!numItemsFromStore.containsKey("Kroger") || numItemsFromStore.get("Kroger") == 0) {
if(!totalPriceByStore.containsKey("Kroger")) {
totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity());
pNames.add("Kroger");
pStores.add("");
pPrices.add("$?.??");
pQuantity.add("0");
pPrices.add("$" + totalPriceByStore.get("Kroger"));
pQuantity.add("-1");
pImages.add(-1);
pListItemPair.add(null);
numItemsFromStore.put("Kroger", 1);
pNames.add(item.getDescription());
pStores.add("Kroger");
pPrices.add(item.getPrice().toString());
@ -100,10 +100,13 @@ public class ListPage extends AppCompatActivity {
else {
int index = 0;
while(index < pNames.size() && pNames.get(index).equals("Kroger")) {
while(index < pNames.size() && !pNames.get(index).equals("Kroger")) {
index++;
}
totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity()));
pPrices.set(index, "$" + totalPriceByStore.get("Kroger"));
index++;
pNames.add(index, item.getDescription());
@ -112,8 +115,6 @@ public class ListPage extends AppCompatActivity {
pQuantity.add(index, entry.getQuantity().toString());
pImages.add(index, R.drawable.placeholder);
pListItemPair.add(index, entry);
numItemsFromStore.put("Kroger", numItemsFromStore.get("Kroger") + 1);
}
}
}
@ -161,6 +162,7 @@ public class ListPage extends AppCompatActivity {
public void onClick(View v) {
int q = Integer.parseInt(pQuantity.get(position)) - 1;
pQuantity.set(position, Integer.toString(q));
totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) - 1.0);
ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() - 1);
pListItemPair.add(position, le);
@ -190,6 +192,7 @@ public class ListPage extends AppCompatActivity {
public void onClick(View v) {
int q = Integer.parseInt(pQuantity.get(position)) + 1;
pQuantity.set(position, Integer.toString(q));
totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) + 1.0);
ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() + 1);
pListItemPair.add(position, le);
@ -217,6 +220,8 @@ public class ListPage extends AppCompatActivity {
removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (1.0 * Integer.parseInt(pQuantity.get(position))));
pNames.remove(position);
pStores.remove(position);
pPrices.remove(position);
@ -239,9 +244,24 @@ public class ListPage extends AppCompatActivity {
name.setText(pNames.get(position));
store.setText(pStores.get(position));
price.setText(pPrices.get(position));
if(pQuantity.get(position).equals("-1")) {
quantity.setVisibility(View.INVISIBLE);
decrQuan.setVisibility(View.INVISIBLE);
incrQuan.setVisibility(View.INVISIBLE);
removeItem.setVisibility(View.INVISIBLE);
}
else {
quantity.setText(pQuantity.get(position));
}
if(pImages.get(position) == -1) {
image.setVisibility(View.INVISIBLE);
}
else {
image.setImageResource(pImages.get(position));
}
}
return listproduct;
}

View File

@ -20,11 +20,6 @@
android:id="@+id/nav_lists"
android:icon="@drawable/ic_baseline_list_alt_28"
android:title="@string/menu_lists" />
<!-- <item-->
<!-- android:id="@+id/nav_create_list"-->
<!-- android:icon="@drawable/ic_baseline_add_28"-->
<!-- android:title="Create New List"-->
<!-- android:onClick="onClickCreateList" />-->
<item
android:id="@+id/nav_logout"
android:title="Sign out"