Skip to content

Instantly share code, notes, and snippets.

@abdahma01
Last active March 4, 2018 00:36
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 abdahma01/720283e1d2c23a624db910e9990aced1 to your computer and use it in GitHub Desktop.
Save abdahma01/720283e1d2c23a624db910e9990aced1 to your computer and use it in GitHub Desktop.
package com.example.android.coffeeupdated;
import android.content.Intent;
import android.icu.text.NumberFormat;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int quantity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox)findViewById(R.id.chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
EditText text = (EditText)findViewById(R.id.name_field);
String name = text.getText().toString();
int price = calculatePrice(hasWhippedCream, hasChocolate);
String priceMessage = createOrderSummary(name, price, hasWhippedCream, hasChocolate);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.Coffee_Updated_order_for, name));
intent.putExtra((Intent.EXTRA_TEXT), priceMessage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
public void increment(View view) {
if (quantity == 10){
Toast.makeText(this, getString(R.string.toastIncrease), Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
public void decrement(View view) {
if (quantity == 1){
Toast.makeText(this, getString(R.string.toastDecrease), Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
int coffeeCupPrice = 5;
if (addWhippedCream){
coffeeCupPrice += 1;
}
if(addChocolate){
coffeeCupPrice += 2;
}
return coffeeCupPrice * quantity;
}
private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate) {
String orderSummary = getString(R.string.order_summary_name , name);
orderSummary += "\n" + getString(R.string.order_whipped_cream ,(addWhippedCream? getString(R.string.boolean_true) : getString(R.string.boolean_false)));;
orderSummary += "\n" + getString(R.string.order_chocolate , (addChocolate? getString(R.string.boolean_true) : getString(R.string.boolean_false)));;
orderSummary += "\n" + getString(R.string.order_summary_quantity , quantity);
orderSummary += "\n" + getString( R.string.order_price , NumberFormat.getCurrencyInstance().format(price));
orderSummary += "\n" + getString(R.string.thank_you);
return orderSummary;
}
private void displayQuantity(int numbersOfCoffee) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + numbersOfCoffee);
}
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">Coffee Updated</string>
<!--XML-->
<!--Header text for the list of toppings [CHAR LIMIT=20]-->
<string name="name">Name</string>
<string name="toppings">Toppings</string>
<string name="Whipped_cream">Whipped cream</string>
<string name="chocolate">Chocolate</string>
<string name="intial_value">1</string>
<string name="order_quantity">quantity</string>
<string name="order">Order</string>
<!--Java-->
<string name="boolean_true">Yes</string>
<string name="boolean_false">No</string>
<string name="toastIncrease">You cannot have more than 10 coffees </string>
<string name="toastDecrease">You cannot have less than 1 coffee </string>
<string name="thank_you">Thank you!</string>
<string name="order_summary_name">Name: <xliff:g id="name" example="Amy">%s</xliff:g></string>
<string name="order_whipped_cream">Add whipped cream? <xliff:g id="addWhippedCream" example="true">%s</xliff:g></string>
<string name="order_chocolate">Add chocolate? <xliff:g id="addChocolate" example="true">%s</xliff:g></string>
<string name="order_summary_quantity">Quantity: <xliff:g id="quantity" example="2">%s</xliff:g></string>
<string name="order_price">Total: <xliff:g id="total_price" example="$10">%s</xliff:g></string>
<string name="Coffee_Updated_order_for">Coffee Updated order for <xliff:g id="name" example="amy">%s</xliff:g></string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">قهوتك !!</string>
<string name="chocolate">شوكولا</string>
<string name="Whipped_cream">كريمه مخفوقه</string>
<string name="order">أطلب</string>
<string name="order_quantity">عدد الأكواب</string>
<string name="thank_you">شكرا لك!</string>
<string name="toppings">الإضافات</string>
<string name="name">الإسم</string>
<string name="intial_value">1</string>
<string name="toastDecrease">لا يمكنك طلب أقل من كوب !</string>
<string name="toastIncrease">لا يمكنك طلب أكثر من 10 أكواب</string>
<string name="boolean_true">نعم</string>
<string name="boolean_false">لا</string>
<string name="order_summary_name">الإسم: <xliff:g id="name" example="Amy">%s</xliff:g></string>
<string name="order_whipped_cream">هل تم إضافه الكريمه المخفوقه؟ <xliff:g id="addWhippedCream" example="Yes">%s</xliff:g></string>
<string name="order_chocolate">هل تم إضافه شوكولا؟ <xliff:g id="addChocolate" example="No">%s</xliff:g></string>
<string name="order_summary_quantity">عدد الأكواب: <xliff:g id="quantity" example="1">%s</xliff:g></string>
<string name="order_price">الثمن: <xliff:g id="price" example="10 ج.م">%s</xliff:g></string>
<string name="Coffee_Updated_order_for">طلب قهوه مقدم من <xliff:g id="name" example="Amy">%s</xliff:g></string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment