PizzeriaApp / app / src / main / java / com / example / pizzeria / ui / CurrentOrderActivity.java
CurrentOrderActivity.java
Raw
package com.example.pizzeria.ui;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import com.example.pizzeria.MainActivity;
import com.example.pizzeria.R;
import com.example.pizzeria.pizzeria.*;

import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 This class stores information used for the current order view.
 @author Andy Xu, Jeffrey Zhang
 */
public class CurrentOrderActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private CurrentOrderActivity current;
    private Order currentOrder;
    private ListView pizzaList;
    private ArrayAdapter<Pizza> adapter;
    private static final int INVALID_INDEX = -1;
    private int currSelectionIndex = -1;

    /**
     This method contains the necessary setup when this activity is created.
     @param savedInstanceState the saved state.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_order);
        this.current = this;
        this.currentOrder = MainActivity.currentOrder;
        this.pizzaList = findViewById(R.id.co_pizza_listview);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, currentOrder.getPizzas());
        pizzaList.setAdapter(adapter);
        pizzaList.setOnItemClickListener(this);
        buttonSetup();
        changePrices();
    }

    /**
     This method keeps track of the last selected item in the order.
     @param parent the AdapterView for the items.
     @param view the current view.
     @param position the position of the element.
     @param id the id of the item.
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        currSelectionIndex = position;
    }

    /**
     This method prepares the various buttons.
     */
    private void buttonSetup() {
        removeButtonSetup();
        placeOrderButtonSetup();
        clearOrderButtonSetup();
    }

    /**
     This method prepares the remove button.
     */
    private void removeButtonSetup() {
        Button removeButton = findViewById(R.id.co_remove_button);
        removeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currSelectionIndex != INVALID_INDEX) {
                    currentOrder.remove(currentOrder.getPizzas().get(currSelectionIndex));
                    currSelectionIndex = INVALID_INDEX;
                    pizzaList.setAdapter(adapter);
                    changePrices();
                    Context ctx = getApplicationContext();
                    String message = "Pizza Removed!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(ctx, message, duration);
                    toast.show();
                }
            }
        });
    }

    /**
     This method prepares the place order button.
     */
    private void placeOrderButtonSetup() {
        Button placeOrder = findViewById(R.id.co_place_order);
        placeOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentOrder.getPizzas().size() > 0) {
                    MainActivity.placeOrder();
                    currentOrder = MainActivity.currentOrder;
                    adapter = new ArrayAdapter<>(current, android.R.layout.simple_list_item_1, currentOrder.getPizzas());
                    pizzaList.setAdapter(adapter);
                    changePrices();
                    Context ctx = getApplicationContext();
                    String message = "Order Placed!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(ctx, message, duration);
                    toast.show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(CurrentOrderActivity.this);
                    builder.setTitle("Error!");
                    builder.setMessage("Cannot place order (0 items in order)!");
                    builder.setPositiveButton("Okay", null);
                    AlertDialog result = builder.create();
                    result.show();
                }
            }
        });
    }

    /**
     This method prepares the cancel order button.
     */
    private void clearOrderButtonSetup() {
        Button clearOrder = findViewById(R.id.co_clear_order);
        clearOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentOrder.getPizzas().size() > 0) {
                    currentOrder.clearOrder();
                    adapter.notifyDataSetChanged();
                    changePrices();
                    Context ctx = getApplicationContext();
                    String message = "Order Cleared!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(ctx, message, duration);
                    toast.show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(CurrentOrderActivity.this);
                    builder.setTitle("Error!");
                    builder.setMessage("Order is already empty!");
                    builder.setPositiveButton("Okay", null);
                    AlertDialog result = builder.create();
                    result.show();
                }
            }
        });
    }


    /**
     This method updates the price information on the current order.
     */
    public void changePrices() {
        TextView orderNumber = findViewById(R.id.co_order_num);
        TextView subtotal = findViewById(R.id.co_subtotal);
        TextView taxes = findViewById(R.id.co_tax);
        TextView total = findViewById(R.id.co_total);
        orderNumber.setText(String.format("Order Number: %s", currentOrder.getID()));
        subtotal.setText(String.format("Subtotal: $%s", MainActivity.format.format(currentOrder.getSubtotal())));
        taxes.setText(String.format("Sales Tax: $%s", MainActivity.format.format(currentOrder.getTax())));
        total.setText(String.format("Order Total: $%s", MainActivity.format.format(currentOrder.getTotalPrice())));
    }
}