DEV Community

Varun Vikyat b
Varun Vikyat b

Posted on

Restaurant app(Spinner)(Java & XML)

MainActivity.java

package com.example.restaurantreservation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText guestCount;
private RadioButton indoor, outdoor;
private Button nextButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    guestCount = findViewById(R.id.guestCount);
    indoor = findViewById(R.id.indoor);
    outdoor = findViewById(R.id.outdoor);
    nextButton = findViewById(R.id.nextButton);

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            proceedToMenu();
        }
    });
}

private void proceedToMenu() {
    String guests = guestCount.getText().toString().trim();
    if (guests.isEmpty()) {
        Toast.makeText(this, "Enter number of guests", Toast.LENGTH_SHORT).show();
        return;
    }

    int numGuests = Integer.parseInt(guests);
    if (numGuests <= 0) {
        Toast.makeText(this, "Invalid number of guests", Toast.LENGTH_SHORT).show();
        return;
    }

    String seatingType;
    int seatingPrice;
    if (indoor.isChecked()) {
        seatingType = "Indoor";
        seatingPrice = 200;
    } else if (outdoor.isChecked()) {
        seatingType = "Outdoor";
        seatingPrice = 150;
    } else {
        Toast.makeText(this, "Select seating type", Toast.LENGTH_SHORT).show();
        return;
    }

    Intent intent = new Intent(MainActivity.this, MenuActivity.class);
    intent.putExtra("numGuests", numGuests);
    intent.putExtra("seatingType", seatingType);
    intent.putExtra("seatingPrice", seatingPrice);
    startActivity(intent);
}
Enter fullscreen mode Exit fullscreen mode

}

MainActivity (XML)

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<EditText
    android:id="@+id/guestCount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter number of guests"
    android:inputType="number" />

<RadioButton
    android:id="@+id/indoor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Indoor (₹200 per guest)" />

<RadioButton
    android:id="@+id/outdoor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Outdoor (₹150 per guest)" />

<Button
    android:id="@+id/nextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Next" />
Enter fullscreen mode Exit fullscreen mode

MenuActivity.java

package com.example.restaurantreservation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class MenuActivity extends AppCompatActivity {

private CheckBox pizza, burger, pasta;
private Button nextButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    pizza = findViewById(R.id.pizza);
    burger = findViewById(R.id.burger);
    pasta = findViewById(R.id.pasta);
    nextButton = findViewById(R.id.nextButton);

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            proceedToSummary();
        }
    });
}

private void proceedToSummary() {
    ArrayList<String> selectedItems = new ArrayList<>();
    int foodPrice = 0;

    if (pizza.isChecked()) {
        selectedItems.add("Pizza (300 rupees)");
        foodPrice += 300;
    }
    if (burger.isChecked()) {
        selectedItems.add("Burger (200 rupees)");
        foodPrice += 200;
    }
    if (pasta.isChecked()) {
        selectedItems.add("Pasta (250 rupees)");
        foodPrice += 250;
    }

    // Retrieve data from previous activity
    Intent previousIntent = getIntent();
    int numGuests = previousIntent.getIntExtra("numGuests", 0);
    String seatingType = previousIntent.getStringExtra("seatingType");
    int seatingPrice = previousIntent.getIntExtra("seatingPrice", 0);

    // Send all data to SummaryActivity
    Intent intent = new Intent(MenuActivity.this, SummaryActivity.class);
    intent.putExtra("numGuests", numGuests);
    intent.putExtra("seatingType", seatingType);
    intent.putExtra("seatingPrice", seatingPrice);
    intent.putStringArrayListExtra("selectedItems", selectedItems);
    intent.putExtra("foodPrice", foodPrice);
    startActivity(intent);
}
Enter fullscreen mode Exit fullscreen mode

}

MenuActivity (XML)

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<CheckBox
    android:id="@+id/pizza"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pizza (₹300)" />

<CheckBox
    android:id="@+id/burger"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Burger (₹200)" />

<CheckBox
    android:id="@+id/pasta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pasta (₹250)" />

<Button
    android:id="@+id/nextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Next" />
Enter fullscreen mode Exit fullscreen mode

SummaryActivity.java

package com.example.restaurantreservation;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class SummaryActivity extends AppCompatActivity {

private TextView summaryText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_summary);

    summaryText = findViewById(R.id.summaryText);

    // Retrieve all passed data
    int numGuests = getIntent().getIntExtra("numGuests", 0);
    String seatingType = getIntent().getStringExtra("seatingType");
    int seatingPrice = getIntent().getIntExtra("seatingPrice", 0);
    ArrayList<String> foodItems = getIntent().getStringArrayListExtra("selectedItems");
    int foodPrice = getIntent().getIntExtra("foodPrice", 0);

    // Calculate total bill
    int totalBill = (numGuests * seatingPrice) + foodPrice;

    // Display summary
    String summary = "Guests: " + numGuests + "\n" +
                     "Seating Type: " + (seatingType != null ? seatingType : "Not Selected") + "\n" +
                     "Food Order: " + (foodItems != null && !foodItems.isEmpty() ? foodItems : "None") + "\n" +
                     "Total Bill: ₹" + totalBill;

    summaryText.setText(summary);
}
Enter fullscreen mode Exit fullscreen mode

}

SummaryActivity (XML)

<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SummaryActivity">

<TextView
    android:id="@+id/summaryText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp" />
Enter fullscreen mode Exit fullscreen mode

Top comments (0)