|
11 | 11 | import android.widget.Button; |
12 | 12 | import android.widget.EditText; |
13 | 13 | import android.widget.TextView; |
| 14 | +import android.widget.Toast; |
| 15 | + |
14 | 16 | import androidx.annotation.Nullable; |
15 | 17 | import androidx.fragment.app.Fragment; |
16 | 18 | import java.util.Objects; |
@@ -100,14 +102,22 @@ public void onClick(View view) { |
100 | 102 | percentage_tip.setText(""); |
101 | 103 | } |
102 | 104 |
|
103 | | - float total_price_int = Float.parseFloat(total_price.getText().toString()); |
104 | | - float percentage_tip_int = Float.parseFloat(percentage_tip.getText().toString()); |
| 105 | + float total_price_int; |
| 106 | + float percentage_tip_int; |
| 107 | + float calculated_tip; |
| 108 | + double rounded_tip; |
| 109 | + |
| 110 | + try{ |
| 111 | + total_price_int = Float.parseFloat(total_price.getText().toString()); |
| 112 | + percentage_tip_int = Float.parseFloat(percentage_tip.getText().toString()); |
105 | 113 |
|
106 | | - float calculated_tip = (total_price_int * (percentage_tip_int / 100)); |
107 | | - double rounded_tip = Math.round(calculated_tip * 100.0) / 100.0; |
| 114 | + calculated_tip = (total_price_int * (percentage_tip_int / 100)); |
| 115 | + rounded_tip = Math.round(calculated_tip * 100.0) / 100.0; |
108 | 116 |
|
109 | | - // String formatting for displaying a trailing zero if there is only a value in the tenths |
110 | | - tip_result.setText("TIP: $" + String.format("%.2f", rounded_tip)); |
| 117 | + tip_result.setText("TIP: $" + String.format("%.2f", rounded_tip)); |
| 118 | + }catch(Exception e){ |
| 119 | + Toast.makeText(getActivity(), "Enter Price/Percentage Info!", Toast.LENGTH_SHORT).show(); |
| 120 | + } |
111 | 121 | } |
112 | 122 | }); |
113 | 123 | } |
|
0 commit comments