(Solved) : Exercise 13 4 Add Database Tip Calculator App Exercise Ll Create Database Use Tip Calculat Q32988245 . . .
Exercise 13-4 Add a database to the Tip Calculatorapp
In this exercise, you’ll create a database that you can use withthe Tip Calculator app. Add a database class 1. Start AndroidStudio and open the project named ch13_ex4_TipCalculator. 2. Reviewthe code. Note that it includes a Tip class that you can use tostore the data for a tip. 1. Add a database class that creates atable with these column names and data types: _id INTEGER bill_dateINTEGER bill_amount REAL tip_percent REAL When the database classcreates the database, it should also insert two rows of test data.(You can use 0 for the bill date values, but make up some billamount and tip percent values such as 40.60 and .15.) 2. Add apublic getTips method that returns an ArrayList object thatcontains all columns and rows from the database table. 3. Switch tothe activity class. Then, add code to its onResume method thatcalls the getTips method and loops through all saved tips. For eachsaved tip, this code should use LogCat logging to send the billdate milliseconds, the bill amount, and the tip percent to theLogCat view. 4. Run the app. At this point, it should create thedatabase, add two rows of test data, and print the data for eachrow to the LogCat view. Save tip calculations 5. Switch to thedatabase class and add a method that saves tip calculations. 6. Inthe layout for the activity, add a Save button below the otherwidgets. 7. In the class for the activity, add code that saves thecurrent bill amount and tip percent to the database when the userclicks the Save button. This code should also clear the bill amountfrom the user interface. 8. Run the app and test the Save button.To do that, you can execute the onResume method by navigating awayfrom the app and navigating back to it. Then, you can check theLogCat view to make sure the data is saved when you click on theSave button. Display the date and time of the last saved tip 9.Switch to the database class and add a method that returns a Tipobject for the last tip that was saved. 10. Add code to theonResume method that displays the date and time of the last savedtip in the LogCat view. This date and time should be in thisformat: Sep 4 2016 15:54:00 You can use a method of the Tip classto return this format. 11. Execute the onResume method again. Then,check the LogCat view to make sure the data is displayed correctly.Set default tip percent to average tip percent 12. Switch to thedatabase class and add a method that gets the average tip percent.To do that, you can provide an array of strings for the columnsparameter like this: String columns[] = {“AVG(” + TIP_PERCENT +”)”}; (This assumes the TIP_PERCENT constant contains the name ofthe column that stores the tip percent.) 13. Add code to theonResume method that displays the average tip percent in the LogCatview. 14. Execute the onResume method again by navigating away fromthe app and navigating back to it. Then, check the LogCat view tomake sure the average tip percent is displayed correctly. 15.Modify the code for the Save button so it sets the tip percent tothe average tip percent. That way, after you click the Save button,it should set the tip percent to the average tip percent.
<RelativeLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:padding=”10dp” >
<!– The bill amount –>
<TextView
android:id=”@+id/billAmountLabel”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:padding=”10dp”
android:text=”@string/bill_amount_label”
android:textSize=”20sp”
android:textStyle=”bold” />
<EditText
android:id=”@+id/billAmountEditText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/billAmountLabel”
android:layout_marginLeft=”5dp”
android:layout_toRightOf=”@+id/billAmountLabel”
android:ems=”8″
android:inputType=”numberDecimal”
android:text=”@string/bill_amount”
android:textSize=”20sp” >
<requestFocus />
</EditText>
<!– The tip percent –>
<TextView
android:id=”@+id/percentLabel”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/billAmountLabel”
android:layout_below=”@+id/billAmountLabel”
android:padding=”10dp”
android:text=”@string/tip_percent_label”
android:textSize=”20sp”
android:textStyle=”bold” />
<TextView
android:id=”@+id/percentTextView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/percentLabel”
android:layout_alignLeft=”@+id/billAmountEditText”
android:padding=”5dp”
android:text=”@string/tip_percent”
android:textSize=”20sp” />
<Button
android:id=”@+id/percentDownButton”
android:layout_width=”45dp”
android:layout_height=”45dp”
android:layout_alignBaseline=”@+id/percentTextView”
android:layout_marginLeft=”25dp”
android:layout_toRightOf=”@+id/percentTextView”
android:text=”@string/decrease”
android:textSize=”20sp” />
<Button
android:id=”@+id/percentUpButton”
android:layout_width=”45dp”
android:layout_height=”45dp”
android:layout_alignBaseline=”@+id/percentDownButton”
android:layout_toRightOf=”@+id/percentDownButton”
android:text=”@string/increase”
android:textSize=”20sp” />
<!– the tip amount –>
<TextView
android:id=”@+id/tipLabel”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/percentLabel”
android:layout_below=”@+id/percentLabel”
android:padding=”10dp”
android:text=”@string/tip_amount_label”
android:textSize=”20sp”
android:textStyle=”bold” />
<TextView
android:id=”@+id/tipTextView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/tipLabel”
android:layout_alignLeft=”@id/billAmountEditText”
android:padding=”5dp”
android:text=”@string/tip_amount”
android:textSize=”20sp” />
<!– the total –>
<TextView
android:id=”@+id/totalLabel”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/tipLabel”
android:layout_below=”@+id/tipLabel”
android:padding=”10dp”
android:text=”@string/total_amount_label”
android:textSize=”20sp”
android:textStyle=”bold” />
<TextView
android:id=”@+id/totalTextView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/totalLabel”
android:layout_alignLeft=”@+id/tipTextView”
android:padding=”5dp”
android:text=”@string/total_amount”
android:textSize=”20sp” />
</RelativeLayout>
package com.murach.tipcalculator;
import android.annotation.SuppressLint;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Tip {
private long id;
private long dateMillis;
private float billAmount;
private float tipPercent;
public Tip() {
setId(0);
setDateMillis(System.currentTimeMillis());
setBillAmount(0);
setTipPercent(.15f);
}
public Tip(long id, long dateMillis, float billAmount, floattipPercent) {
this.setId(id);
this.setDateMillis(dateMillis);
this.setBillAmount(billAmount);
this.setTipPercent(tipPercent);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id =id;
}
public long getDateMillis() {
returndateMillis;
}
@SuppressLint(“SimpleDateFormat”)
public String getDateStringFormatted(){
// set the date with formatting
Date date = new Date(dateMillis);
SimpleDateFormat sdf = newSimpleDateFormat(“MMM d yyyy HH:mm:ss”);
return sdf.format(date);
}
public void setDateMillis(longdateMillis) {
this.dateMillis= dateMillis;
}
public float getBillAmount() {
returnbillAmount;
}
public String getBillAmountFormatted(){
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(billAmount);
}
public void setBillAmount(floatbillAmount) {
this.billAmount= billAmount;
}
public float getTipPercent() {
returntipPercent;
}
public String getTipPercentFormatted(){
NumberFormat percent = NumberFormat.getPercentInstance();
return percent.format(tipPercent);
}
public void setTipPercent(floattipPercent) {
this.tipPercent= tipPercent;
}
}
package com.murach.tipcalculator;
import java.text.NumberFormat;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class TipCalculatorActivity extends Activity
implements OnEditorActionListener, OnClickListener {
// define variables for the widgets
private EditText billAmountEditText;
private TextView percentTextView;
private Button percentUpButton;
private Button percentDownButton;
private TextView tipTextView;
private TextView totalTextView;
// define instance variables that should be saved
private String billAmountString = “”;
private float tipPercent = .15f;
// set up preferences
private SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip_calculator);
// get references to the widgets
billAmountEditText = (EditText)findViewById(R.id.billAmountEditText);
percentTextView = (TextView)findViewById(R.id.percentTextView);
percentUpButton = (Button)findViewById(R.id.percentUpButton);
percentDownButton = (Button)findViewById(R.id.percentDownButton);
tipTextView = (TextView) findViewById(R.id.tipTextView);
totalTextView = (TextView) findViewById(R.id.totalTextView);
// set the listeners
billAmountEditText.setOnEditorActionListener(this);
percentUpButton.setOnClickListener(this);
percentDownButton.setOnClickListener(this);
// get default SharedPreferences object
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public void onPause() {
// save the instance variables
Editor editor = prefs.edit();
editor.putString(“billAmountString”, billAmountString);
editor.putFloat(“tipPercent”, tipPercent);
editor.commit();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
// get the instance variables
billAmountString = prefs.getString(“billAmountString”, “”);
tipPercent = prefs.getFloat(“tipPercent”, 0.15f);
// set the bill amount on its widget
billAmountEditText.setText(billAmountString);
// calculate and display
calculateAndDisplay();
}
public void calculateAndDisplay() {
// get the bill amount
billAmountString = billAmountEditText.getText().toString();
float billAmount;
if (billAmountString.equals(“”)) {
billAmount = 0;
}
else {
billAmount = Float.parseFloat(billAmountString);
}
// calculate tip and total
float tipAmount = billAmount * tipPercent;
float totalAmount = billAmount + tipAmount;
// display the other results with formatting
NumberFormat currency = NumberFormat.getCurrencyInstance();
tipTextView.setText(currency.format(tipAmount));
totalTextView.setText(currency.format(totalAmount));
NumberFormat percent = NumberFormat.getPercentInstance();
percentTextView.setText(percent.format(tipPercent));
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEventevent) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId ==EditorInfo.IME_ACTION_UNSPECIFIED) {
calculateAndDisplay();
}
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.percentDownButton:
tipPercent = tipPercent – .01f;
calculateAndDisplay();
break;
case R.id.percentUpButton:
tipPercent = tipPercent + .01f;
calculateAndDisplay();
break;
}
}
}
Expert Answer
Answer to Exercise 13 4 Add Database Tip Calculator App Exercise Ll Create Database Use Tip Calculat Q32988245 . . .
OR