xSiRON

MainActivity.java

Feb 26th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package justjava.alessiosferro.com.justjava;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import java.text.NumberFormat;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17.     // Attributi Privati
  18.     private EditText editText;
  19.     private CheckBox checkBox;
  20.     private TextView quantity;
  21.     private TextView price;
  22.     private TextView finalPrice;
  23.     private TextView summary;
  24.     private TextView orders;
  25.     private TextView whippedCream;
  26.     private TextView name;
  27.     private int var;
  28.     private boolean order;
  29.  
  30.     // Metodo protetto (ereditato da AppCompatActivity)
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.         editText = (EditText) findViewById(R.id.editText);
  35.         checkBox = (CheckBox) findViewById(R.id.prova_check_box);
  36.         quantity = (TextView) findViewById(R.id.quantity_text_view);
  37.         price = (TextView) findViewById(R.id.price_text_view);
  38.         finalPrice = (TextView) findViewById(R.id.finalPrice);
  39.         summary = (TextView) findViewById(R.id.summary);
  40.         orders = (TextView) findViewById(R.id.orders);
  41.         whippedCream = (TextView) findViewById(R.id.panna);
  42.         name = (TextView) findViewById(R.id.nome);
  43.         order = false;
  44.     }
  45.  
  46.     // Metodi pubblici
  47.  
  48.     /**
  49.      * Questo metodo resetta le textView a default alla pressione del bottone CANCELLA
  50.      * @param view prende la view come parametro
  51.      */
  52.  
  53.     public void canc(View view){
  54.         editText.setText("");
  55.         checkBox.setChecked(false);
  56.         quantity.setText("0");
  57.         price.setText("0,00 €");
  58.         finalPrice.setText("");
  59.         summary.setText("");
  60.         orders.setText("");
  61.         whippedCream.setText("");
  62.         name.setText("");
  63.         var = 0;
  64.         order = false;
  65.     }
  66.  
  67.     /**
  68.      * Questo metodo imposta un ordine alla pressione del bottone ORDINA
  69.      * @param view prende la view corrente come parametro
  70.      */
  71.  
  72.     public void submitOrder(View view) {
  73.         if(editText.length() == 0){
  74.             Toast.makeText(MainActivity.this, "Inserisci il nome!", Toast.LENGTH_LONG).show();
  75.             return;
  76.         }else if(var == 0){
  77.             Toast.makeText(MainActivity.this, "Nessun ordine inserito!", Toast.LENGTH_LONG).show();
  78.             return;
  79.         }
  80.  
  81.         order = true;
  82.  
  83.         summary.setText("Riepilogo ordine");
  84.         orders.setText("Numero ordini: " + quantity.getText());
  85.         finalPrice.setText("Prezzo: " + price.getText());
  86.  
  87.         if(checkBox.isChecked()){
  88.             whippedCream.setText("Panna: SI");
  89.         }else{
  90.             whippedCream.setText("Panna: NO");
  91.         }
  92.  
  93.         name.setText("Nome: " + editText.getText());
  94.     }
  95.  
  96.     /**
  97.      * Questo metodo invoca il metodo composeEmail alla pressione del bottone EMAIL
  98.      * @param view prende la view corrente come parametro
  99.      */
  100.  
  101.     public void email(View view){
  102.         if(!order){
  103.             Toast.makeText(MainActivity.this, "Ordina qualcosa!", Toast.LENGTH_LONG).show();
  104.             return;
  105.         }
  106.         composeEmail("" + summary.getText() + "\n" + name.getText() + "\n" + orders.getText() + "\n"
  107.                 + whippedCream.getText() + "\n" + finalPrice.getText(), "Ordine");
  108.     }
  109.  
  110.     public void pannaMont(View view){
  111.         if(checkBox.isChecked())
  112.             price.setText(NumberFormat.getCurrencyInstance().format(var*.80));
  113.         else
  114.             price.setText(NumberFormat.getCurrencyInstance().format(var*.50));
  115.     }
  116.  
  117.     public void increment(View view){
  118.         var++;
  119.         quantity.setText("" + var);
  120.         displayPrice();
  121.     }
  122.  
  123.     public void decrement(View view){
  124.         if(var > 0) {
  125.             var--;
  126.             quantity.setText("" + var);
  127.             displayPrice();
  128.         }else{
  129.             Toast.makeText(MainActivity.this, "Impossibile decrementare", Toast.LENGTH_SHORT).show();
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Questo metodo crea un intent per le app di posta elettronica
  135.      * @param body corpo dell'email
  136.      * @param subject oggetto dell'email
  137.      */
  138.  
  139.     public void composeEmail(String body, String subject) {
  140.         Intent intent = new Intent(Intent.ACTION_SENDTO);
  141.         intent.setData(Uri.parse("mailto:")); // only email apps should handle this
  142.         intent.putExtra(Intent.EXTRA_TEXT, body);
  143.         intent.putExtra(Intent.EXTRA_SUBJECT, subject);
  144.         if (intent.resolveActivity(getPackageManager()) != null) {
  145.             startActivity(intent);
  146.         }
  147.     }
  148. }
Add Comment
Please, Sign In to add comment