Advertisement
tampoe

SourceDiskonJava

Nov 30th, 2020 (edited)
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.dosenprogram.hitungdiskon;
  2.  
  3. import java.text.DecimalFormat;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.ImageButton;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16.     // silahkan buat variable untuk
  17.     // gunakan tombol Ctrl + space untuk memudahkan pemanggilan item
  18.     EditText harga, diskon;
  19.     TextView hasildiskon, andahemat;
  20.     Button reset;
  21.     ImageButton hitung;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         // panggil variable yang dibuat diatas sesuai dengan item widget yang
  28.         // ada pada activity_main.xml
  29.         harga = (EditText) findViewById(R.id.harga);
  30.         diskon = (EditText) findViewById(R.id.diskon);
  31.         hasildiskon = (TextView) findViewById(R.id.hasil);
  32.         andahemat = (TextView) findViewById(R.id.hemat);
  33.         hitung = (ImageButton) findViewById(R.id.btnhitung);
  34.         reset = (Button) findViewById(R.id.btnreset);
  35.  
  36.         hitung.setOnClickListener(new OnClickListener() {
  37.  
  38.             @Override
  39.             public void onClick(View view) {
  40.                 // TODO Auto-generated method stub
  41.                 Hitungaja(view);
  42.             }
  43.         });
  44.  
  45.         reset.setOnClickListener(new OnClickListener() {
  46.  
  47.             @Override
  48.             public void onClick(View arg0) {
  49.                 // TODO Auto-generated method stub
  50.                 harga.setText("");
  51.                 diskon.setText("");
  52.                 hasildiskon.setText("");
  53.                 andahemat.setText("");
  54.             }
  55.         });
  56.  
  57.     }
  58.  
  59.     public void Hitungaja(View view) {
  60.         // Double adalah salah satu typedata untuk bilangan berkoma
  61.         Double hrg, disc, hsl, hmt;
  62.         DecimalFormat df = new DecimalFormat("@@##");
  63.  
  64.         hrg = Double.parseDouble(harga.getText().toString());
  65.         disc = Double.parseDouble(diskon.getText().toString());
  66.         hsl = hrg - (hrg * (disc / 100));
  67.         // hasildiskon.setText(String.valueOf(hsl));
  68.         hasildiskon.setText("Rp." + df.format(hsl) + ",00");
  69.         hmt = hrg * (disc / 100);
  70.         andahemat.setText("Selamat!! Anda Hemat = Rp." + df.format(hmt) + ",00");
  71.     }
  72.  
  73.     @Override
  74.     public boolean onCreateOptionsMenu(Menu menu) {
  75.         // Inflate the menu; this adds items to the action bar if it is present.
  76.         getMenuInflater().inflate(R.menu.main, menu);
  77.         return true;
  78.     }
  79.  
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement