Advertisement
Guest User

Tip calc

a guest
Nov 20th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. package com.example.jdsch_000.testing;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7.  
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16.     EditText billAmount =(EditText) findViewById(R.id.billAmount);
  17.     EditText tip1Field = (EditText) findViewById(R.id.tip1);
  18.     EditText tip2Field = (EditText) findViewById(R.id.tip2);
  19.     EditText tip3Field = (EditText) findViewById(R.id.tip3);
  20.  
  21.     EditText comp1 = (EditText) findViewById(R.id.comp1);
  22.     EditText comp2 = (EditText) findViewById(R.id.comp2);
  23.     EditText comp3 = (EditText) findViewById(R.id.comp3);
  24.  
  25.  
  26.  
  27.  
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.     }
  34.  
  35.  
  36.     @Override
  37.     public boolean onCreateOptionsMenu(Menu menu) {
  38.         // Inflate the menu; this adds items to the action bar if it is present.
  39.         getMenuInflater().inflate(R.menu.menu_main, menu);
  40.  
  41.         Button okBtn = (Button) findViewById(R.id.okBtn);
  42.         Button resetBtn = (Button) findViewById(R.id.resetBtn);
  43.  
  44.  
  45.         okBtn.setOnClickListener(new View.OnClickListener() {
  46.             @Override
  47.             public void onClick(View v) {
  48.                tip tip1 =  new tip(Double.parseDouble(billAmount.toString()), Double.parseDouble(tip1Field.toString()));
  49.                 tip tip2 =  new tip(Double.parseDouble(billAmount.toString()), Double.parseDouble(tip2Field.toString()));
  50.                 tip tip3 =  new tip(Double.parseDouble(billAmount.toString()), Double.parseDouble(tip3Field.toString()));
  51.  
  52.                 comp1.setText(String.format(".2f", tip1.calculateTip()));
  53.                 comp2.setText(String.format(".2f", tip2.calculateTip()));
  54.                 comp3.setText(String.format(".2f", tip3.calculateTip()));
  55.  
  56.             }
  57.         });
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.         return true;
  66.     }
  67.  
  68.  
  69.  
  70.     @Override
  71.     public boolean onOptionsItemSelected(MenuItem item) {
  72.         // Handle action bar item clicks here. The action bar will
  73.         // automatically handle clicks on the Home/Up button, so long
  74.         // as you specify a parent activity in AndroidManifest.xml.
  75.         int id = item.getItemId();
  76.  
  77.         //noinspection SimplifiableIfStatement
  78.         if (id == R.id.action_settings) {
  79.             return true;
  80.         }
  81.  
  82.         return super.onOptionsItemSelected(item);
  83.     }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. package com.example.jdsch_000.testing;
  90.  
  91. /**
  92.  * Created by jdsch_000 on 11/20/2014.
  93.  */
  94. public class tip {
  95.     private double billAmount;
  96.     private double rate;
  97.  
  98.  
  99.  
  100.     public tip(double v){
  101.         billAmount = 0;
  102.     }
  103.  
  104.     public tip(double billAmount, double rate){
  105.         this.rate = rate / 100;
  106.         this.billAmount = billAmount;
  107.     }
  108.  
  109.     public double calculateTip(){
  110.         double total = rate * billAmount;
  111.  
  112.  
  113.         return total;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement