Removed my grand total code (Clayton's job) and reformatted shopping list page

This commit is contained in:
Aaron Sun 2020-10-31 17:25:56 -07:00
parent 7cf6c0534a
commit 4b8561ebd7
2 changed files with 69 additions and 72 deletions

View File

@ -36,6 +36,7 @@ public class ListPage extends AppCompatActivity {
Button incrQuan; Button incrQuan;
Button decrQuan; Button decrQuan;
Button removeItem; Button removeItem;
Button clearAll;
ArrayList<String> pNames = new ArrayList<>(); ArrayList<String> pNames = new ArrayList<>();
ArrayList<String> pStores = new ArrayList<>(); ArrayList<String> pStores = new ArrayList<>();
@ -52,15 +53,31 @@ public class ListPage extends AppCompatActivity {
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { 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"); final int listID = (int) getIntent().getSerializableExtra("listID");
pNames.add("Total Price"); clearAll = (Button) findViewById(R.id.buttonClear);
pStores.add(""); clearAll.setOnClickListener(new View.OnClickListener() {
pPrices.add("0.00"); @Override
pQuantity.add("-1"); public void onClick(View v) {
pImages.add("-1"); pNames.clear();
pListItemPair.add(null); 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(); Properties configs = new Properties();
try { try {
@ -98,9 +115,6 @@ public class ListPage extends AppCompatActivity {
totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity()); totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity());
storeHeaderIndex.put("Kroger", pNames.size()); 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"); pNames.add("Kroger");
pStores.add(""); pStores.add("");
pPrices.add(df.format(totalPriceByStore.get("Kroger"))); 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())); totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity()));
pPrices.set(index, df.format(totalPriceByStore.get("Kroger"))); 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++; index++;
pNames.add(index, item.getDescription()); 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); listView = findViewById(R.id.listView);
myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages); myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages);
listView.setAdapter(myAdapter); listView.setAdapter(myAdapter);
} }
@ -187,8 +194,6 @@ public class ListPage extends AppCompatActivity {
pQuantity.set(position, Integer.toString(q)); pQuantity.set(position, Integer.toString(q));
totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) - Double.parseDouble(pPrices.get(position))); 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)))); 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); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() - 1); le.setQuantity(le.getQuantity() - 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
@ -220,8 +225,6 @@ public class ListPage extends AppCompatActivity {
pQuantity.set(position, Integer.toString(q)); pQuantity.set(position, Integer.toString(q));
totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) + Double.parseDouble(pPrices.get(position))); 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)))); 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); ListEntry le = pListItemPair.remove(position);
le.setQuantity(le.getQuantity() + 1); le.setQuantity(le.getQuantity() + 1);
pListItemPair.add(position, le); pListItemPair.add(position, le);
@ -249,41 +252,16 @@ 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) {
if(position == 0) { totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position))));
pNames.clear(); pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position))));
pStores.clear();
pPrices.clear();
pQuantity.clear();
pImages.clear();
pNames.add("Total Price"); pNames.remove(position);
pStores.add(""); pStores.remove(position);
pPrices.add("0.00"); pPrices.remove(position);
pQuantity.add("-1"); pQuantity.remove(position);
pImages.add("-1"); pImages.remove(position);
while(pListItemPair.size() > 1) { requestor.deleteObject(pListItemPair.remove(position));
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));
}
listView.setAdapter(myAdapter); listView.setAdapter(myAdapter);
} }
@ -301,23 +279,17 @@ public class ListPage extends AppCompatActivity {
price.setText("$" + pPrices.get(position)); price.setText("$" + pPrices.get(position));
if(pQuantity.get(position).equals("-1")) { if(pQuantity.get(position).equals("-1")) {
quantity.setVisibility(View.INVISIBLE); quantity.setVisibility(View.GONE);
decrQuan.setVisibility(View.INVISIBLE); decrQuan.setVisibility(View.GONE);
incrQuan.setVisibility(View.INVISIBLE); incrQuan.setVisibility(View.GONE);
removeItem.setVisibility(View.GONE);
if(position == 0) {
removeItem.setText("Clear all");
}
else {
removeItem.setVisibility(View.INVISIBLE);
}
} }
else { else {
quantity.setText(pQuantity.get(position)); quantity.setText(pQuantity.get(position));
} }
if(pImages.get(position).equals("-1")) { if(pImages.get(position).equals("-1")) {
image.setVisibility(View.INVISIBLE); image.setVisibility(View.GONE);
} }
else { else {
Glide.with(getContext()).load(pImages.get(position)).into(image); Glide.with(getContext()).load(pImages.get(position)).into(image);

View File

@ -1,14 +1,39 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<ListView <Button
android:layout_width="match_parent" android:id="@+id/buttonClear"
android:layout_height="match_parent" android:layout_width="wrap_content"
android:id="@+id/listView"> android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text="Clear All"
android:textSize="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ListView> <Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginTop="10dp"
android:text="Share"
android:textSize="10dp"
app:layout_constraintStart_toEndOf="@+id/buttonClear"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="580dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>