In: Computer Science
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used by the customers to book cinema tickets. Only users of 15 years old and above are allowed to book cinema tickets for a particular film. The user can book more than one ticket where ticket prices vary between 20 and 50 AED. Your app contains tree Activities. The first activity is a launching activity containing a logo and a button start. When the user press the “Start” button the second activity will appear. The second activity is composed of three Editexts and a button; the name of the user, the user age, and the movie title the button “Reserve” is used to move to the third activity. In the third activity contains a TextView displaying the user name and the movie title, two EditTexts : Price of a ticket and the number of tickets, a button “Calculate Total” used to calculate and to confirm the reservation. First activity: 1. There should be an ImageView to display the logo of the app (Choose any icon). 2. There should be a button “Start”, when the user press this button he should be able to move to the second activity. Second activity: 1. There should be an Editext field for user to enter his/her name; this Editext must not be left empty. 2. There should be an Editext field for user to enter a valid value of his/her age. Appropriate keyboard must be displayed accordingly for the user input. The user age must be 15 and above. The Editext field relative to the user age must not be left empty. 3. There should be an Editext field for user to enter the movie title. For example Avatar, Matrix, Batman, etc. The EdiText field relative to the movie title must not be left empty. 4. There should be a button field “Reserve” for user to move to the third activity. User should be able to pass the user name and the movie title from the second activity to the third activity, which will be displayed as a textbox on the top of the third activity. Third activity: 5. There should be an EditText field for user to enter the price of a single ticket. Ticket prices vary between 20 and 50 AED, this Editext field must not be left empty. 6. User should be able to enter in an EditText how many tickets he/she is booking. Appropriate keyboard must be displayed accordingly for the user input. The user can reserve minimum 1 and maximum 10 tickets at a time.
(First Activity)
Step 1- Add image and a start button to first activity
Step 2- Add functionality to the start button (move to next activity on clicking)
(Second Activity)
Step 3- Add edit texts to take input and a button to proceed to next activity
Step 4- Add checks to the edit text fields and add functionality to reserve button to move to next activity. Also, save the data of edit texts to display them in next activity.
(Third Activity)
Step 5- Add the required text views and edit texts and apply checks on them. Lastly add functionality to calculate button
Here is the complete code with comments for this task-
MainActivity.xml
<LinearLayout>
....
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/logo"/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:backgroundTint="@color/teal_200" />
</LinearLayout>
SecondActivity.xml
<LinearLayout
...
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:layout_gravity="center"
android:layout_marginTop="50dp"
/>
<EditText
android:id="@+id/et_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:inputType="number"/>
<EditText
android:id="@+id/et_movie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Movie Title"
android:layout_gravity="center"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/reserve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/teal_200"
android:layout_gravity="center"
android:text="RESERVE"
android:layout_marginTop="100dp" />
</LinearLayout>
ThirdActivity.xml
<LinearLayout
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:textSize="20dp"
android:text="Name: "
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:textSize="20dp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:textSize="20dp"
android:text="Movie: "
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_movie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:textSize="20dp"
android:textColor="@color/black"/>
</LinearLayout>
<EditText
android:id="@+id/et_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Enter price of one ticket"
android:inputType="number"
android:layout_margin="20dp"/>
<EditText
android:id="@+id/et_tickets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Enter number of tickets to book"
android:inputType="number"
android:layout_margin="20dp"/>
<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/teal_200"
android:text="Calculate Total Amount"
android:layout_gravity="center"
android:layout_marginTop="100dp"/>
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Click calculate to view total price here"/>
</LinearLayout>
MainActivity.java
package com.example.cinema;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//on click of button, user goes to second activity
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
}
SecondActivity.java
package com.example.cinema;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SecondActivity extends AppCompatActivity {
EditText name, age, movie;
Button reserve;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
name=findViewById(R.id.et_name);
age=findViewById(R.id.et_age);
movie=findViewById(R.id.et_movie);
reserve=findViewById(R.id.reserve);
reserve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strName=name.getText().toString();
String strAge=age.getText().toString();
String strMovie=movie.getText().toString();
if(strName.equalsIgnoreCase(""))
name.setError("please enter username");//it gives user error msg
else if(strAge.equals(""))
age.setError("please enter age");//it gives user error msg
if(strMovie.equalsIgnoreCase(""))
movie.setError("please enter movie");//it gives user error msg
if(Integer.parseInt(strAge)<15)
age.setError("age should be 15 or above");
else{
String passname = name.getText().toString().trim();
String passmovie = movie.getText().toString().trim();
Bundle bundle = new Bundle();
bundle.putString("name", passname);
bundle.putString("age", passmovie);
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
ThirdActivity.java
package com.example.cinema;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ThirdActivity extends AppCompatActivity {
TextView name, movie;
EditText price, tickets;
Button calculate;
TextView total;
int tot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
price=findViewById(R.id.et_price);
tickets=findViewById(R.id.et_tickets);
calculate=findViewById(R.id.calculate);
total=findViewById(R.id.total);
Bundle bundle = getIntent().getExtras();
if (bundle!= null) {
String name = bundle.getString("name");
String age = bundle.getString("age");
TextView tvName = findViewById(R.id.tv_name);
TextView tvMovie = findViewById(R.id.tv_movie);
tvName.setText(name);
tvMovie.setText(age);
}
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String p=price.getText().toString();
String t=tickets.getText().toString();
int pr=Integer.parseInt(p);
int ti=Integer.parseInt(t);
Log.d("TAG",p+" "+t+" "+pr+" "+ti+" ");
if(p.equals(""))
price.setError("can't be empty");
if(t.equals(""))
tickets.setError("can't be empty");
if(pr<20||pr>50)
price.setError("price should be entered between 20 and 50 EAD");
if((ti>10))
tickets.setError("Can't book more than 10 tickets");
else {
tot = pr * ti;
total.setText(Integer.toString(tot));
}
}
});
}
}
I hope the above code fulfills all the requirements. If something is missing, please let me know in comments. I would solve the queries too.