From 4b8561ebd7fd34fac1bdc7773930c57146a9be23 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Sat, 31 Oct 2020 17:25:56 -0700 Subject: [PATCH 1/2] Removed my grand total code (Clayton's job) and reformatted shopping list page --- .../java/com/example/listify/ListPage.java | 102 +++++++----------- .../app/src/main/res/layout/activity_list.xml | 39 +++++-- 2 files changed, 69 insertions(+), 72 deletions(-) diff --git a/Listify/app/src/main/java/com/example/listify/ListPage.java b/Listify/app/src/main/java/com/example/listify/ListPage.java index 6f3044f..c965314 100644 --- a/Listify/app/src/main/java/com/example/listify/ListPage.java +++ b/Listify/app/src/main/java/com/example/listify/ListPage.java @@ -36,6 +36,7 @@ public class ListPage extends AppCompatActivity { Button incrQuan; Button decrQuan; Button removeItem; + Button clearAll; ArrayList pNames = new ArrayList<>(); ArrayList pStores = new ArrayList<>(); @@ -52,15 +53,31 @@ public class ListPage extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - // Read list ID from caller + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_list); + final int listID = (int) getIntent().getSerializableExtra("listID"); - pNames.add("Total Price"); - pStores.add(""); - pPrices.add("0.00"); - pQuantity.add("-1"); - pImages.add("-1"); - pListItemPair.add(null); + clearAll = (Button) findViewById(R.id.buttonClear); + clearAll.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + pNames.clear(); + pStores.clear(); + pPrices.clear(); + pQuantity.clear(); + pImages.clear(); + + while(!pListItemPair.isEmpty()) { + try { + requestor.deleteObject(pListItemPair.remove(0)); + } + catch(Exception e) {} + } + + listView.setAdapter(myAdapter); + } + }); Properties configs = new Properties(); try { @@ -98,9 +115,6 @@ public class ListPage extends AppCompatActivity { totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity()); storeHeaderIndex.put("Kroger", pNames.size()); - double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity()); - pPrices.set(0, String.valueOf(newTotal)); - pNames.add("Kroger"); pStores.add(""); pPrices.add(df.format(totalPriceByStore.get("Kroger"))); @@ -121,9 +135,6 @@ public class ListPage extends AppCompatActivity { totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity())); pPrices.set(index, df.format(totalPriceByStore.get("Kroger"))); - double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity()); - pPrices.set(0, df.format(newTotal)); - index++; pNames.add(index, item.getDescription()); @@ -143,12 +154,8 @@ public class ListPage extends AppCompatActivity { } } - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_list); - listView = findViewById(R.id.listView); myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages); - listView.setAdapter(myAdapter); } @@ -187,8 +194,6 @@ public class ListPage extends AppCompatActivity { pQuantity.set(position, Integer.toString(q)); totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) - Double.parseDouble(pPrices.get(position))); pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); - double newTotal = Double.parseDouble(pPrices.get(0)) - Double.parseDouble(pPrices.get(position)); - pPrices.set(0, df.format(newTotal)); ListEntry le = pListItemPair.remove(position); le.setQuantity(le.getQuantity() - 1); pListItemPair.add(position, le); @@ -220,8 +225,6 @@ public class ListPage extends AppCompatActivity { pQuantity.set(position, Integer.toString(q)); totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) + Double.parseDouble(pPrices.get(position))); pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); - double newTotal = Double.parseDouble(pPrices.get(0)) + Double.parseDouble(pPrices.get(position)); - pPrices.set(0, df.format(newTotal)); ListEntry le = pListItemPair.remove(position); le.setQuantity(le.getQuantity() + 1); pListItemPair.add(position, le); @@ -249,41 +252,16 @@ public class ListPage extends AppCompatActivity { removeItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if(position == 0) { - pNames.clear(); - pStores.clear(); - pPrices.clear(); - pQuantity.clear(); - pImages.clear(); + totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position)))); + pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); - pNames.add("Total Price"); - pStores.add(""); - pPrices.add("0.00"); - pQuantity.add("-1"); - pImages.add("-1"); + pNames.remove(position); + pStores.remove(position); + pPrices.remove(position); + pQuantity.remove(position); + pImages.remove(position); - while(pListItemPair.size() > 1) { - try { - requestor.deleteObject(pListItemPair.remove(1)); - } - catch(Exception e) {} - } - } - else { - totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position)))); - pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); - - double newTotal = Double.parseDouble(pPrices.get(0)) - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position))); - pPrices.set(0, df.format(newTotal)); - - pNames.remove(position); - pStores.remove(position); - pPrices.remove(position); - pQuantity.remove(position); - pImages.remove(position); - - requestor.deleteObject(pListItemPair.remove(position)); - } + requestor.deleteObject(pListItemPair.remove(position)); listView.setAdapter(myAdapter); } @@ -301,23 +279,17 @@ public class ListPage extends AppCompatActivity { price.setText("$" + pPrices.get(position)); if(pQuantity.get(position).equals("-1")) { - quantity.setVisibility(View.INVISIBLE); - decrQuan.setVisibility(View.INVISIBLE); - incrQuan.setVisibility(View.INVISIBLE); - - if(position == 0) { - removeItem.setText("Clear all"); - } - else { - removeItem.setVisibility(View.INVISIBLE); - } + quantity.setVisibility(View.GONE); + decrQuan.setVisibility(View.GONE); + incrQuan.setVisibility(View.GONE); + removeItem.setVisibility(View.GONE); } else { quantity.setText(pQuantity.get(position)); } if(pImages.get(position).equals("-1")) { - image.setVisibility(View.INVISIBLE); + image.setVisibility(View.GONE); } else { Glide.with(getContext()).load(pImages.get(position)).into(image); diff --git a/Listify/app/src/main/res/layout/activity_list.xml b/Listify/app/src/main/res/layout/activity_list.xml index 65c12f4..2dc9220 100644 --- a/Listify/app/src/main/res/layout/activity_list.xml +++ b/Listify/app/src/main/res/layout/activity_list.xml @@ -1,14 +1,39 @@ - - +