Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.atnimak.coffeeorder;
- 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 application makes a coffee order and sends it by email
- */
- public class MainActivity extends AppCompatActivity {
- //quantity of cups of coffee
- int quantity = 0;
- //price of one cup of coffee
- int price = 5;
- //true if ORDER button was clicked
- boolean orderChecked = false;
- //customer`s name
- String name = "";
- //total order to display and send by e-mail
- String totalOrder = "";
- @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) {
- name = "";
- totalOrder = "";
- if (configName().trim().equals("")) {
- Toast.makeText(getApplicationContext(), getString(R.string.toast_havetoWriteAname), Toast.LENGTH_SHORT).show();
- } else {
- int priceWithToppings = price;
- name = configName();
- totalOrder = getString(R.string.name, name);
- totalOrder += getString(R.string.quantity_sum, quantity);
- String toppings = "";
- if (isWhippedCream()) {
- toppings += getString(R.string.whipped_cream_sum);
- priceWithToppings++;
- }
- if (isChokolate()) {
- if (isWhippedCream()) {
- toppings += getString(R.string.andChocolate_sum);
- priceWithToppings += 2;
- } else {
- toppings += getString(R.string.chocolate_sum);
- priceWithToppings += 2;
- }
- }
- if (!isChokolate() && !isWhippedCream()) {
- toppings = getString(R.string.notoppings);
- }
- // totalOrder += getString(R.string.total_sum, quantity * priceWithToppings);
- totalOrder += getString(R.string.total_sum, NumberFormat.getCurrencyInstance().format(quantity * priceWithToppings));
- totalOrder += getString(R.string.toppings_sum, toppings);
- totalOrder += getString(R.string.thankU);
- /*String[] adresses = {getString(R.string.orderEmail)};
- composeEmail(adresses, getString(R.string.justCoffeeFor) + name, totalOrder);*/
- displayPrice(totalOrder);
- orderChecked = true;
- }
- }
- /**
- * This method displays the given quantity value on the screen.
- */
- private void display(int number) {
- TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
- quantityTextView.setText("" + number);
- }
- /**
- * This method decrements quantity of cups of coffee
- */
- public void decrementCoffee(View view) {
- if (quantity > 1) {
- quantity--;
- } else {
- Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.toast_lessOne), Toast.LENGTH_SHORT);
- toast.show();
- }
- display(quantity);
- }
- /**
- * This method displays total price
- */
- private void displayPrice(String message) {
- TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
- priceTextView.setText(message);
- }
- /**
- * This method increments quantity of cups of coffee
- */
- public void incrementCoffee(View view) {
- if (quantity < 100) {
- quantity++;
- } else {
- Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.toast_more100), Toast.LENGTH_SHORT);
- toast.show();
- }
- display(quantity);
- }
- /**
- * This method checks was the whipped cream selected or not.
- */
- public boolean isWhippedCream() {
- CheckBox checkBox = findViewById(R.id.whippedCream);
- return checkBox.isChecked();
- }
- /**
- * This method checks was the chocolate selected or not.
- */
- public boolean isChokolate() {
- CheckBox checkBox = findViewById(R.id.chocolate);
- return checkBox.isChecked();
- }
- /**
- * This method get the customer`s name
- */
- public String configName() {
- EditText editText = (EditText) findViewById(R.id.editName);
- String name = editText.getText().toString();
- return name;
- }
- /**
- * This method send the e-mail intent
- */
- public void composeEmail(String[] addresses, String subject, String message) {
- Intent intent = new Intent(Intent.ACTION_SENDTO);
- intent.setData(Uri.parse("mailto:")); // only email apps should handle this
- intent.putExtra(Intent.EXTRA_EMAIL, addresses);
- intent.putExtra(Intent.EXTRA_SUBJECT, subject);
- intent.putExtra(Intent.EXTRA_TEXT, message);
- if (intent.resolveActivity(getPackageManager()) != null) {
- startActivity(intent);
- }
- }
- /**
- * This method send totel order by e-mail
- */
- public void sendOrder(View view) {
- if (orderChecked) {
- orderChecked = false;
- String[] adresses = {getString(R.string.orderEmail)};
- composeEmail(adresses, getString(R.string.justCoffeeFor) + name, totalOrder);
- name = "";
- totalOrder = "";
- } else {
- Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.shoudOrder), Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- }
Add Comment
Please, Sign In to add comment