Created
February 4, 2018 22:25
-
-
Save HOLARO/89b7b49ae3265e0770f70f95e0ecb00b to your computer and use it in GitHub Desktop.
This is the .java file of my Just Java app.
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.newjustjava; | |
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 = 0; | |
@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 an error message as a toast | |
Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show(); | |
// Exit this method early because there's nothing left to do | |
return; | |
} | |
quantity = quantity + 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method is called when the minus button is clicked. | |
*/ | |
public void decrement(View view) { | |
if (quantity == 1) { | |
// Show an error message as a toast | |
Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show(); | |
// Exit this method early because there's nothing left to do | |
return; | |
} | |
quantity = quantity - 1; | |
displayQuantity(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
EditText nameField = findViewById(R.id.name_field); | |
String name = nameField.getText().toString(); | |
// Figure out if the user wants whipped cream topping | |
CheckBox whippedCreamCheckBox = findViewById(R.id.whipped_cream_checkbox); | |
boolean hasWhippedCream = whippedCreamCheckBox.isChecked(); | |
// Figure out if the user wants chocolate topping | |
CheckBox chocolateCheckBox = findViewById(R.id.chocolate_checkbox); | |
boolean hasChocolate = chocolateCheckBox.isChecked(); | |
// Calculate the price | |
int price = calculatePrice(hasWhippedCream, hasChocolate); | |
// Display the order summary on the screen | |
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); | |
} | |
} | |
/** | |
* Calculates the 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 total price | |
*/ | |
private int calculatePrice(boolean addWhippedCream, boolean addChocolate) { | |
int basePrice = 5; | |
if (addWhippedCream) { | |
basePrice = basePrice + 1; | |
} | |
if (addChocolate) { | |
basePrice = basePrice + 2; | |
} | |
return quantity * basePrice; | |
} | |
/** | |
* Create summary of the order. | |
* | |
* @param name of the customer | |
* @param price of the order | |
* @param addWhippedCream is whether or not to add whipped cream to the coffee | |
* @param addChocolate is whether or not to add chocolate to the coffee | |
* @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, price); | |
priceMessage += "\n" + getString(R.string.thank_you); | |
return priceMessage; | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
*/ | |
private void displayQuantity(int numberOfCoffees) { | |
TextView quantityTextView = findViewById(R.id.quantity_text_view); | |
quantityTextView.setText("" + numberOfCoffees); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment