Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java, Android App: Modify this app. Allow the user to input the number of guests and calculate the tip and total per guest. 1 package

Java, Android App: Modify this app. Allow the user to input the number of guests and calculate the tip and total per guest. image text in transcribed
image text in transcribed
1 package com.jblearning.tipcalculatorv4 3 import android.os. Bundle 4 import android.support.v7. app. AppCompatactivity 5 import android.text.Editable: 6 import android.text.TextWatcher 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import java.text. NumberFormat 10 11 public class MainAactivity extends AppCompatActivity ( 13 public NumberFormat money NumberFormat.getcurrencyInstance 12 private TipCalculator tipcalc private EditText billEditText private EditText tipEditTexti 14 15 16 17 18 19 20 21 eoverride protected void oncreate( Bundle savedinstancestate) ( super.onCreate( savedInstanceState) tipCalc- new TipCalculator ( 0.17E, 100.0E setContentView( R.layout.activity main); billEditText = ( EditText ) findviewById( R.d.amount, bill ); tipEditText = ( EditText ) findviemById( R.id.amount-tip-percent ) ; 23 24 25 26 27 28 29 30 31 public void calculate 32 TextChangeHandler tchnew TextChangeHandler billEditText.addTextChangedListener( tch ) tipEditText.addTextChangedListener( tch) String bilistring -billEditText.getText (.tostring String tipString tipEditText.getText () .tostring) TextView tipTextView = 34 35 36 37 3 8 39 40 TextView) findviewByid( R.id. amount_tip: TextView tota!TextView = (TextView) findviewById ( R. id,amount-total ); // convert billString and tipString to floats float bil1Amount Float.parseFloat ( bilistring int tipPercent Integer.parseint( tipstring; // update the Model tipCalc.setBill ( billAmount): tipCalc.setTip( .01f tipPercent // ask Model to calculate tip and total 42 43 4 6 amounts 47 4 8 49 50 51 52 53 54 float tip tipCalc.tipAmount ) float total tipCalc.totalAmount ( // update the View with formatted tip and total amounts tipTextView.setText( money.format( tip totalTextView.setText( money.format ( total ) ) catch( NumberFormatException nfe) // pop up an alert view here 56 57 58 59 60 61 62 63 64 65 private class TextchangeHandler implements TextWatcherf public void afterTextChanged( Editable e calculate) public void beforeTextChanged ( Charsequence s, int start, int count, int after public void onTextChanged Charsequence s, int start, 67 68 69 70 int before, int after) 1 package com.jblearning.tipcalculatorv4 3 import android.os. Bundle 4 import android.support.v7. app. AppCompatactivity 5 import android.text.Editable: 6 import android.text.TextWatcher 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import java.text. NumberFormat 10 11 public class MainAactivity extends AppCompatActivity ( 13 public NumberFormat money NumberFormat.getcurrencyInstance 12 private TipCalculator tipcalc private EditText billEditText private EditText tipEditTexti 14 15 16 17 18 19 20 21 eoverride protected void oncreate( Bundle savedinstancestate) ( super.onCreate( savedInstanceState) tipCalc- new TipCalculator ( 0.17E, 100.0E setContentView( R.layout.activity main); billEditText = ( EditText ) findviewById( R.d.amount, bill ); tipEditText = ( EditText ) findviemById( R.id.amount-tip-percent ) ; 23 24 25 26 27 28 29 30 31 public void calculate 32 TextChangeHandler tchnew TextChangeHandler billEditText.addTextChangedListener( tch ) tipEditText.addTextChangedListener( tch) String bilistring -billEditText.getText (.tostring String tipString tipEditText.getText () .tostring) TextView tipTextView = 34 35 36 37 3 8 39 40 TextView) findviewByid( R.id. amount_tip: TextView tota!TextView = (TextView) findviewById ( R. id,amount-total ); // convert billString and tipString to floats float bil1Amount Float.parseFloat ( bilistring int tipPercent Integer.parseint( tipstring; // update the Model tipCalc.setBill ( billAmount): tipCalc.setTip( .01f tipPercent // ask Model to calculate tip and total 42 43 4 6 amounts 47 4 8 49 50 51 52 53 54 float tip tipCalc.tipAmount ) float total tipCalc.totalAmount ( // update the View with formatted tip and total amounts tipTextView.setText( money.format( tip totalTextView.setText( money.format ( total ) ) catch( NumberFormatException nfe) // pop up an alert view here 56 57 58 59 60 61 62 63 64 65 private class TextchangeHandler implements TextWatcherf public void afterTextChanged( Editable e calculate) public void beforeTextChanged ( Charsequence s, int start, int count, int after public void onTextChanged Charsequence s, int start, 67 68 69 70 int before, int after)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions