Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dosenprogram.hitungdiskon;
- import java.text.DecimalFormat;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- // silahkan buat variable untuk
- // gunakan tombol Ctrl + space untuk memudahkan pemanggilan item
- EditText harga, diskon;
- TextView hasildiskon, andahemat;
- Button reset;
- ImageButton hitung;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // panggil variable yang dibuat diatas sesuai dengan item widget yang
- // ada pada activity_main.xml
- harga = (EditText) findViewById(R.id.harga);
- diskon = (EditText) findViewById(R.id.diskon);
- hasildiskon = (TextView) findViewById(R.id.hasil);
- andahemat = (TextView) findViewById(R.id.hemat);
- hitung = (ImageButton) findViewById(R.id.btnhitung);
- reset = (Button) findViewById(R.id.btnreset);
- hitung.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- Hitungaja(view);
- }
- });
- reset.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- harga.setText("");
- diskon.setText("");
- hasildiskon.setText("");
- andahemat.setText("");
- }
- });
- }
- public void Hitungaja(View view) {
- // Double adalah salah satu typedata untuk bilangan berkoma
- Double hrg, disc, hsl, hmt;
- DecimalFormat df = new DecimalFormat("@@##");
- hrg = Double.parseDouble(harga.getText().toString());
- disc = Double.parseDouble(diskon.getText().toString());
- hsl = hrg - (hrg * (disc / 100));
- // hasildiskon.setText(String.valueOf(hsl));
- hasildiskon.setText("Rp." + df.format(hsl) + ",00");
- hmt = hrg * (disc / 100);
- andahemat.setText("Selamat!! Anda Hemat = Rp." + df.format(hmt) + ",00");
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement