Skip to content

Instantly share code, notes, and snippets.

@mathsways
Created February 13, 2018 07:32
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 mathsways/99a3a1cc58697020e59496f3c78468e3 to your computer and use it in GitHub Desktop.
Save mathsways/99a3a1cc58697020e59496f3c78468e3 to your computer and use it in GitHub Desktop.
Coofee Order final
/**
* 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.mathsways.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;
/** */
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 1;
String theName;
String priceMessage;
String emailAdresseT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
* quantity += 1 is like quantity=quantity+1.
*/
public void increment(View view) {
if (quantity >= 100) {
Toast.makeText(this, "You can not have more than 100 cups of coffees", Toast.LENGTH_SHORT).show();
return;
}
quantity += 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void decrement(View view) {
if (quantity == 1) {
Toast.makeText(this, "You can not have 0 cup of coffee", Toast.LENGTH_SHORT).show();
return;
}
quantity -= 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
CheckBox W_C_Checkox = findViewById(R.id.W_C_Checkox);
boolean hasWB = W_C_Checkox.isChecked();
CheckBox Choco_Checkox = findViewById(R.id.Choco_Checkox);
boolean hasChok = Choco_Checkox.isChecked();
// get EditText by id and store it into "inputTxt"
EditText yourname = (EditText) findViewById(R.id.name_input);
// Store EditText - Input in variable
theName = yourname.getText().toString();
/** Log.v("MainActivity", "Hase whipped cream" + hasWB);*/
int price = calculatePrice(hasChok, hasWB);
/** display the message -*/
priceMessage = creatOrderSummary(theName, price, hasWB, hasChok);
displayMessage(priceMessage);
}
/**
* This method displays the given email on the screen.
*/
private void emailAdresse(String Email) {
EditText emailAdresseT = (EditText) findViewById(R.id.email_input);
emailAdresseT.setText(Email);
return ;
}
/** send message */
public void SendEmail(View view) {
/** send Email message */
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Order For " + theName);
intent.putExtra(Intent.EXTRA_EMAIL, emailAdresseT);
intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* New Method for MainActivity
*/
public String creatOrderSummary(String theNameT, int price, boolean hasWBok, boolean hasChokok) {
String messgaet;
String WithOrWithout = "Without";
String whithChoco = "Without";
if (hasWBok == true) {
WithOrWithout = "with";
} else {
WithOrWithout = "without";
}
if (hasChokok == true) {
whithChoco = "with";
} else {
whithChoco = "without";
}
messgaet = getString(R.string.Hi) + " " + theName+ "\nYou have " + quantity + " Cups of coffees \n";
messgaet = messgaet + WithOrWithout + " whipped cream\n& ";
messgaet = messgaet + whithChoco + " chocolate\n";
messgaet = messgaet + "The total price is " + price + "$" + "\nTank you";
return messgaet;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int nbr) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + nbr);
}
/**
* This method displays the given price on the screen.
*/
/**
* This method displays the given price on the screen.
*/
/** i delete it
* private void displayPrice(int number) {
* TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
* priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
* }
* */
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSommaryTextView = (TextView) findViewById(R.id.order_sommary_text_view);
orderSommaryTextView.setText(message);
}
/**
* Calculates the price of the order.
*/
private int calculatePrice(boolean addchoco, boolean addWB) {
int finalprice = 5;
if (addWB) {
finalprice = finalprice + 1;
}
if (addchoco) {
finalprice = finalprice + 2;
}
return quantity * finalprice;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Title for the application. [CHAR LIMIT=12] -->
<string name="app_name">Just JAVA</string>
/** */
<string name="Hi">Hi</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="name">Name</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="toppings">Ingredients</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="whipped_cream">"Whipped cream "</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="chocolate">Chocolate</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="quantity">Quantity</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=5] -->
<string name="initial_quantity_value">2</string>
<!-- Hint text display in the empty field for the user's name [CHAR LIMIT=20] -->
<string name="order">Order</string>
<!--
Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the
user's name. [CHAR LIMIT=NONE]
-->
<string name="order_summary_name">Name: <xliff:g id="name" example="Amy">%s</xliff:g></string>
<!--
Whipped cream topping for the order summary. It will be shown in the format of
"Add whipped cream? true" or "Add whipped cream? false". [CHAR LIMIT=NONE]
-->
<string name="order_summary_whipped_cream">Add the whipped cream ? <xliff:g id="addWhippedCream" example="true">%b</xliff:g></string>
<!--
Chocolate topping for the order summary. It will be shown in the format of
"Add chocolate? true" or "Add chocolate? false". [CHAR LIMIT=NONE]
-->
<string name="order_summary_chocolate">Add the chocolate? <xliff:g id="addChocolate" example="true">%b</xliff:g></string>
<!--
Quantity of coffee cups for the order summary. It will be shown in the format of
"Quantity: 2", where 2 is the number of cups ordered. [CHAR LIMIT=NONE]
-->
<string name="order_summary_quantity">Quantity: <xliff:g id="quantity" example="2">%d</xliff:g></string>
<!--
Total price for the order summary. It will be shown in the format of
"Total: $10" where $10 is the price. [CHAR LIMIT=NONE]
-->
<string name="order_summary_price">Total: <xliff:g id="price" example="$10">%s</xliff:g></string>
<!-- Thank you message for the order summary. [CHAR LIMIT=NONE] -->
<string name="thank_you">Thank You!</string>
<!--
Subject line for the order summary email. It will be in the format of
"Just Java order for Amy" where Amy is the user's name. [CHAR LIMIT=NONE]
-->
<string name="order_summary_email_subject">Only java for <xliff:g id="name" example="Amy">%s</xliff:g></string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#26A69A</item>
<item name="colorPrimaryDark">#00796B</item>
<item name="colorAccent">#80CBC4</item>
</style>
<style name="headerTextStyle">
<item name="android:layout_height">48dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textAllCaps">true</item>
<item name="android:textSize">17sp</item>
<item name="android:layout_marginBottom">16dp</item>
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">16dp</item>
</style>
</resources>
<?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_height="match_parent"
android:layout_width="match_parent">
<LinearLayout 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"
android:background="@drawable/coffee"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:textSize="20sp"
android:hint="@string/name"
android:inputType="textCapWords"/>
<EditText
android:id="@+id/email_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"
android:hint="Email"
android:inputType="textEmailAddress"/>
<TextView
style="@style/headerTextStyle"
android:text="toppings" />
<CheckBox
android:id="@+id/W_C_Checkox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="whipped Cream"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp" />
<CheckBox
android:id="@+id/Choco_Checkox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="Chocolate"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="16sp" />
<TextView
style="@style/headerTextStyle"
android:text="Quantity"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="1"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
style="@style/headerTextStyle"
android:text="order sommary" />
<TextView
android:id="@+id/order_sommary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0$"
android:textColor="@android:color/black"
android:textSize="21sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:padding="8dp"
android:text="ORDER"
android:textSize="16dp" />
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="SendEmail"
android:padding="8dp"
android:text="Send Order By Email"
android:textSize="16dp" />
</LinearLayout>
</ScrollView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment