0% found this document useful (0 votes)
32 views

Package Import Import Import Import Import Import Import Import Import Import Import Import Import

This document contains code for an Android coffee ordering app. It defines a MainActivity class that handles the order form UI and logic. The app displays name, topping, and quantity fields. It calculates the order total and sends an email when submitted. Methods increment/decrement the quantity and calculate prices based on selections.

Uploaded by

Joey Tribbiani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Package Import Import Import Import Import Import Import Import Import Import Import Import Import

This document contains code for an Android coffee ordering app. It defines a MainActivity class that handles the order form UI and logic. The app displays name, topping, and quantity fields. It calculates the order total and sends an email when submitted. Methods increment/decrement the quantity and calculate prices based on selections.

Uploaded by

Joey Tribbiani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.example.android.

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);

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 */
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_TEXT,mail);
intent.putExtra(Intent.EXTRA_EMAIL,"[email protected]");
intent.putExtra(Intent.EXTRA_SUBJECT,"Coffee Order For "+Name);

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);

public void increment(View view)


{

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

<?xml version="1.0" encoding="utf-8"?>


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.android.justjava.MainActivity">

<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>

You might also like