-
Notifications
You must be signed in to change notification settings - Fork 1
/
MGS Bank.rb
258 lines (204 loc) · 5.83 KB
/
MGS Bank.rb
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
class TransactionAccount
attr_accessor :balance
attr_reader :name, :type
def initialize(name, balance)
@name = name
@balance = balance.to_f
end
def deposit(amount)
self.balance += amount
end
def withdraw(amount)
self.balance -= amount
end
def can_withdraw(amount)
self.balance > amount
end
end
class SavingAccount < TransactionAccount
end
class CheckingAccount < TransactionAccount
end
class BankAccount
attr_reader :name, :pin, :accounts
def initialize(name, saving_account, checking_account)
@name = name
@pin = 1234
@accounts = {
"savings" => saving_account,
"checking" => checking_account
}
end
end
class ATMSession
attr_reader :account
def initialize(account)
@account = account
end
def start()
puts self.welcome_message
self.check_pin
self.menu
end
def welcome_message
<<-EOS
I hope you'll enjoy banking with me today. This is my first program ever -- and I'm so PUMPED!
So you know, your PIN is 1234. (You won't be able to do ANY banking without it!)
Welcome to MGS INVESTMENT BANK: today is #{Time.new.inspect}.
EOS
end
def menu_message
<<-EOS
-- Type 'BALANCE' to view account balances.
-- Type 'DEPOSIT' to deposit money into an account.
-- Type 'WITHDRAW' to withdraw money from an account.
-- Type 'TRANSFER' to transfer money between your accounts.
-- Type 'EXIT' to exit your accounts.
EOS
end
def menu()
puts self.menu_message
case self.user_input_text
when "balance"
self.balance_transaction
when "deposit"
self.deposit_transaction
when "withdraw"
self.withdrawal_transaction
when "transfer"
self.transfer_transaction
when "exit"
puts self.balance_summary
puts self.exit_message
exit
else
puts "Sorry, I didn't understand you. Please re-enter your input."
end
self.continue_menu
end
def continue_menu
puts "Would you like to complete another transaction? YES or NO?"
case self.user_input_text
when 'yes'
self.check_pin
self.menu
when 'no'
puts self.balance_summary
puts self.exit_message
exit
else
puts self.error_message
puts continue_menu
end
end
def exit_message()
<<-EOS
On behalf of MGS Bank, thank you for your business.
Have a nice day.
EOS
end
def check_pin()
puts "Please enter your PIN for security."
self.verify_pin
puts "Thank you."
end
def verify_pin()
if self.user_input_pin != self.account.pin
puts "Incorrect PIN. Please re-enter your PIN."
self.verify_pin
end
end
def balance_transaction()
puts self.balance_summary
end
def deposit_transaction
puts "Which account would you like to deposity money into? SAVINGS for CHECKING?"
account = self.get_account
puts "How much would you like to deposit?"
amount = user_input_amount
account.deposit(amount)
puts self.deposit_summary(account, amount)
end
def withdrawal_transaction()
puts "Which account would you like to withdraw money from? SAVINGS or CHECKING?"
account = self.get_account
puts "How much would you like to withdraw?"
amount = user_input_amount
if account.can_withdraw(amount)
account.withdraw(amount)
puts self.withdrawal_summary(account, amount)
else
puts self.low_balance_error(account)
end
end
def transfer_transaction()
puts "Which account would you like to transfer money from? SAVINGS or CHECKING?"
account_from = self.get_account
if account_from == self.account.accounts['savings']
account_to = self.account.accounts['checking']
elsif account_from == self.account.accounts['checking']
account_to = self.account.accounts['savings']
end
puts "How much would you like to transfer?"
amount = user_input_amount
if account_from.can_withdraw(amount)
self.transfer(account_from, account_to, amount)
puts self.transfer_summary(account_from, account_to, amount)
puts self.balance_summary
else
puts self.low_balance_error(account_from)
end
end
def get_account()
account = user_input_text
if self.account.accounts[account]
self.account.accounts[account]
else
puts self.error_message
self.get_account
end
end
def error_message()
"Sorry, I didn't understand you. Please re-enter your input."
end
def transfer(account_from, account_to, amount)
if account_from.can_withdraw(amount)
account_from.withdraw(amount)
account_to.deposit(amount)
end
end
def low_balance_error(account)
"You do not have enough money to complete this transaction. Your balance is #{cash(account.balance)}."
end
def deposit_summary(account, amount)
"Deposited amount: #{cash(amount)}. New balance: #{cash(account.balance)}."
end
def withdrawal_summary(account, amount)
"Withdrawn amount: #{cash(amount)}. New balance: #{cash(account.balance)}."
end
def transfer_summary(account_from, account_to, amount)
"Transferred #{cash(amount)} from #{account_from.name} to #{account_to.name}."
end
def balance_summary()
savings = self.account.accounts["savings"]
checking = self.account.accounts["checking"]
"#{savings.name} balance: #{cash(savings.balance)}. #{checking.name} balance: #{cash(checking.balance)}."
end
def user_input_text()
gets.chomp.downcase
end
def user_input_amount()
gets.chomp.to_s.tr('$,','').to_f
end
def user_input_pin()
gets.chomp.to_i
end
def cash(amount)
'$%.2f' % amount
end
end
saving = SavingAccount.new("Your Savings", 1_000_000.00)
checking = CheckingAccount.new("Your Checking", 1_000_000.00)
user_account = BankAccount.new("Your Account", saving, checking)
atm = ATMSession.new(user_account)
atm.start