Advertisement
Umambeeirozst

Second Activity Java

Mar 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package com.example.kedaiumis;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.CheckBox;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import java.text.NumberFormat;
  13.  
  14. /**
  15. * This app displays an order form to order coffee.
  16. */
  17. public class SecondActivity extends AppCompatActivity {
  18.  
  19. int quantity=0;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.second_main);
  24. }
  25.  
  26.  
  27. public void increment(View view){//perintah tombol tambah
  28. if(quantity==100){
  29. Toast.makeText(this,"pesanan maximal 100",Toast.LENGTH_SHORT).show();
  30. return;
  31. }
  32. quantity = quantity+1 ;
  33. display(quantity);
  34. }
  35. public void decrement(View view){//perintah tombol tambah
  36. if (quantity==1){
  37. Toast.makeText(this,"pesanan minimal 1",Toast.LENGTH_SHORT).show();
  38. return;
  39. }
  40. quantity = quantity -1;
  41. display(quantity);
  42. }
  43.  
  44.  
  45. public void Submitorder(View view) {
  46. EditText nameEditText=(EditText)findViewById(R.id.edt_name);
  47. String name=nameEditText.getText().toString();
  48. Log.v("MainActivity","Nama:"+name);
  49.  
  50. CheckBox whippedcreamChekBox= (CheckBox) findViewById(R.id.WhippedCream_checkbox);
  51. boolean haswhippedcream=whippedcreamChekBox.isChecked();//mengidentifikasi check
  52. Log.v("MainActivity","has whippedcream:"+haswhippedcream);
  53.  
  54. CheckBox chocolateChekBox= (CheckBox) findViewById(R.id.Chocolate_checkbox);
  55. boolean haschocolate=chocolateChekBox.isChecked();//mengidentifikasi check
  56. Log.v("MainActivity","has whippedcream:"+haschocolate);
  57.  
  58. int price=calculateprice(haswhippedcream,haschocolate);//memanggil method jumlah harga
  59. String pricemessage=createOrderSummary(price,name,haswhippedcream,haschocolate);
  60.  
  61.  
  62. displayMessage(pricemessage);
  63.  
  64. }
  65. private int calculateprice(boolean addwhipedcream,boolean addchocolate){//jumlah pesanan * harga
  66. int harga=0;
  67.  
  68. if(addwhipedcream){
  69. harga=13000;//harga tambahan toping
  70. }
  71.  
  72. if (addchocolate){
  73. harga=15000;
  74. }
  75.  
  76. return quantity * harga;
  77. }
  78. private String createOrderSummary(int price, String name, boolean addChocolate, boolean addWhippedCream) {//hasil pemesanan
  79. String pricemessage=" Nama = "+name;
  80. if (addChocolate)
  81. pricemessage+="\n Roti";
  82. else if (addWhippedCream)
  83. pricemessage+="\n Ice Cream";
  84. pricemessage+="\n Jumlah Pemesanan =" +quantity;
  85. pricemessage+="\n Total = Rp " +price;
  86. pricemessage+="\n Terimakasih";
  87. return pricemessage;
  88. }
  89.  
  90. //method ini untuk mencetak hasil perintah yang di tampilkan dengan inisial quantity_textview di textview 0
  91. private void displayMessage(String message) {
  92. TextView priceTextView = (TextView) findViewById(R.id.price_textview);
  93. priceTextView.setText(message);
  94. }
  95. private void display(int number) {
  96. TextView quantityTextView = (TextView) findViewById(R.id.quantity_textview);
  97. quantityTextView.setText("" + number);
  98. }
  99. private void displayPrice(int number) {
  100. TextView priceTextView = (TextView) findViewById(R.id.price_textview);
  101. priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement