-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
104 lines (94 loc) · 4.35 KB
/
MainActivity.java
File metadata and controls
104 lines (94 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.example.user.tipcalculator;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText amount, per;
RadioButton fif, twn, other;
Button cal;
TextView tip, total;
float num_amount, num_per;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amount = (EditText)findViewById(R.id.amount);
per = (EditText)findViewById(R.id.per);
fif = (RadioButton)findViewById(R.id.fif);
twn = (RadioButton)findViewById(R.id.twn);
other = (RadioButton)findViewById(R.id.other);
cal = (Button)findViewById(R.id.cal);
tip = (TextView)findViewById(R.id.tip);
total = (TextView)findViewById(R.id.total);
cal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tip.setText("");
total.setText("");
if(amount.getText().toString().isEmpty()) // amount is empty exceptional condition
{
String text = "Non_value";
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
else if(Float.valueOf(amount.getText().toString()) < 0) // amount is negative value exceptional condition
{
String text = "Can't input negative";
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
else
{
num_amount = Float.valueOf(amount.getText().toString());
if(fif.isChecked())
{
tip.setText("Tip: " + String.valueOf(calculate(num_amount, 15)));
total.setText("Total: " + String.valueOf(calculate(num_amount, 15) + num_amount));
}
else if(twn.isChecked())
{
tip.setText("Tip: " + String.valueOf(calculate(num_amount, 20)));
total.setText("Total: " + String.valueOf(calculate(num_amount, 20) + num_amount));
}
else if(other.isChecked())
{
if(per.getText().toString().isEmpty()) // Tip is empty exceptional condition
{
String text = "Non_value";
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
else if(Float.valueOf(per.getText().toString()) < 0) // Tip is negative value exceptional condition
{
String text = "Can't input negative";
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
}
else if(Float.valueOf(per.getText().toString()) == 0)
{
tip.setText("Tip: 0");
total.setText("Total: " + amount.getText().toString());
}
else {
num_per = Float.valueOf(per.getText().toString());
tip.setText("Tip: " + String.valueOf(calculate(num_amount, num_per)));
total.setText("Total: " + String.valueOf(calculate(num_amount, num_per) + num_amount));
}
}
}
}
});
}
public float calculate(float amount, float percent) // Tip and Total pay calculate function
{
float result=0;
result = amount * percent / 100;
return result;
}
}