Update total price when quantities change and items are removed

This commit is contained in:
Clayton Wilson 2020-10-25 23:06:56 -04:00
parent caf929a71e
commit 04a944f2d5

View File

@ -16,7 +16,6 @@ import com.example.listify.data.ListEntry;
import org.json.JSONException; import org.json.JSONException;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
@ -29,6 +28,7 @@ public class ListPage extends AppCompatActivity {
Button incrQuan; Button incrQuan;
Button decrQuan; Button decrQuan;
Button removeItem; Button removeItem;
TextView tvTotalPrice;
ArrayList<String> pNames = new ArrayList<>(); ArrayList<String> pNames = new ArrayList<>();
ArrayList<String> pStores = new ArrayList<>(); ArrayList<String> pStores = new ArrayList<>();
@ -39,6 +39,7 @@ public class ListPage extends AppCompatActivity {
ArrayList<ListEntry> pListItemPair = new ArrayList<>(); ArrayList<ListEntry> pListItemPair = new ArrayList<>();
Requestor requestor; Requestor requestor;
double totalPrice = 0;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -65,7 +66,6 @@ public class ListPage extends AppCompatActivity {
list = null; list = null;
} }
double totalPrice = 0;
if(list != null) { if(list != null) {
for (ListEntry entry : list.getEntries()) { for (ListEntry entry : list.getEntries()) {
int product = entry.getProductID(); int product = entry.getProductID();
@ -118,7 +118,7 @@ public class ListPage extends AppCompatActivity {
listView.setAdapter(myAdapter); listView.setAdapter(myAdapter);
TextView tvTotalPrice = (TextView) findViewById(R.id.total_price); tvTotalPrice = (TextView) findViewById(R.id.total_price);
tvTotalPrice.setText(String.format("$%.2f", totalPrice)); tvTotalPrice.setText(String.format("$%.2f", totalPrice));
} }
@ -158,6 +158,10 @@ public class ListPage extends AppCompatActivity {
ListEntry le = pListItemPair.remove(position); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() - 1); le.setQuantity(le.getQuantity() - 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
totalPrice -= Double.parseDouble(pPrices.get(position));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>(); SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer); requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try { try {
@ -187,6 +191,10 @@ public class ListPage extends AppCompatActivity {
ListEntry le = pListItemPair.remove(position); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() + 1); le.setQuantity(le.getQuantity() + 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
totalPrice += Double.parseDouble(pPrices.get(position));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>(); SynchronousReceiver<Integer> synchronousenforcer = new SynchronousReceiver<>();
requestor.deleteObject(le, synchronousenforcer, synchronousenforcer); requestor.deleteObject(le, synchronousenforcer, synchronousenforcer);
try { try {
@ -211,6 +219,10 @@ public class ListPage extends AppCompatActivity {
removeItem.setOnClickListener(new View.OnClickListener() { removeItem.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
totalPrice -= (Double.parseDouble(pPrices.get(position)) *
Double.parseDouble(pQuantity.get(position)));
tvTotalPrice.setText(String.format("$%.2f", totalPrice));
pNames.remove(position); pNames.remove(position);
pStores.remove(position); pStores.remove(position);
pPrices.remove(position); pPrices.remove(position);