Created
April 19, 2016 15:46
-
-
Save borisdamato/75cc38bdf76f2c424b03e38b1e063980 to your computer and use it in GitHub Desktop.
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.justjava; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.text.NumberFormat; | |
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int quantity = 2; | |
@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 submitOrder(View view) { | |
int totalPrice = calculatePrice(quantity); | |
String summary = createOrderSummary(totalPrice); | |
displayMessage(summary); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void increment(View view) { | |
quantity = quantity + 1; | |
display(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void decrement(View view) { | |
if (quantity > 0) { | |
quantity = quantity - 1; | |
display(quantity); | |
} | |
} | |
/** | |
* This method displays the given quantity value on the screen. | |
*/ | |
private void display(int number) { | |
TextView quantityTextView = (TextView) findViewById( | |
R.id.quantity_text_view); | |
quantityTextView.setText("" + number); | |
} | |
/** | |
* This method displays the given text on the screen. | |
*/ | |
private void displayMessage(String message) { | |
TextView priceTextView = (TextView) findViewById(R.id.price_text_view); | |
priceTextView.setText(message); | |
} | |
/** | |
* Calculates the price of the order. | |
* | |
* @param quantity is the number of cups of coffee ordered | |
*/ | |
private int calculatePrice(int quantity) { | |
int price = quantity * 5; | |
return price; | |
} | |
/** | |
* Creates the order summary. | |
* | |
* @param price is the total order price | |
*/ | |
private String createOrderSummary(int price) { | |
String summary = "Name: Kapitain Boris"; | |
summary += "\nQuantity: " + quantity; | |
summary += "\nTotal: $" + price; | |
summary += "\nThank you!"; | |
return summary; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment