Created
February 6, 2018 21:59
-
-
Save Leonard02/632920cd3e6aac21f4f493d222ee18f6 to your computer and use it in GitHub Desktop.
JUST JAVA by Leonard
This file contains hidden or 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
/** | |
* 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.view.View; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.text.NumberFormat; | |
import java.text.NumberFormat; | |
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int quantity = 0; | |
@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 increment(View view) { | |
quantity=quantity+1; | |
if (quantity>100) | |
{ | |
Toast.makeText(this, "You cannot have more than 100 cups of coffee", Toast.LENGTH_SHORT).show(); | |
quantity = 100; | |
} | |
displayQuantity(quantity); | |
} | |
public void decrement(View view) { | |
quantity =quantity-1; | |
if (quantity<1) | |
{ | |
Toast.makeText(this, "You cannot have less than 1 cup of coffee", Toast.LENGTH_SHORT).show(); | |
quantity = 1; | |
} | |
displayQuantity(quantity); | |
} | |
public void submitOrders(View view) { | |
EditText addNameEditText = (EditText) findViewById(R.id.name_field); | |
String name =addNameEditText.getText().toString(); | |
EditText addSurnameEditText = (EditText) findViewById(R.id.surname_field); | |
String surname =addSurnameEditText.getText().toString(); | |
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(); | |
CheckBox extraSugarCheckBox = (CheckBox) findViewById(R.id.extra_suggar_checkbox); | |
boolean hasExtrSugar = extraSugarCheckBox.isChecked(); | |
int price=calculatePrice(hasWhippedCream,hasChocolate); | |
String priceMessage=createOrderSummary(name,surname,price,hasWhippedCream,hasChocolate, hasExtrSugar); | |
{ | |
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); | |
} | |
private int calculatePrice(boolean addWhippedCream,boolean addChocolate){ | |
int basePrice=5; | |
if (addWhippedCream) | |
{ | |
basePrice=basePrice+1; | |
} | |
if (addChocolate); | |
{ | |
basePrice=basePrice+2; | |
} | |
int price = quantity * basePrice; | |
return price; | |
} | |
private String createOrderSummary(String addName, String addSurname, int price, boolean addWhippedCream,boolean addChocolate, boolean addExtraSugar){ | |
String priceMessage="Name: " + addName; | |
priceMessage+="\nSurname: " +addSurname; | |
priceMessage+="\nAdd Whipped Cream: " +addWhippedCream; | |
priceMessage+="\nAdd Chocolate: " +addChocolate; | |
priceMessage+="\nAdd Extra Sugar: " +addExtraSugar; | |
priceMessage+="\nQuantity: " + quantity; | |
priceMessage+="\nTotal:$ "+price; | |
priceMessage+="\nThank you!"; | |
return priceMessage; | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
*/ | |
private void displayQuantity(int number1) { | |
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); | |
quantityTextView.setText("" + number1); | |
} | |
/** | |
* This method displays the given price on the screen. | |
*/ | |
/** | |
* This method displays the given text on the screen. | |
*/ | |
private void displayMessage(String message) { | |
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_view); | |
orderSummaryTextView.setText(message); | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<ScrollView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:background="@drawable/images" | |
tools:context="com.example.android.justjava.MainActivity"> | |
<EditText | |
android:id="@+id/name_field" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Add Name" | |
android:inputType="textCapWords" /> | |
<EditText | |
android:id="@+id/surname_field" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Add Surname" | |
android:inputType="textCapWords" /> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:padding="10dp" | |
android:text="Toppings" | |
android:textColor="@android:color/darker_gray" | |
android:textSize="16sp" | |
android:textAllCaps="true"/> | |
<CheckBox | |
android:id="@+id/whipped_cream_checkbox" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="24dp" | |
android:layout_marginLeft="16dp" | |
android:text="Whipped cream" | |
android:textSize="16sp" | |
/> | |
<CheckBox | |
android:id="@+id/chocolate_checkbox" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="24dp" | |
android:layout_marginLeft="16dp" | |
android:text="Chocolate" | |
android:textSize="16sp" | |
/> | |
<CheckBox | |
android:id="@+id/extra_suggar_checkbox" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:paddingLeft="24dp" | |
android:layout_marginLeft="16dp" | |
android:text="Extra Sugar" | |
android:textSize="16sp" | |
/> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:padding="10dp" | |
android:text="Quantity" | |
android:textColor="@android:color/darker_gray" | |
android:textSize="16sp" | |
android:textAllCaps="true"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<Button | |
android:id="@+id/plus_button" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:onClick="decrement" | |
android:text="-" /> | |
<TextView | |
android:id="@+id/quantity_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:text="0" | |
android:paddingLeft="8dp" | |
android:paddingRight="8dp" | |
android:textColor="@android:color/black" | |
android:textSize="24sp" /> | |
<Button | |
android:id="@+id/minus_button" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:onClick="increment" | |
android:text="+" | |
android:textSize="20dp"/> | |
</LinearLayout> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:padding="10dp" | |
android:text="Order Summary" | |
android:textColor="@android:color/darker_gray" | |
android:textSize="16sp" | |
android:textAllCaps="true"/> | |
<TextView | |
android:id="@+id/order_summary_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:padding="10dp" | |
android:text="$0" | |
android:textColor="@android:color/black" | |
android:textSize="16sp" /> | |
<Button | |
android:id="@+id/next_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:onClick="submitOrders" | |
android:text="ORDER" /> | |
</LinearLayout> | |
</ScrollView> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment