Professional Documents
Culture Documents
Package Import Import Import Import Import Import Import Import Import Import Import Import Import
Package Import Import Import Import Import Import Import Import Import Import Import Import Import
justjava;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.*;
import java.util.Locale;
/**
* 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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String Name;
EditText nameField=(EditText)findViewById(R.id.name_field);
Name=nameField.getText().toString();
CheckBox
whippedCreamCheckedBox=(CheckBox)findViewById(R.id.whipped_cream_checkbox);
hasWhippedCream=whippedCreamCheckedBox.isChecked();
CheckBox chocolateCheckBox=(CheckBox)findViewById(R.id.chocolate_checkbox);
hasChocolate=chocolateCheckBox.isChecked();
int price=calculatePrice(hasWhippedCream,hasChocolate);
if (intent.resolveActivity(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);
if(Quantity==100)
{
Context context=getApplicationContext();
int duration=Toast.LENGTH_SHORT;
Toast toast=Toast.makeText(context,"Please Enter Value Below
100",duration);
toast.show();
return;
}
Quantity++;
displayQuantity(Quantity);
}
public void decrement(View view)
{
if(Quantity==1)
{
Context context=getApplicationContext();
int duration=Toast.LENGTH_SHORT;
Toast toast=Toast.makeText(context,"Please Enter Value Above
0",duration);
toast.show();
return;
}
Quantity--;
displayQuantity(Quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}
XML
<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>