/*
========================================================================================
Filename: 23_FoodOrderApp.txt
Program 23: Develop an Android application in Java that enables customers to place a food order, view the order summary, and generate the final bill using three activities.
Files Included: MainActivity.java, OrderSummaryActivity.java, FinalInvoiceActivity.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   Activity 1 (activity_main.xml):
     - 2 EditText (TextBox): etCustomerName, etMobileNumber
     - 1 RadioGroup (rgMealType) with 2 RadioButtons: rbVeg, rbNonVeg
     - 4 CheckBox: cbPizza (₹250), cbBurger (₹150), cbSandwich (₹120), cbSoftDrink (₹60)
     - 1 Button: btnPlaceOrder ('Place Order')
     - 3 TextView labels
   Activity 2 (activity_order_summary.xml):
     - 5 TextViews (Name, Mobile, Meal Type, Items, Total Bill)
     - 1 Button: btnGenerateInvoice ('Generate Invoice')
   Activity 3 (activity_final_invoice.xml):
     - 8 TextViews showing invoice breakdown, discount, final amount

2. Layout Configuration:
   - LinearLayout / ScrollView

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Use NoActionBar theme (e.g. Theme.MaterialComponents.DayNight.NoActionBar in res/values/themes.xml).

5. AndroidManifest.xml:
   Register 2 additional activities:
   <activity android:name=".OrderSummaryActivity" android:exported="false" />
   <activity android:name=".FinalInvoiceActivity" android:exported="false" />

6. Additional Files & Resources Required:
   - Create OrderSummaryActivity.java and FinalInvoiceActivity.java.
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.foodorderapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    private EditText etCustomerName, etMobileNumber;
    private RadioGroup rgMealType;
    private CheckBox cbPizza, cbBurger, cbSandwich, cbSoftDrink;
    private Button btnPlaceOrder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etCustomerName = findViewById(R.id.etCustomerName);
        etMobileNumber = findViewById(R.id.etMobileNumber);
        rgMealType = findViewById(R.id.rgMealType);
        cbPizza = findViewById(R.id.cbPizza);
        cbBurger = findViewById(R.id.cbBurger);
        cbSandwich = findViewById(R.id.cbSandwich);
        cbSoftDrink = findViewById(R.id.cbSoftDrink);
        btnPlaceOrder = findViewById(R.id.btnPlaceOrder);
        btnPlaceOrder.setOnClickListener(v -> validateAndSubmitOrder());
    }
    private void validateAndSubmitOrder() {
        String name = etCustomerName.getText().toString().trim();
        String mobile = etMobileNumber.getText().toString().trim();
        if (name.isEmpty()) {
            etCustomerName.setError("Customer name cannot be empty");
            etCustomerName.requestFocus();
            return;
        }
        if (mobile.length() != 10 || !mobile.matches("\\d{10}")) {
            etMobileNumber.setError("Mobile number must be exactly 10 digits");
            etMobileNumber.requestFocus();
            return;
        }
        int selectedMealId = rgMealType.getCheckedRadioButtonId();
        if (selectedMealId == -1) {
            Toast.makeText(this, "Please select a meal type", Toast.LENGTH_SHORT).show();
            return;
        }
        RadioButton selectedRadioButton = findViewById(selectedMealId);
        String mealType = selectedRadioButton.getText().toString();
        ArrayList<String> selectedItems = new ArrayList<>();
        double totalBill = 0.0;
        if (cbPizza.isChecked()) {
            selectedItems.add("Pizza (₹250)");
            totalBill += 250;
        }
        if (cbBurger.isChecked()) {
            selectedItems.add("Burger (₹150)");
            totalBill += 150;
        }
        if (cbSandwich.isChecked()) {
            selectedItems.add("Sandwich (₹120)");
            totalBill += 120;
        }
        if (cbSoftDrink.isChecked()) {
            selectedItems.add("Soft Drink (₹60)");
            totalBill += 60;
        }
        if (selectedItems.isEmpty()) {
            Toast.makeText(this, "Please select at least one food item", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(MainActivity.this, OrderSummaryActivity.class);
        intent.putExtra("CUSTOMER_NAME", name);
        intent.putExtra("MOBILE_NUMBER", mobile);
        intent.putExtra("MEAL_TYPE", mealType);
        intent.putStringArrayListExtra("SELECTED_ITEMS", selectedItems);
        intent.putExtra("TOTAL_BILL", totalBill);
        startActivity(intent);
    }
}

// ======================================================================================
// File: OrderSummaryActivity.java
// ======================================================================================
package com.example.foodorderapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class OrderSummaryActivity extends AppCompatActivity {
    private TextView tvName, tvMobile, tvMealType, tvItems, tvTotalBill;
    private Button btnGenerateInvoice;
    private String name, mobile, mealType;
    private ArrayList<String> items;
    private double totalBill;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_summary);
        tvName = findViewById(R.id.tvSummaryName);
        tvMobile = findViewById(R.id.tvSummaryMobile);
        tvMealType = findViewById(R.id.tvSummaryMealType);
        tvItems = findViewById(R.id.tvSummaryItems);
        tvTotalBill = findViewById(R.id.tvTotalBill);
        btnGenerateInvoice = findViewById(R.id.btnGenerateInvoice);
        Intent intent = getIntent();
        if (intent != null) {
            name = intent.getStringExtra("CUSTOMER_NAME");
            mobile = intent.getStringExtra("MOBILE_NUMBER");
            mealType = intent.getStringExtra("MEAL_TYPE");
            items = intent.getStringArrayListExtra("SELECTED_ITEMS");
            totalBill = intent.getDoubleExtra("TOTAL_BILL", 0.0);
            tvName.setText("Name: " + name);
            tvMobile.setText("Mobile: " + mobile);
            tvMealType.setText("Meal Type: " + mealType);
            StringBuilder itemsFormatted = new StringBuilder("Selected Items:\n");
            if (items != null) {
                for (String item : items) {
                    itemsFormatted.append(" • ").append(item).append("\n");
                }
            }
            tvItems.setText(itemsFormatted.toString().trim());
            tvTotalBill.setText(String.format("Total Bill Amount: ₹%.2f", totalBill));
        }
        btnGenerateInvoice.setOnClickListener(v -> {
            Intent invoiceIntent = new Intent(OrderSummaryActivity.this, FinalInvoiceActivity.class);
            invoiceIntent.putExtra("CUSTOMER_NAME", name);
            invoiceIntent.putExtra("MOBILE_NUMBER", mobile);
            invoiceIntent.putExtra("MEAL_TYPE", mealType);
            invoiceIntent.putStringArrayListExtra("SELECTED_ITEMS", items);
            invoiceIntent.putExtra("TOTAL_BILL", totalBill);
            startActivity(invoiceIntent);
        });
    }
}

// ======================================================================================
// File: FinalInvoiceActivity.java
// ======================================================================================
package com.example.foodorderapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class FinalInvoiceActivity extends AppCompatActivity {
    private TextView tvName, tvMobile, tvMealType, tvItems;
    private TextView tvSubtotal, tvDiscount, tvFinalAmount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final_invoice);
        tvName = findViewById(R.id.tvInvoiceName);
        tvMobile = findViewById(R.id.tvInvoiceMobile);
        tvMealType = findViewById(R.id.tvInvoiceMealType);
        tvItems = findViewById(R.id.tvInvoiceItems);
        tvSubtotal = findViewById(R.id.tvInvoiceSubtotal);
        tvDiscount = findViewById(R.id.tvInvoiceDiscount);
        tvFinalAmount = findViewById(R.id.tvInvoiceFinalAmount);
        Intent intent = getIntent();
        if (intent != null) {
            String name = intent.getStringExtra("CUSTOMER_NAME");
            String mobile = intent.getStringExtra("MOBILE_NUMBER");
            String mealType = intent.getStringExtra("MEAL_TYPE");
            ArrayList<String> items = intent.getStringArrayListExtra("SELECTED_ITEMS");
            double totalBill = intent.getDoubleExtra("TOTAL_BILL", 0.0);
            tvName.setText("Customer Name: " + name);
            tvMobile.setText("Mobile: " + mobile);
            tvMealType.setText("Meal Type: " + mealType);
            StringBuilder itemsFormatted = new StringBuilder("Ordered Items:\n");
            if (items != null) {
                for (String item : items) {
                    itemsFormatted.append(" • ").append(item).append("\n");
                }
            }
            tvItems.setText(itemsFormatted.toString().trim());
            double discountRate = 0.0;
            if (totalBill > 800) {
                discountRate = 0.15;
            } else if (totalBill >= 500) {
                discountRate = 0.10;
            } else {
                discountRate = 0.00;
            }
            double discountAmount = totalBill * discountRate;
            double finalAmount = totalBill - discountAmount;
            tvSubtotal.setText(String.format("Subtotal: ₹%.2f", totalBill));
            tvDiscount.setText(String.format("Discount (%.0f%%): -₹%.2f", discountRate * 100, discountAmount));
            tvFinalAmount.setText(String.format("Final Amount Payable: ₹%.2f", finalAmount));
        }
    }
}
