Skip to content

Instantly share code, notes, and snippets.

@BorjaBobby
Last active April 6, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BorjaBobby/42c0c41f79bff882ade5427308e59547 to your computer and use it in GitHub Desktop.
Save BorjaBobby/42c0c41f79bff882ade5427308e59547 to your computer and use it in GitHub Desktop.
Coffee ordering app for Android device
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_just_java"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Topings"
android:textAllCaps="true"
android:textColor="#000000"/>
<CheckBox
android:id="@+id/whipped_cream_checkbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingleft="24dp"
android:text="Whipped cream"
android:textSize="16sp"
/>
<CheckBox
android:id="@+id/chocolate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="Chocolate"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUANTITY"
android:id="@+id/quantity_name"
android:layout_marginBottom="16dp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="48dp"
android:layout_width="48dp"
android:text="+"
android:id="@+id/button3"
android:onClick="increment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quantity_text_view"
android:text="1"
android:textSize="16sp"
android:padding="10dp"
android:layout_below="@id/quantity_name"
android:textColor="#000000"/>
<Button
android:layout_height="48dp"
android:layout_width="48dp"
android:text="-"
android:id="@+id/button4"
android:onClick="decrement"/>
</LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Order"
android:id="@+id/button2"
android:layout_marginTop="16dp"
android:onClick="submitOrder"/>
</LinearLayout>
</ScrollView>
package com.example.android.justjava;
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 com.example.android.justjava.R;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 2;
@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) {
// Find the user's name
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
Log.v("Main activity", "Name: " + name);
// Figure out if the user wants whipped cream topping
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
// Figure out if the user wants chocolate topping
CheckBox chocolate = (CheckBox) findViewById(R.id.chocolate);
boolean hasChocolate = chocolate.isChecked();
int price = calculatePrice(hasWhippedCream, hasChocolate);
String priceMessage = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
// This intent sends the data to an email app. It already provides the receiver, the subject
// and the body of the message.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("message/rfc822");
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Coffee order for " + name);
intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice(boolean hasWhippedCream, boolean hasChocolate) {
//Price of one cup of coffee.
int basePrice = 5;
//Price of one cup of coffee with whipped cream topping + 1 $.
if (hasWhippedCream) {
basePrice = basePrice + 1;
}
//Price of one cup of coffee with chocolate topping + 2 $ .
if (hasChocolate) {
basePrice = basePrice + 2;
}
//calculate the total price by multiplying by quantity.
return quantity * basePrice;
}
/**
* Creates the order summary.
*
* @param name the client
* @param price of the order
* @param hasWhippedCream is whether or not the user wants whipped cream topping
* @return text summary
*/
private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate) {
String priceMessage = "Name: " + name;
priceMessage += "\nAdd whipped cream?" + hasWhippedCream;
priceMessage += "\nAdd chocolate?" + hasChocolate;
priceMessage = priceMessage + "\nQuantity: " + quantity;
priceMessage = priceMessage + "\nTotal: £" + price;
priceMessage = priceMessage + "\nThank you!";
return priceMessage;
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
quantity = quantity - 1;
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment