Advertisement
Leedwon

Untitled

Aug 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /**
  2.  * IMPORTANT: Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  3.  * This is the package name our example uses:
  4.  *
  5.  * package com.example.android.justjava;
  6.  *
  7.  */
  8. package com.example.android.justjava;
  9. import android.os.Bundle;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.view.View;
  12. import android.widget.TextView;
  13. import java.text.NumberFormat;
  14.  
  15. /**
  16.  * This app displays an order form to order coffee.
  17.  */
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.     }
  25.  
  26.     /**
  27.      * This method is called when the order button is clicked.
  28.      */
  29.     private int quantity = 0;
  30.     private static int cofeePrice = 5;
  31.  
  32.     public void submitOrder(View view) {
  33.         display(quantity);
  34.         displayPrice(quantity * cofeePrice);
  35.     }
  36.  
  37.     /**
  38.      * This method displays the given quantity value on the screen.
  39.      */
  40.     private void display(int number) {
  41.         TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  42.         quantityTextView.setText("" + number);
  43.     }
  44.  
  45.     /**
  46.      * This method displays the given price on the screen.
  47.      */
  48.     private void displayPrice(int number) {
  49.         TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
  50.         priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
  51.     }
  52.  
  53.     public void addCoffee(View view) {
  54.         increase(quantity);
  55.     }
  56.  
  57.     public void substractCoffee(View view){
  58.         decrease(quantity);
  59.     }
  60.  
  61.     public void increase(int number) {
  62.         number++;
  63.         TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  64.         quantityTextView.setText("" + number);
  65.     }
  66.  
  67.     public void decrease(int number) {
  68.         number--;
  69.         TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  70.         quantityTextView.setText("" + number);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement