package [Link].
justjava;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int Quantity=2;
public boolean hasWhippedCream;
public boolean hasChocolate;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String Name;
EditText nameField=(EditText)findViewById([Link].name_field);
Name=[Link]().toString();
CheckBox
whippedCreamCheckedBox=(CheckBox)findViewById([Link].whipped_cream_checkbox);
hasWhippedCream=[Link]();
CheckBox chocolateCheckBox=(CheckBox)findViewById([Link].chocolate_checkbox);
hasChocolate=[Link]();
int price=calculatePrice(hasWhippedCream,hasChocolate);
String mail="Name: "+Name+"\nAdd Whipped Cream? "+hasWhippedCream+"\n Add
Chocolate? "+hasChocolate+"\nQuantity: "+Quantity+"\nTotal: $"+price+"\nThank
You!!";
Intent intent = new Intent(Intent.ACTION_SENDTO);
/* only email apps should handle this */
[Link]([Link]("[Link]
[Link](Intent.EXTRA_TEXT,mail);
[Link](Intent.EXTRA_EMAIL,"abhishek22.2323@[Link]");
[Link](Intent.EXTRA_SUBJECT,"Coffee Order For "+Name);
if ([Link](getPackageManager()) != null) {
startActivity(intent);
}
}
public int calculatePrice(boolean hasWhippedCream, boolean hasChocolate)
{
int basePrice=5;
//add 1$ for whipped cream
if(hasWhippedCream)
basePrice=basePrice+1;
//add 2$ for choclate topping
if(hasChocolate)
basePrice=basePrice+2;
//return the total price
return (basePrice*Quantity);
public void increment(View view)
{
if(Quantity==100)
{
Context context=getApplicationContext();
int duration=Toast.LENGTH_SHORT;
Toast toast=[Link](context,"Please Enter Value Below
100",duration);
[Link]();
return;
}
Quantity++;
displayQuantity(Quantity);
}
public void decrement(View view)
{
if(Quantity==1)
{
Context context=getApplicationContext();
int duration=Toast.LENGTH_SHORT;
Toast toast=[Link](context,"Please Enter Value Above
0",duration);
[Link]();
return;
}
Quantity--;
displayQuantity(Quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(
[Link].quantity_text_view);
[Link]("" + number);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="[Link]">
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapWords"
/>
<TextView
android:text="toppings"
style="@style/HeaderTextStyle" />
<CheckBox
android:id="@+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped Cream"
android:paddingLeft="24dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/chocolate_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chocolate"
android:paddingLeft="24dp"
android:textSize="16sp" />
<TextView
android:text="Quantity"
style="@style/HeaderTextStyle" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="47dp"
android:layout_height="47dp"
android:layout_marginTop="16dp"
android:onClick="decrement"
android:paddingRight="8dp"
android:text="-" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="2"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:layout_width="47dp"
android:layout_height="47dp"
android:layout_marginTop="16dp"
android:onClick="increment"
android:paddingLeft="8dp"
android:text="+" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="ORDER" />
</LinearLayout>
</ScrollView>