Question

In: Computer Science

Allow the user to enter the number of people in the party. Calculate and display the...

Allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mdc.tippcalcula">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

-------------------------------

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:useDefaultMargins="true"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/amountEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint=""
        android:digits="0123456789"
        android:inputType="number"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:maxLength="6"/>

    <TextView
        android:id="@+id/amountTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        android:background="@color/amount_background"
        android:elevation="@dimen/elevation"
        android:hint="@string/enter_amount"
        android:padding="@dimen/textview_padding"
        />

    <TextView
        android:id="@+id/percentTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|end"
        android:text="@string/tip_percentage" />

    <SeekBar
        android:id="@+id/percentSeekBar"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/seekbar_height"
        android:layout_gravity="fill_horizontal"
        android:max="30"
        android:progress="15"/>

    <TextView
        android:id="@+id/TipLabelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/tip" />

    <TextView
        android:id="@+id/tipTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:background="@color/result_background"
        android:elevation="@dimen/elevation"
        android:gravity="center"
        android:padding="@dimen/textview_padding" />

    <TextView
        android:id="@+id/totalLabelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/total" />

    <TextView
        android:id="@+id/totalTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:background="@color/result_background"
        android:elevation="@dimen/elevation"
        android:gravity="center"
        android:padding="@dimen/textview_padding"
        />

    <EditText
        android:id="@+id/numberOfPeopleEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:background="@color/result_background"
        android:elevation="@dimen/elevation"
        android:gravity="center"
        android:padding="@dimen/textview_padding"
        android:ems="10"
        android:hint="Enter Number Of People"
        android:digits="012345678"
        android:inputType="number"
        android:layout_columnSpan="2"/>

    <TextView
        android:id="@+id/eachPersonLabelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/each" />

    <TextView
        android:id="@+id/eachPersonTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:background="@color/result_background"
        android:elevation="@dimen/elevation"
        android:gravity="center"
        android:padding="@dimen/textview_padding"
        />

</GridLayout>

----------

package com.mdc.tippcalcula;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    private static final NumberFormat percentFormat = NumberFormat.getCurrencyInstance();

    private double billAmount = 0.0;
    private double percent = 0.15;
    private double numPeople = 0;
    private TextView percentTextView;
    private TextView tipTextView;
    private TextView amountTextView;
    private TextView totalTextView;
    private TextView eachPersonTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        amountTextView = (TextView)  findViewById (R.id.amountTextView);
        percentTextView = (TextView)  findViewById (R.id.percentTextView);
        tipTextView = (TextView) findViewById(R.id.tipTextView);
        totalTextView = (TextView) findViewById(R.id.totalTextView);
        eachPersonTextView = (TextView) findViewById(R.id.eachPersonTextView);

        tipTextView.setText(currencyFormat.format(0));
        totalTextView.setText(currencyFormat.format(0));
        eachPersonTextView.setText(currencyFormat.format(0));

        EditText amountEditText = (EditText) findViewById(R.id.amountEditText);
        amountEditText.addTextChangedListener(amountEditTextWatcher);


        SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar);
        percentSeekBar.setOnSeekBarChangeListener(seekBarListener);
    }

    private void calculate(){
        percentTextView.setText(percentFormat.format(percent));

        double tip = billAmount * percent;
        double total = billAmount + tip;


        tipTextView.setText(currencyFormat.format(tip));
        totalTextView.setText((currencyFormat.format(total)));
    }



    private final OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b)
        {
            percent = progress / 100.0;
            calculate();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    private final TextWatcher amountEditTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
        {
            try {
                billAmount = Double.parseDouble(charSequence.toString()) / 100.0;
                amountTextView.setText(currencyFormat.format(billAmount));
            }
            catch (NumberFormatException e){
                amountTextView.setText("");
                billAmount = 0.0;
            }
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

        @Override
        public void afterTextChanged(Editable editable) { }
    };
}

------

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_padding">12dp</dimen>
    <dimen name="elevation">4dp</dimen>
    <dimen name="seekbar_height">40dp</dimen>
</resources>

---

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="amount_background">#BBDEFB</color>
    <color name="result_background">#ffE0B2</color>
</resources>

----

<resources>
    <string name="app_name">Tip Calculator</string>
    <string name="enter_amount"> Enter Amount</string>
    <string name="tip_percentage">15%</string>
    <string name="tip">Tip</string>
    <string name="total">Total</string>
    <string name="number">No. Of People</string>
    <string name="each">Each Person</string>
</resources>

Solutions

Expert Solution

***Please upvote/thumbsup if you liked the answer***

Screenshot of the modified Android code:-

Output:-

Modified Android Code to copy:-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    private static final NumberFormat percentFormat = NumberFormat.getCurrencyInstance();

    private double billAmount = 0.0;
    private double percent = 0.15;
    private double numPeople = 0;
    private TextView percentTextView;
    private TextView tipTextView;
    private TextView amountTextView;
    private TextView totalTextView;
    private TextView eachPersonTextView;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        amountTextView = (TextView)  findViewById (R.id.amountTextView);
        percentTextView = (TextView)  findViewById (R.id.percentTextView);
        tipTextView = (TextView) findViewById(R.id.tipTextView);
        totalTextView = (TextView) findViewById(R.id.totalTextView);
        eachPersonTextView = (TextView) findViewById(R.id.eachPersonTextView);


        tipTextView.setText(currencyFormat.format(0));
        totalTextView.setText(currencyFormat.format(0));
        eachPersonTextView.setText(currencyFormat.format(0));

        EditText amountEditText = (EditText) findViewById(R.id.amountEditText);
        amountEditText.addTextChangedListener(amountEditTextWatcher);

        EditText numberofPeopleEditText = (EditText) findViewById(R.id.numberOfPeopleEditText);
        numberofPeopleEditText.addTextChangedListener(numberOfPeopleEditTextWatcher);

        SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar);
        percentSeekBar.setOnSeekBarChangeListener(seekBarListener);
    }

    private void calculate(){
        percentTextView.setText(percentFormat.format(percent));

        double tip = billAmount * percent;
        double total = billAmount + tip;
        double eachPersonAmt = total/numPeople;

        tipTextView.setText(currencyFormat.format(tip));
        totalTextView.setText((currencyFormat.format(total)));
        eachPersonTextView.setText(currencyFormat.format(eachPersonAmt));
    }



    private final OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b)
        {
            percent = progress / 100.0;
            calculate();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    private final TextWatcher amountEditTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
        {
            try {
                billAmount = Double.parseDouble(charSequence.toString()) / 100.0;
                amountTextView.setText(currencyFormat.format(billAmount));
            }
            catch (NumberFormatException e){
                amountTextView.setText("");
                billAmount = 0.0;
            }
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

        @Override
        public void afterTextChanged(Editable editable) { }
    };

    private final TextWatcher numberOfPeopleEditTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
        {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            try {

                numPeople = Integer.parseInt(charSequence.toString());

            }
            catch (NumberFormatException e){

                numPeople = 0;
            }
        }

        @Override
        public void afterTextChanged(Editable editable) { }
    };
}

Related Solutions

Modify the Tip Calculator app to allow the user to enter the number of people in...
Modify the Tip Calculator app to allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members. Code: ----------------TipCalculator.java----------------------- import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class TipCalculator extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("TipCalculator.fxml")); Scene scene = new Scene(root); // attach scene graph to scene...
Write a program that asks the user to enter a number. Display the following pattern by...
Write a program that asks the user to enter a number. Display the following pattern by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user.For example, if the user enters 5, the output would be: * *   * *   *   * *   *   *   * *   *   *   *   * short codes please
Write a JAVA program that allow a user to enter 2 number, starting point and end...
Write a JAVA program that allow a user to enter 2 number, starting point and end point. For example a user me enter 1 and 100. Your program will Add numbers from 1 to 100, add all the even number, and add all the odd numbers Output: The sum of number 1 to 100 is: The sum of even number 1 to 100 is: The sum of odd number 1 to 100 is:
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
We will be creating an application that will allow the user to enter an order, delete...
We will be creating an application that will allow the user to enter an order, delete an order, or display the total of all orders in the system. Create a new .NET Frameword Console Application project in Visual Studio called “OrderTest.” Rename the Program.cs file to OrderTest.cs. Inside the OrderTest class, but outside of the Main method, create a new static Random object. Add a class to the project and call it “Order.” In Order there should be a static...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
Forms often allow a user to enter an integer. Write a programthat takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT