-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavings.js
More file actions
158 lines (141 loc) · 4 KB
/
savings.js
File metadata and controls
158 lines (141 loc) · 4 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, TextInput } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';
const HideShowButton = ({ onPress, showBalance }) => {
const buttonIcon = showBalance ? 'visibility-off' : 'visibility';
return (
<TouchableOpacity style={styles.button} onPress={onPress}>
<MaterialIcons name={buttonIcon} size={24} color="black" />
</TouchableOpacity>
);
};
export default class Savings extends Component {
state = {
balance: 0,
showBalance: true,
textInputValue: '',
};
handleDeposit = () => {
const { balance, textInputValue } = this.state;
const depositAmount = parseFloat(textInputValue);
if (!isNaN(depositAmount) && depositAmount > 0) {
this.setState((prevState) => ({
balance: prevState.balance + depositAmount,
textInputValue: '',
}));
}
};
handleWithdrawal = () => {
const { balance, textInputValue } = this.state;
const withdrawalAmount = parseFloat(textInputValue);
if (!isNaN(withdrawalAmount) && withdrawalAmount > 0 && balance >= withdrawalAmount) {
this.setState((prevState) => ({
balance: prevState.balance - withdrawalAmount,
textInputValue: '',
}));
}
};
toggleBalanceVisibility = () => {
this.setState((prevState) => ({
showBalance: !prevState.showBalance,
}));
};
handleTextInputChange = (value) => {
this.setState({
textInputValue: value,
});
};
render() {
const { balance, showBalance, textInputValue } = this.state;
return (
<View style={styles.container}>
<Text style={styles.title}>Savings Account</Text>
<View style={{ paddingBottom: 10 }} />
<Text style={styles.sub}>
<Text style={{ color: 'black' }}>"</Text>Every penny saved is a step closer to financial independence and the
ability to live life on your terms.<Text style={{ color: 'black' }}>"</Text>
</Text>
<TextInput
placeholder="Enter amount....."
keyboardType="numeric"
style={styles.input}
placeholderTextColor="black"
value={textInputValue}
onChangeText={this.handleTextInputChange}
/>
<View style={styles.buttons}>
<TouchableOpacity style={styles.button} onPress={this.handleDeposit}>
<Text style={styles.buttonText}>Deposit</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={this.handleWithdrawal}>
<Text style={styles.buttonText}>Withdraw</Text>
</TouchableOpacity>
</View>
<View style={styles.balanceContainer}>
<Text style={styles.balanceTitle}>Account Balance:</Text>
{showBalance ? (
<Text style={styles.balanceAmount}>{balance} KES</Text>
) : (
<HideShowButton onPress={this.toggleBalanceVisibility} showBalance={showBalance} />
)}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: 'gainsboro',
paddingVertical: 20,
paddingHorizontal: 10,
},
title: {
color: 'black',
fontSize: 20,
fontWeight: 'bold',
},
sub: {
fontSize: 16,
color: 'grey',
textAlign: 'center',
marginVertical: 10,
},
input: {
borderWidth: 1,
borderColor: 'black',
width: '70%',
marginBottom: 10,
paddingHorizontal: 10,
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '100%',
marginVertical: 10,
},
balanceContainer: {
flexDirection: 'row',
alignItems: 'center',
marginTop: 20,
},
balanceTitle: {
fontSize: 20,
marginRight: 10,
color: 'black',
},
balanceAmount: {
fontSize: 16,
fontWeight: 'bold',
color: 'black',
},
button: {
backgroundColor: 'blue',
padding: 10,
borderRadius: 5,
},
buttonText: {
color: 'white',
fontSize: 16,
}});