Advertisement
tampoe

ActivityMainPoin

Dec 4th, 2020 (edited)
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package com.dosenprogram.hitungpoin;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends Activity {
  13.     // CARA PERTAMA
  14.     // ************************************************************
  15.     // Pembuatan variable yang dibutuhkan sesuai dengan yang ada pada layout
  16.     TextView nama; // variable nama untuk id textview etnama
  17.     EditText harga, jumlah, total, poin;
  18.     // variable harga untuk id edittext etharga
  19.     // variable jumlah untuk id edittext etjumlah
  20.     // variable total untuk id edittext ettotalharga
  21.     // variable poin untuk id edittext etpoin
  22.     Button hitung, batal;
  23.  
  24.     // variable hitung untuk id button btnhitung
  25.     // variable batal untuk id button btnbatal
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.         // CARA KEDUA
  32.         // ************************************************************
  33.         // Pemanggilan variable yang telah dibuat diatas
  34.         nama = (TextView) findViewById(R.id.etnama);
  35.         harga = (EditText) findViewById(R.id.etharga);
  36.         jumlah = (EditText) findViewById(R.id.etjumlah);
  37.         total = (EditText) findViewById(R.id.ettotalharga);
  38.         poin = (EditText) findViewById(R.id.etpoin);
  39.         hitung = (Button) findViewById(R.id.btnhitung);
  40.         hitung.setOnClickListener(new OnClickListener() {
  41.  
  42.             @Override
  43.             public void onClick(View view) {
  44.                 // TODO Auto-generated method stub
  45.                 perhitungan(view);
  46.             }
  47.         });
  48.  
  49.         batal = (Button) findViewById(R.id.btnbatal);
  50.         batal.setOnClickListener(new OnClickListener() {
  51.  
  52.             @Override
  53.             public void onClick(View arg0) {
  54.                 // TODO Auto-generated method stub
  55.                 nama.setText("");
  56.                 harga.setText("0");
  57.                 jumlah.setText("0");
  58.                 total.setText("0");
  59.                 poin.setText("0");
  60.             }
  61.         });
  62.     }
  63.  
  64.     // CARA KETIGA
  65.     // ************************************************************
  66.     // Pembuatan prosedure untuk menghitung total harga dan jumlah poin
  67.  
  68.     public void perhitungan(View view) {
  69.         // Buatkan tipe data untuk variable harga, jumlah, totalharga dan poin
  70.         // usahakan variable ini berbeda dengan variable yang ada pada cara
  71.         // pertama
  72.  
  73.         int hrg, jlh, ttl, jpoin;
  74.         hrg = Integer.parseInt(String.valueOf(harga.getText()));
  75.         jlh = Integer.parseInt(String.valueOf(jumlah.getText()));
  76.         ttl = hrg * jlh;
  77.         total.setText(String.valueOf(ttl));
  78.         jpoin = ttl / 15000;
  79.         poin.setText(String.valueOf(jpoin));
  80.  
  81.     }
  82.  
  83.     @Override
  84.     public boolean onCreateOptionsMenu(Menu menu) {
  85.         // Inflate the menu; this adds items to the action bar if it is present.
  86.         getMenuInflater().inflate(R.menu.main, menu);
  87.         return true;
  88.     }
  89.  
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement