Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Working on this this shipping calculator when i run the app given out unfortunitely shiping calculator has stop. Dont know what wrong with my code
Working on this this shipping calculator when i run the app given out unfortunitely shiping calculator has stop. Dont know what wrong with my code
Here is my mainactivity.java code
package com.kean.shippingcalculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private ShipItem shipItem; private EditText weightET; private TextView baseCostTV; private TextView addedCostTV; private TextView totalCostTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); shipItem = new ShipItem(); weightET = (EditText) findViewById(R.id.editText); baseCostTV = (TextView) findViewById(R.id.textView6); addedCostTV = (TextView) findViewById(R.id.textView7); totalCostTV = (TextView) findViewById(R.id.textView8); weightET.addTextChangedListener(weightTextWatcher); } private TextWatcher weightTextWatcher = new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { try { shipItem.setWeight((int)Double.parseDouble(s.toString())); } catch (NumberFormatException e) { shipItem.setWeight(0); } displayShipping(); } public void afterTextChanged (Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after) {} }; private void displayShipping () { baseCostTV.setText("$" + String.format("%.02", shipItem.getBaseCost())); addedCostTV.setText("$" + String.format("%.02", shipItem.getAddedCost())); totalCostTV.setText("$" + String.format("%.02", shipItem.getTotalCost())); } } -------------------------------------------------------------------------------------------
Shiping.java code
package com.kean.shippingcalculator; /** * Created by ademola on 10/31/17. */ public class ShipItem { static final double BASE = 3.00; static final double BASE_EXTRA = 5.00; static final double ADDED = .50; static final double BASE_WEIGHT = 20; static final double BASE_EXTRA_WEIGHT= 50; static final double EXTRA_OUNCES = 5.0; //Data memebers private int mWeight; private double mBaseCost; private double mAddedCost; private double mTotalCost; public ShipItem() { mWeight =0; mAddedCost =0.0; mBaseCost =0.0; mTotalCost =0.0; } public void setWeight (int weight) { mWeight =weight; computeCosts(); } private void computeCosts(){ if (mWeight <=0) mBaseCost = 0.0; else if (mWeight > 0 && mWeight < BASE_WEIGHT) { mBaseCost = BASE; mAddedCost = 0.0; } else if (mWeight >= BASE_WEIGHT && mWeight < BASE_EXTRA_WEIGHT){ mBaseCost = BASE; mAddedCost = (int)(( mWeight - BASE_WEIGHT)/ EXTRA_OUNCES) * ADDED; } else if (mWeight >= BASE_EXTRA_WEIGHT){ mBaseCost = BASE_EXTRA; mAddedCost = (int) ((mWeight - BASE_WEIGHT) / EXTRA_OUNCES) * ADDED; } mTotalCost = mBaseCost + mAddedCost; } public double getBaseCost (){ return mBaseCost; } public double getAddedCost(){ return mAddedCost; } public double getTotalCost(){ return mTotalCost; } } -------------------------------------------------------------------------------------
activity_main.xml code
xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:background="@drawable/shippingbck" tools:context="com.kean.shippingcalculator.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:gravity="center_vertical|center_horizontal|center" android:inputType="number" android:text="Weight of the Package" android:textAppearance="@android:style/TextAppearance.Large" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText" android:layout_width="64dp" android:layout_height="49dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="25dp" android:ems="10" android:inputType="textPersonName" android:textAppearance="@android:style/TextAppearance.Large" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="(in ounces)" android:textAppearance="@android:style/TextAppearance.Large" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="57dp" android:text="Base Cost" app:layout_constraintHorizontal_bias="0.153" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/textView6" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-10dp" android:layout_marginTop="31dp" android:text="Added Cost" app:layout_constraintLeft_toLeftOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/textView3" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-19dp" android:layout_marginTop="32dp" android:text="Total Shipping Cost" app:layout_constraintLeft_toLeftOf="@+id/textView4" app:layout_constraintTop_toBottomOf="@+id/textView4" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="68dp" android:layout_marginTop="58dp" android:text="$0.00" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="31dp" android:text="$0.00" app:layout_constraintHorizontal_bias="0.706" app:layout_constraintLeft_toRightOf="@+id/textView4" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView6" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="23dp" android:text="$0.00" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.606" app:layout_constraintLeft_toRightOf="@+id/textView5" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView7" app:layout_constraintVertical_bias="0.045" /> android.support.constraint.ConstraintLayout>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started