Last active
April 27, 2016 16:27
-
-
Save Garretthh07/11317204c9a6e38ed2465bd61104c03f to your computer and use it in GitHub Desktop.
JustJava MainActivity.java
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.shunzhang.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 { | |
private int quantity = 2; | |
private int pricePreCupOfCoffee = 5; | |
private int numOfCoffeeMax = 100; | |
private int numOfCoffeeMin = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initVar(); | |
} | |
/** | |
* This method is use for init the global var | |
*/ | |
public void initVar() { | |
quantity = Integer.parseInt(getResources().getString(R.string.quantity_of_coffee)); | |
pricePreCupOfCoffee = Integer.parseInt(getResources().getString(R.string.quantity_pre_coffee)); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
EditText nameEditText = (EditText) findViewById(R.id.name_field); | |
String name = nameEditText.getText().toString(); | |
// Figure out if the user wants whipped cream topping | |
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox); | |
boolean hasWhippedCream = whippedCreamCheckBox .isChecked(); | |
Log.v("MainActivity", "Has whipped cream: " + hasWhippedCream); | |
// Figure out if the user wants chocolate topping | |
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox); | |
boolean hasChocolate = chocolateCheckBox.isChecked(); | |
Log.v("MainActivity", "Has chocolate: " + hasChocolate); | |
int price = calculatePrice(quantity, pricePreCupOfCoffee, hasWhippedCream, hasChocolate); | |
//displayMessage(createOrderSummary(name, price, hasWhippedCream, hasChocolate)); | |
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); | |
} | |
} | |
/** | |
* Open map app on Android device | |
* @param view | |
*/ | |
public void openMap(View view) { | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setData(Uri.parse("geo:47.6, -122.3")); | |
if (intent.resolveActivity(getPackageManager()) != null) { | |
startActivity(intent); | |
} | |
} | |
/** | |
* Create summary of the order. | |
* | |
* @param name of user | |
* @param price of the order. | |
* @param hasWhippedCream | |
* @param hasChocolate | |
* @return text summary | |
*/ | |
private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate) { | |
String priceMessage = "Name: " + name; | |
priceMessage = priceMessage + "\nAdd whipped cream? " + hasWhippedCream; | |
priceMessage = priceMessage + "\nAdd chocolate? " + hasChocolate; | |
priceMessage = priceMessage + "\nQuantity: " + quantity; | |
priceMessage = priceMessage + "\nTotal: $" + price; | |
priceMessage = priceMessage + "\nThank You!!"; | |
return priceMessage; | |
} | |
/** | |
* Calculates the price of the order. | |
* | |
* @param quantity is the number of cups of coffee ordered | |
* @param pricePerCup is the price of one cup of coffee | |
* @param hasWhippedCream is whether or not the user wants whipped cream topping | |
* @param hasChocolate is whether or not the user wants chocolate topping | |
* @return total price | |
*/ | |
private int calculatePrice(int quantity, int pricePerCup, boolean hasWhippedCream, boolean hasChocolate) { | |
// Price of 1 cup of coffee | |
int basePrice = pricePerCup; | |
// Add $1 if the user wants whipped cream | |
if (hasWhippedCream) { | |
basePrice = basePrice + 1; | |
} | |
// Add $2 if the user wants chocolate | |
if (hasChocolate) { | |
basePrice = basePrice + 2; | |
} | |
int price = quantity * basePrice; | |
return price; | |
} | |
/** | |
* This method is called when the plus button is clicked. | |
*/ | |
public void increment(View view) { | |
quantity = quantity + 1; | |
if (quantity > numOfCoffeeMax) { | |
// 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; | |
} | |
display(quantity); | |
} | |
/** | |
* This method is called when the minus button is clicked. | |
*/ | |
public void decrement(View view) { | |
quantity = quantity - 1; | |
if (quantity < numOfCoffeeMin) { | |
// Show an error message as a toast | |
Toast.makeText(this, "You cannot have less than 1 coffees", Toast.LENGTH_SHORT).show(); | |
// Exit this method early because there's nothing left to do | |
return; | |
} | |
display(quantity); | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
* @param number | |
*/ | |
private void display(int number) { | |
TextView quantityTextView = (TextView) findViewById( | |
R.id.quantity_text_view); | |
quantityTextView.setText("" + number); | |
} | |
/** | |
* This method displyas the given text on the screen. | |
* @param message | |
*/ | |
private void displayMessage(String message) { | |
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view); | |
orderSummaryTextView.setText(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment