Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Appli commande de café
/**
* IMPORTANT: Make sure you are using the correct package name.
* This example uses the package name:
* package com.example.android.justjava
* If you get an error when copying this code into Android studio, update it to match teh package name found
* in the project's AndroidManifest.xml file.
**/
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 android.widget.Toast;
import java.text.NumberFormat;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity=1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* this method increases the number of coffees ordered when + button is clicked
*/
public void increment(View view) {
if (quantity == 100) {
// limits max number of coffee to 100 and shows toast msg if user tries to go over
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
return;}
quantity = quantity +1;
displayQuantity(quantity);
}
/**
* this method decreases the number of coffees ordered when - button is clicked
*/
public void decrement(View view) {
if (quantity == 1) {
// limits min number to 1 and shows toast if user tries to go to 0
Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* Calculates the price of the order based on the current quantity
* @return the price
*/
private int calculatePrice(boolean hasWhippedCream,boolean hasChocolate) {
//calculate coffee base price
int basePrice = 5;
//adds 1 to ase price if whippedCream
if(hasWhippedCream){
basePrice = basePrice +1;
}
//addCs 2 if chocolate
if(hasChocolate){
basePrice =basePrice+2;
}
return basePrice* quantity;
}
/**
* This method is called when the order button is clicked.
* it Gets whippedCream and Chocolate values from CheckBox
* also Gets text from EdiText field
*/
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_check_box);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_check_box);
boolean hasChocolate = chocolateCheckBox.isChecked();
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
int price = calculatePrice(hasWhippedCream,hasChocolate);
String message = createOrderSummary(price, hasWhippedCream, hasChocolate, name);
//sends email containing the order summary
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
intent.putExtra(Intent.EXTRA_TEXT, message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* this method creates a message that will be called fron submitOrder method
* intake @param price and hasWhippedCream
* @return message
*/
private String createOrderSummary(int price, boolean hasWhippedCream, boolean hasChocolate, String name){
String message= "Name : " + name;
message += "\nQuantity " + quantity;
message += "\nAdd whipped cream? " + hasWhippedCream ;
message += "\nAdd chocolate? " + hasChocolate ;
message += "\nTotal: $" + price ;
message+= "\nCheers !";
return message;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + quantity);
}
}
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">
<EditText
android:id="@+id/name_field"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="textCapWords"
android:hint="Name"
android:layout_marginBottom="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Toppings"
android:textAllCaps="true"
android:textSize="16sp" />
<CheckBox
android:id="@+id/whipped_cream_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:textSize="16sp"
android:text="Whipped cream"/>
<CheckBox
android:id="@+id/chocolate_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:textSize="16sp"
android:text="Chocolate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="16sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/lessCoffeeButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:text="-"
android:onClick="decrement"
/>
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="1"
android:textColor="#000000"
android:textSize="16sp"
/>
<Button
android:id="@+id/moreCoffeeButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:text="+"
android:onClick="increment"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Order"
android:textAllCaps="true"
android:onClick="submitOrder"
/>
</LinearLayout>
</ScrollView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment