Created
January 31, 2018 16:24
-
-
Save Tarek-Bohdima/56ad228ae701190de0a540da260e9e52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.view.View; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
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 plus button is clicked. | |
*/ | |
public void increment(View view) { | |
if (quantity >= 100) { | |
//show error message as a toast | |
Toast.makeText(this, getText(R.string.too_many_coffees), Toast.LENGTH_SHORT).show(); | |
//exit method early as there is nothing to do | |
return; | |
} | |
quantity += 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method is called when the minus button is clicked. | |
*/ | |
public void decrement(View view) { | |
if (quantity == 1) { | |
//show error message as a toast | |
Toast.makeText(this, getText(R.string.too_few_coffees), Toast.LENGTH_SHORT).show(); | |
//exit method early as there is nothing to do | |
return; | |
} | |
quantity -= 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
//find and store the name of the user | |
EditText nameField = findViewById(R.id.name_field); | |
String name = nameField.getText().toString(); | |
//add whipped cream to the coffee order | |
CheckBox whippedCreamCheckBox = findViewById(R.id.whipped_cream); | |
boolean hasWhippedCream = whippedCreamCheckBox.isChecked(); | |
//add chocolate topping to the coffee order | |
CheckBox chocolateCheckBox = findViewById(R.id.chocolate); | |
boolean hasChocolate = chocolateCheckBox.isChecked(); | |
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, "Just Java order for : " + name); | |
intent.putExtra(Intent.EXTRA_TEXT, priceMessage); | |
if (intent.resolveActivity(getPackageManager()) != null) { | |
startActivity(intent); | |
} | |
// displayMessage(priceMessage); | |
} | |
/** | |
* Calculates the price of the order. | |
* | |
* @param addWhippedCream is whether the user wants to add whipped cream to coffee | |
* @param addChocolate is whether the user wants to add chocolate topping to coffee | |
* @return the total price. | |
*/ | |
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) { | |
//price of one cup of coffee | |
int basePrice = 5; | |
//adds $1 if user wants whipped cream | |
if (addWhippedCream) { | |
basePrice += 1; | |
} | |
//adds $2 if user wants chocolate topping | |
if (addChocolate) { | |
basePrice += 2; | |
} | |
//Calculate the total order price by multiplying basePrice by quantity | |
return quantity * basePrice; | |
} | |
/** | |
* Create summary of the order. | |
* | |
* @param name of the user | |
* @param price of the order | |
* @param addWhippedCream is whether or not the user wants whipped cream topping | |
* @param addChocolate is whether or not the user wants chocolate topping | |
* @return text summary | |
*/ | |
private String createOrderSummary(String name, int price, boolean addWhippedCream, boolean addChocolate) { | |
String priceMessage = getString(R.string.order_summary_name, name); | |
priceMessage += "\n" + getString(R.string.order_summary_whipped_cream, addWhippedCream); | |
priceMessage += "\n" + getString(R.string.order_summary_chocolate, addChocolate); | |
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity); | |
priceMessage += "\n" + getString(R.string.order_summary_price, NumberFormat.getCurrencyInstance().format(price)); | |
priceMessage += "\n" + getString(R.string.thank_you); | |
return priceMessage; | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
*/ | |
private void displayQuantity(int number) { | |
TextView quantityTextView = findViewById(R.id.quantity_text_view); | |
quantityTextView.setText("" + number); | |
} | |
// /** | |
// * This method displays the given text on the screen. | |
// */ | |
// private void displayMessage(String message) { | |
// TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view); | |
// orderSummaryTextView.setText(message); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment